Java Code Examples for com.sun.codemodel.JExpr#_new
The following examples show how to use
com.sun.codemodel.JExpr#_new .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FastDeserializerGenerator.java From avro-fastserde with Apache License 2.0 | 6 votes |
private void processFixed(final Schema schema, JBlock body, FieldAction action, BiConsumer<JBlock, JExpression> putFixedIntoParent) { if (action.getShouldRead()) { JVar fixedBuffer = body.decl(codeModel.ref(byte[].class), getVariableName(schema.getName())) .init(JExpr.direct(" new byte[" + schema.getFixedSize() + "]")); body.directStatement(DECODER + ".readFixed(" + fixedBuffer.name() + ");"); JInvocation createFixed = JExpr._new(schemaAssistant.classFromSchema(schema)); if (useGenericTypes) createFixed = createFixed.arg(getSchemaExpr(schema)); putFixedIntoParent.accept(body, createFixed.arg(fixedBuffer)); } else { body.directStatement(DECODER + ".skipFixed(" + schema.getFixedSize() + ");"); } }
Example 2
Source File: JaxRsEnumRule.java From apicurio-studio with Apache License 2.0 | 6 votes |
private void addFactoryMethod(JDefinedClass _enum, JType backingType) { JFieldVar quickLookupMap = addQuickLookupMap(_enum, backingType); JMethod fromValue = _enum.method(JMod.PUBLIC | JMod.STATIC, _enum, "fromValue"); JVar valueParam = fromValue.param(backingType, "value"); JBlock body = fromValue.body(); JVar constant = body.decl(_enum, "constant"); constant.init(quickLookupMap.invoke("get").arg(valueParam)); JConditional _if = body._if(constant.eq(JExpr._null())); JInvocation illegalArgumentException = JExpr._new(_enum.owner().ref(IllegalArgumentException.class)); JExpression expr = valueParam; // if string no need to add "" if(!isString(backingType)){ expr = expr.plus(JExpr.lit("")); } illegalArgumentException.arg(expr); _if._then()._throw(illegalArgumentException); _if._else()._return(constant); ruleFactory.getAnnotator().enumCreatorMethod(_enum, fromValue); }
Example 3
Source File: PojoBuilder.java From springmvc-raml-plugin with Apache License 2.0 | 6 votes |
private JInvocation appendFieldsToHashCode(Map<String, JFieldVar> nonTransientAndNonStaticFields, JClass hashCodeBuilderRef) { JInvocation invocation = JExpr._new(hashCodeBuilderRef); Iterator<Map.Entry<String, JFieldVar>> iterator = nonTransientAndNonStaticFields.entrySet().iterator(); if (!this.pojo._extends().name().equals(Object.class.getSimpleName())) { // If // this // POJO // has // a // superclass, // append // the // superclass // hashCode() // method. invocation = invocation.invoke("appendSuper").arg(JExpr._super().invoke("hashCode")); } while (iterator.hasNext()) { Map.Entry<String, JFieldVar> pair = iterator.next(); invocation = invocation.invoke("append").arg(pair.getValue()); } return invocation; }
Example 4
Source File: PojoBuilder.java From springmvc-raml-plugin with Apache License 2.0 | 6 votes |
private JInvocation appendFieldsToEquals(Map<String, JFieldVar> nonTransientAndNonStaticFields, JVar otherObject, JClass equalsBuilderRef) { JInvocation invocation = JExpr._new(equalsBuilderRef); Iterator<Map.Entry<String, JFieldVar>> iterator = nonTransientAndNonStaticFields.entrySet().iterator(); if (!this.pojo._extends().name().equals(Object.class.getSimpleName())) {// If // this // POJO // has // a // superclass, // append // the // superclass // equals() // method. invocation = invocation.invoke("appendSuper").arg(JExpr._super().invoke("equals").arg(otherObject)); } while (iterator.hasNext()) { Map.Entry<String, JFieldVar> pair = iterator.next(); invocation = invocation.invoke("append").arg(pair.getValue()).arg(otherObject.ref(pair.getKey())); } return invocation; }
Example 5
Source File: PluginImpl.java From immutable-xjc with MIT License | 6 votes |
private JExpression getEmptyCollectionExpression(JCodeModel codeModel, JVar param) { if (param.type().erasure().equals(codeModel.ref(Collection.class))) { return codeModel.ref(Collections.class).staticInvoke("emptyList"); } else if (param.type().erasure().equals(codeModel.ref(List.class))) { return codeModel.ref(Collections.class).staticInvoke("emptyList"); } else if (param.type().erasure().equals(codeModel.ref(Map.class))) { return codeModel.ref(Collections.class).staticInvoke("emptyMap"); } else if (param.type().erasure().equals(codeModel.ref(Set.class))) { return codeModel.ref(Collections.class).staticInvoke("emptySet"); } else if (param.type().erasure().equals(codeModel.ref(SortedMap.class))) { return JExpr._new(codeModel.ref(TreeMap.class)); } else if (param.type().erasure().equals(codeModel.ref(SortedSet.class))) { return JExpr._new(codeModel.ref(TreeSet.class)); } return param; }
Example 6
Source File: HiveFuncHolder.java From dremio-oss with Apache License 2.0 | 5 votes |
private JInvocation getUDFInstance(JCodeModel m) { if (isGenericUDF) { return JExpr._new(m.directClass(genericUdfClazz.getCanonicalName())); } else { return JExpr._new(m.directClass(GenericUDFBridge.class.getCanonicalName())) .arg(JExpr.lit(udfName)) .arg(JExpr.lit(false)) .arg(JExpr.lit(udfClazz.getCanonicalName().toString())); } }
Example 7
Source File: PluginImpl.java From immutable-xjc with MIT License | 5 votes |
private JExpression getNewCollectionExpression(JCodeModel codeModel, JType jType) { List<JClass> typeParams = ((JClass) jType).getTypeParameters(); JClass typeParameter = null; if (typeParams.iterator().hasNext()) { typeParameter = typeParams.iterator().next(); } JClass newClass = null; if (jType.erasure().equals(codeModel.ref(Collection.class))) { newClass = codeModel.ref(ArrayList.class); } else if (jType.erasure().equals(codeModel.ref(List.class))) { newClass = codeModel.ref(ArrayList.class); } else if (jType.erasure().equals(codeModel.ref(Map.class))) { newClass = codeModel.ref(HashMap.class); } else if (jType.erasure().equals(codeModel.ref(Set.class))) { newClass = codeModel.ref(HashSet.class); } else if (jType.erasure().equals(codeModel.ref(SortedMap.class))) { newClass = codeModel.ref(TreeMap.class); } else if (jType.erasure().equals(codeModel.ref(SortedSet.class))) { newClass = codeModel.ref(TreeSet.class); } if (newClass != null && typeParameter != null) { newClass = newClass.narrow(typeParameter); } return newClass == null ? JExpr._null() : JExpr._new(newClass); }
Example 8
Source File: ClassGenerator.java From Bats with Apache License 2.0 | 4 votes |
/** * Prepare the generated class for use as a plain-old Java class * (to be compiled by a compiler and directly loaded without a * byte-code merge. Three additions are necessary: * <ul> * <li>The class must extend its template as we won't merge byte * codes.</li> * <li>A constructor is required to call the <tt>__DRILL_INIT__</tt> * method. If this is a nested class, then the constructor must * include parameters defined by the base class.</li> * <li>For each nested class, create a method that creates an * instance of that nested class using a well-defined name. This * method overrides the base class method defined for this purpose.</li> */ public void preparePlainJava() { // If this generated class uses the "straight Java" technique // (no byte code manipulation), then the class must extend the // template so it plays by normal Java rules for finding the // template methods via inheritance rather than via code injection. Class<?> baseClass = sig.getSignatureClass(); clazz._extends(baseClass); // Create a constuctor for the class: either a default one, // or (for nested classes) one that passes along arguments to // the super class constructor. Constructor<?>[] ctors = baseClass.getConstructors(); for (Constructor<?> ctor : ctors) { addCtor(ctor.getParameterTypes()); } // Some classes have no declared constructor, but we need to generate one // anyway. if ( ctors.length == 0 ) { addCtor( new Class<?>[] {} ); } // Repeat for inner classes. for(ClassGenerator<T> child : innerClasses.values()) { child.preparePlainJava(); // If there are inner classes, then we need to generate a "shim" method // to instantiate that class. // // protected TemplateClass.TemplateInnerClass newTemplateInnerClass( args... ) { // return new GeneratedClass.GeneratedInnerClass( args... ); // } // // The name is special, it is "new" + inner class name. The template must // provide a method of this name that creates the inner class instance. String innerClassName = child.clazz.name(); JMethod shim = clazz.method(JMod.PROTECTED, child.sig.getSignatureClass(), "new" + innerClassName); JInvocation childNew = JExpr._new(child.clazz); Constructor<?>[] childCtors = child.sig.getSignatureClass().getConstructors(); Class<?>[] params; if (childCtors.length==0) { params = new Class<?>[0]; } else { params = childCtors[0].getParameterTypes(); } for (int i = 1; i < params.length; i++) { Class<?> p = params[i]; childNew.arg(shim.param(model._ref(p), "arg" + i)); } shim.body()._return(JExpr._this().invoke("injectMembers").arg(childNew)); } }
Example 9
Source File: FastDeserializerGenerator.java From avro-fastserde with Apache License 2.0 | 4 votes |
private void processArray(JVar arraySchemaVar, final String name, final Schema arraySchema, final Schema readerArraySchema, JBlock parentBody, FieldAction action, BiConsumer<JBlock, JExpression> putArrayIntoParent) { if (action.getShouldRead()) { Symbol valuesActionSymbol = null; for (Symbol symbol : action.getSymbol().production) { if (Symbol.Kind.REPEATER.equals(symbol.kind) && "array-end".equals(getSymbolPrintName(((Symbol.Repeater) symbol).end))) { valuesActionSymbol = symbol; break; } } if (valuesActionSymbol == null) { throw new FastDeserializerGeneratorException("Unable to determine action for array: " + name); } action = FieldAction.fromValues(arraySchema.getElementType().getType(), action.getShouldRead(), valuesActionSymbol); } else { action = FieldAction.fromValues(arraySchema.getElementType().getType(), false, EMPTY_SYMBOL); } final JVar arrayVar = action.getShouldRead() ? declareValueVar(name, readerArraySchema, parentBody) : null; JVar chunkLen = parentBody.decl(codeModel.LONG, getVariableName("chunkLen"), JExpr.direct(DECODER + ".readArrayStart()")); JConditional conditional = parentBody._if(chunkLen.gt(JExpr.lit(0))); JBlock ifBlock = conditional._then(); JClass arrayClass = schemaAssistant.classFromSchema(action.getShouldRead() ? readerArraySchema : arraySchema, false); if (action.getShouldRead()) { JInvocation newArrayExp = JExpr._new(arrayClass); if (useGenericTypes) { newArrayExp = newArrayExp.arg(JExpr.cast(codeModel.INT, chunkLen)).arg(getSchemaExpr(arraySchema)); } ifBlock.assign(arrayVar, newArrayExp); JBlock elseBlock = conditional._else(); if (useGenericTypes) { elseBlock.assign(arrayVar, JExpr._new(arrayClass).arg(JExpr.lit(0)).arg(getSchemaExpr(arraySchema))); } else { elseBlock.assign(arrayVar, codeModel.ref(Collections.class).staticInvoke("emptyList")); } } JDoLoop doLoop = ifBlock._do(chunkLen.gt(JExpr.lit(0))); JForLoop forLoop = doLoop.body()._for(); JVar counter = forLoop.init(codeModel.INT, getVariableName("counter"), JExpr.lit(0)); forLoop.test(counter.lt(chunkLen)); forLoop.update(counter.incr()); JBlock forBody = forLoop.body(); JVar elementSchemaVar = null; BiConsumer<JBlock, JExpression> putValueInArray = null; if (action.getShouldRead()) { putValueInArray = (block, expression) -> block.invoke(arrayVar, "add").arg(expression); if (useGenericTypes) { elementSchemaVar = declareSchemaVar(arraySchema.getElementType(), name + "ArrayElemSchema", arraySchemaVar.invoke("getElementType")); } } if (SchemaAssistant.isComplexType(arraySchema.getElementType())) { String elemName = name + "Elem"; Schema readerArrayElementSchema = action.getShouldRead() ? readerArraySchema.getElementType() : null; processComplexType(elementSchemaVar, elemName, arraySchema.getElementType(), readerArrayElementSchema, forBody, action, putValueInArray); } else { // to preserve reader string specific options use reader array schema if (action.getShouldRead() && Schema.Type.STRING.equals(arraySchema.getElementType().getType())) { processSimpleType(readerArraySchema.getElementType(), forBody, action, putValueInArray); } else { processSimpleType(arraySchema.getElementType(), forBody, action, putValueInArray); } } doLoop.body().assign(chunkLen, JExpr.direct(DECODER + ".arrayNext()")); if (action.getShouldRead()) { putArrayIntoParent.accept(parentBody, arrayVar); } }
Example 10
Source File: AbstractListField.java From hyperjaxb3 with BSD 2-Clause "Simplified" License | 4 votes |
private JExpression newCoreList() { return JExpr._new(getCoreListType()); }
Example 11
Source File: PluginContext.java From jaxb2-rich-contract-plugin with MIT License | 4 votes |
public JInvocation newArrayList(final JClass elementType) { return JExpr._new(this.arrayListClass.narrow(elementType)); }