private JMethod addPublicGetMethod(JDefinedClass jclass, JMethod internalGetMethod, JFieldRef notFoundValue) { JMethod method = jclass.method(PUBLIC, jclass.owner()._ref(Object.class), GETTER_NAME); JTypeVar returnType = method.generify("T"); method.type(returnType); Models.suppressWarnings(method, "unchecked"); JVar nameParam = method.param(String.class, "name"); JBlock body = method.body(); JVar valueVar = body.decl(jclass.owner()._ref(Object.class), "value", invoke(internalGetMethod).arg(nameParam).arg(notFoundValue)); JConditional found = method.body()._if(notFoundValue.ne(valueVar)); found._then()._return(cast(returnType, valueVar)); JBlock notFound = found._else(); JMethod getAdditionalProperties = jclass.getMethod("getAdditionalProperties", new JType[] {}); if (getAdditionalProperties != null) { notFound._return(cast(returnType, invoke(getAdditionalProperties).invoke("get").arg(nameParam))); } else { notFound._throw(illegalArgumentInvocation(jclass, nameParam)); } return method; }
@Test public void shouldAddNotFoundField() { JFieldRef var = rule.getOrAddNotFoundVar(type); assertThat(var, notNullValue()); }
/** * Adds local variable declaration based on given name and type. * * @param t major type * @param name variable name * @param includeNewInstance whether to create new instance * @return holder instance */ public HoldingContainer declare(MajorType t, String name, boolean includeNewInstance) { JType holderType = getHolderType(t); JVar var; if (includeNewInstance) { var = getEvalBlock().decl(holderType, name + index, JExpr._new(holderType)); } else { var = getEvalBlock().decl(holderType, name + index); } JFieldRef outputSet = null; if (t.getMode() == DataMode.OPTIONAL) { outputSet = var.ref("isSet"); } index++; return new HoldingContainer(t, var, var.ref("value"), outputSet); }
@Override public void process(Element element, EComponentHolder holder) throws Exception { // I just copied this block from BeanHandler, I'm not sure I understand it TypeMirror typeMirror = annotationHelper.extractAnnotationClassParameter(element); if (typeMirror == null) { typeMirror = element.asType(); typeMirror = holder.processingEnvironment().getTypeUtils().erasure(typeMirror); } JClass injectedClass = refClass(typeMirror.toString()); JFieldRef injectField = ref(element.getSimpleName().toString()); final JBlock initBody = holder.getInitBody(); initBody.assign(injectField, ModuleCodeGenerator.moduleGetInstanceOrAddDefaultIfNeeded(holder, annotationHelper, holder.getGeneratedClass(), holder.getInit(), injectedClass, "", typeHasAnnotation(typeMirror, EBean.class))); // field = Module.getInstance() }
private JMethod generateLazyProxyInitGetter(final ClassOutline classOutline, final FieldOutline fieldOutline) { final JCodeModel m = classOutline.parent().getCodeModel(); final JDefinedClass definedClass = classOutline.implClass; final String fieldName = fieldOutline.getPropertyInfo().getName(false); final String getterName = "get" + fieldOutline.getPropertyInfo().getName(true); final JFieldVar collectionField = definedClass.fields().get(fieldName); final JClass elementType = ((JClass) collectionField.type()).getTypeParameters().get(0); final JClass proxyFieldType = m.ref(BoundList.class).narrow(elementType); final JFieldRef collectionFieldRef = JExpr._this().ref(collectionField); final JFieldRef proxyField = JExpr._this().ref(collectionField.name() + BoundPropertiesPlugin.PROXY_SUFFIX); final JMethod oldGetter = definedClass.getMethod(getterName, new JType[0]); definedClass.methods().remove(oldGetter); final JMethod newGetter = definedClass.method(JMod.PUBLIC, proxyFieldType, getterName); newGetter.body()._if(collectionFieldRef.eq(JExpr._null()))._then().assign(collectionFieldRef, JExpr._new(m.ref(ArrayList.class).narrow(elementType))); final JBlock ifProxyNull = newGetter.body()._if(proxyField.eq(JExpr._null()))._then(); ifProxyNull.assign(proxyField, JExpr._new(m.ref(BoundListProxy.class).narrow(elementType)).arg(collectionFieldRef)); newGetter.body()._return(proxyField); return newGetter; }
public HoldingContainer declare(MajorType t, boolean includeNewInstance) { JType holderType = getHolderType(t); JVar var; if (includeNewInstance) { var = getEvalBlock().decl(holderType, "out" + index, JExpr._new(holderType)); } else { var = getEvalBlock().decl(holderType, "out" + index); } JFieldRef outputSet = null; if (t.getMode() == DataMode.OPTIONAL) { outputSet = var.ref("isSet"); } index++; return new HoldingContainer(t, var, var.ref("value"), outputSet); }
public HoldingContainer(MajorType t, JVar holder, JFieldRef value, JFieldRef isSet, boolean singularRepeated, boolean isReader) { this.holder = holder; this.value = value; this.isSet = isSet; this.type = t; this.isConstant = false; this.singularRepeated = singularRepeated; this.isReader = isReader; }
public JFieldRef f(String name) { return holder.ref(name); }
public HoldingContainer(CompleteType t, JVar holder, JFieldRef value, JFieldRef isSet, boolean singularRepeated, boolean isReader) { this.holder = holder; this.value = value; this.isSet = isSet; this.type = t; this.isConstant = false; this.singularRepeated = singularRepeated; this.isReader = isReader; }
public JFieldRef getValue() { return value; }
private void renderClassLevelAnnotations(JDefinedClass classModel, List<FieldModel> fields) throws Exception { JFieldRef constantsClass = classModel.staticRef(Util.CONSTANTS_CLASS_NAME); JFieldRef elementsClass = classModel.staticRef(Util.ELEMENTS_CLASS_NAME); JClass coreConstants = codeModel.ref(CoreConstants.class); JFieldRef commonElementsRef = coreConstants.staticRef("CommonElements"); // XmlRootElement JAnnotationUse rootElementAnnotation = classModel.annotate(XmlRootElement.class); rootElementAnnotation.param("name", constantsClass.ref(Util.ROOT_ELEMENT_NAME_FIELD)); // XmlAccessorType JAnnotationUse xmlAccessorTypeAnnotation = classModel.annotate(XmlAccessorType.class); xmlAccessorTypeAnnotation.param("value", XmlAccessType.NONE); // XmlType JAnnotationUse xmlTypeAnnotation = classModel.annotate(XmlType.class); xmlTypeAnnotation.param("name", constantsClass.ref(Util.TYPE_NAME_FIELD)); JAnnotationArrayMember propOrderMember = xmlTypeAnnotation.paramArray("propOrder"); for (FieldModel field : fields) { if (Util.isCommonElement(field.fieldName)) { propOrderMember.param(commonElementsRef.ref(Util.toConstantsVariable(field.fieldName))); } else { propOrderMember.param(elementsClass.ref(Util.toConstantsVariable(field.fieldName))); } } propOrderMember.param(commonElementsRef.ref("FUTURE_ELEMENTS")); }
private void renderField(JDefinedClass classModel, FieldModel fieldModel) { JFieldVar field = classModel.field(JMod.PRIVATE | JMod.FINAL, fieldModel.fieldType, fieldModel.fieldName); JAnnotationUse annotation = field.annotate(XmlElement.class); if (Util.isCommonElement(fieldModel.fieldName)) { JClass coreConstants = codeModel.ref(CoreConstants.class); JFieldRef commonElementsRef = coreConstants.staticRef("CommonElements"); annotation.param("name", commonElementsRef.ref(Util.toConstantsVariable(fieldModel.fieldName))); } else { JClass elementsClass = codeModel.ref(Util.ELEMENTS_CLASS_NAME); JFieldRef fieldXmlNameRef = elementsClass.staticRef(Util.toConstantsVariable(fieldModel.fieldName)); annotation.param("name", fieldXmlNameRef); } annotation.param("required", false); }
@Override public void process(Element element, EComponentHolder holder) throws Exception { JFieldRef injectField = ref(element.getSimpleName().toString()); // I just copied this block from BeanHandler, I'm not sure I understand it TypeMirror typeMirror = annotationHelper.extractAnnotationClassParameter(element); if (typeMirror == null) { typeMirror = element.asType(); typeMirror = holder.processingEnvironment().getTypeUtils().erasure(typeMirror); } JClass injectedClass = refClass(typeMirror.toString()); final JBlock initBody = holder.getInitBody(); initBody.assign(injectField, cast(injectedClass, ModuleCodeGenerator.getModule(holder))); // field = Module.getInstance() }
@Override public void caseAPrimaryFieldAccess(APrimaryFieldAccess node) { ExpressionAdapter ea = new ExpressionAdapter(new JExprParent(), context); node.getExpressionNoName().apply(ea); JFieldRef field = ea.expr.ref(node.getIdentifier().getText()); expr = field; }
@Override public void caseAHandleDeclaration(AHandleDeclaration node) { String handlerId = node.getHandlerId().getText(); String portId = node.getPortId().getText(); String eventId = node.getEventId().getText(); TypeAdapter ta = new TypeAdapter(context); node.getClassType().apply(ta); JFieldRef handlerField = createHandler(node.getHandlerId(), ta.type, eventId, node.getBlock()); JInvocation sub = initBlock.invoke("subscribe"); sub.arg(handlerField); sub.arg(JExpr.ref(portId)); }
@Override public void caseAHandlerDeclaration(AHandlerDeclaration node) { String handlerId = node.getHandlerId().getText(); String eventId = node.getEventId().getText(); TypeAdapter ta = new TypeAdapter(context); node.getClassType().apply(ta); JFieldRef handlerField = createHandler(node.getHandlerId(), ta.type, eventId, node.getBlock()); }
private JFieldRef createHandler(TIdentifier handlerIdentifier, JType eventType, String eventId, PBlock block) { String handlerId = handlerIdentifier.getText(); JMethod handlerMethod = context.method(JMod.PRIVATE | JMod.FINAL, handlerIdentifier, context.unit.VOID, Optional.of(handlerId + "Method")); try { JVar eVar = handlerMethod.param(eventType, eventId); context.addField(eventId, eVar, Field.Type.NORMAL); context.pushStatementScope(); try { BlockStatementAdapter bsa = new BlockStatementAdapter(context, handlerMethod.body()); block.apply(bsa); } finally { context.popScope(); } } finally { context.popScope(); } JClass specificHType = handlerType.narrow(eventType); JDefinedClass anonHandler = context.anonymousClass(specificHType); try { JMethod anonMethod = context.method(JMod.PUBLIC, handlerIdentifier, context.unit.VOID, Optional.of("handle")); try { JVar amp = anonMethod.param(eventType, eventId); context.addField(eventId, amp, Field.Type.NORMAL); JInvocation handlerMethodInv = anonMethod.body().invoke(handlerMethod); handlerMethodInv.arg(JExpr.ref(eventId)); } finally { context.popScope(); } } finally { context.popScope(); } JFieldVar handlerField = clazz.field(JMod.PROTECTED | JMod.FINAL, specificHType, handlerId, JExpr._new(anonHandler)); context.addField(handlerId, handlerField, Field.Type.HANDLER); return JExpr.ref(handlerId); }
protected JFieldRef createField() { final JFieldVar field = outline.implClass.field(JMod.PROTECTED + JMod.TRANSIENT, propertyListType, property.getName(false)); // field.annotate(XmlTransient.class); return JExpr._this().ref(field); }
protected JFieldRef createField() { final JFieldVar field = outline.implClass.field(JMod.PROTECTED + JMod.TRANSIENT, propertyListType, property.getName(false)); // field.annotate(XmlTransient.class); annotate(field); return JExpr._this().ref(field); }
private ModifierGenerator(final PluginContext pluginContext, final DefinedTypeOutline classOutline, final String modifierClassName, final String modifierInterfaceName, final Collection<TypeOutline> interfaces, final String modifierMethodName, final boolean implement) throws JClassAlreadyExistsException { this.classOutline = classOutline; final JDefinedClass definedClass = classOutline.getImplClass(); this.implement = implement; this.modifierClass = definedClass._class(JMod.PUBLIC, modifierClassName, classOutline.getImplClass().getClassType()); if(interfaces != null) { for (final TypeOutline interfaceOutline : interfaces) { this.modifierClass._implements( pluginContext.ref(interfaceOutline.getImplClass(), modifierInterfaceName, true)); } } final JFieldRef cachedModifierField; if(!"java.lang.Object".equals(definedClass._extends().fullName())) { this.modifierClass._extends(pluginContext.ref(definedClass._extends(), modifierClassName, false)); cachedModifierField = JExpr.refthis(ModifierGenerator.MODIFIER_CACHE_FIELD_NAME); } else { if(implement) { cachedModifierField = JExpr._this().ref(definedClass.field(JMod.PROTECTED | JMod.TRANSIENT, this.modifierClass, ModifierGenerator.MODIFIER_CACHE_FIELD_NAME)); } else { cachedModifierField = null; } } final JDefinedClass typeDefinition = classOutline.isInterface() && ((DefinedInterfaceOutline)classOutline).getSupportInterface() != null ? ((DefinedInterfaceOutline)classOutline).getSupportInterface() : definedClass; final JMethod modifierMethod = typeDefinition.method(JMod.PUBLIC, this.modifierClass, modifierMethodName); if(this.implement) { final JConditional ifCacheNull = modifierMethod.body()._if(JExpr._null().eq(cachedModifierField)); ifCacheNull._then().assign(cachedModifierField, JExpr._new(this.modifierClass)); modifierMethod.body()._return(JExpr.cast(this.modifierClass, cachedModifierField)); } }
private void generateCollectionAccessor(final DefinedPropertyOutline fieldOutline) { final JFieldVar fieldVar = fieldOutline.getFieldVar(); if(fieldVar != null) { final JMethod modifier = this.modifierClass.method(JMod.PUBLIC, fieldVar.type(), ModifierGenerator.GETTER_PREFIX + fieldOutline.getBaseName()); if(this.implement) { final JFieldRef fieldRef = new NestedThisRef(this.classOutline.getImplClass()).ref(fieldVar); final JConditional ifNull = modifier.body()._if(fieldRef.eq(JExpr._null())); ifNull._then().assign(fieldRef, JExpr._new(this.classOutline.getImplClass().owner().ref(ArrayList.class).narrow(fieldOutline.getElementType()))); modifier.body()._return(fieldRef); } } }
private JInvocation generateImmutableListInstantiation(final PluginContext pluginContext, final JFieldRef fieldRef, final JType elementType) { if (this.overrideCollectionClass == null) { return pluginContext.unmodifiableList(fieldRef); } else { final JClass overrideCollection = pluginContext.codeModel.ref(this.overrideCollectionClass); if (overrideCollection.isAssignableFrom(pluginContext.codeModel.ref(Collection.class))) { return pluginContext.unmodifiableList(fieldRef); } else { return JExpr._new(overrideCollection.narrow(elementType)).arg(fieldRef); } } }
public void generateMetaFields() { if(this.classOutline.getSuperClass() != null) { this.selectorClass._extends(this.selectorGenerator.getInfoClass(this.classOutline.getSuperClass().implClass).selectorClass.narrow(this.rootTypeParam).narrow(this.parentTypeParam)); } for (final FieldOutline fieldOutline : this.classOutline.getDeclaredFields()) { final JFieldVar definedField = PluginUtil.getDeclaredField(fieldOutline); if (definedField != null) { final JType elementType = PluginUtil.getElementType(fieldOutline); if (elementType.isReference()) { final ClassOutline modelClass = this.selectorGenerator.getPluginContext().getClassOutline(elementType); final JClass returnType; if (modelClass != null) { returnType = this.selectorGenerator.getInfoClass(modelClass.implClass).selectorClass.narrow(this.rootTypeParam).narrow(this.selectorClass.narrow(this.rootTypeParam).narrow(this.parentTypeParam)); } else { returnType = this.selectorGenerator.getPluginContext().codeModel.ref(this.selectorGenerator.selectorBaseClass).narrow(this.rootTypeParam).narrow(this.selectorClass.narrow(this.rootTypeParam).narrow(this.parentTypeParam)); } final JFieldVar includeField = this.selectorClass.field(JMod.PRIVATE, returnType, definedField.name(), JExpr._null()); final JFieldRef fieldRef = JExpr._this().ref(includeField); final JMethod includeMethod = this.selectorClass.method(JMod.PUBLIC, returnType, definedField.name()); if(this.selectorGenerator.selectorParamName != null) { final JVar includeParam = includeMethod.param(JMod.FINAL, this.selectorGenerator.getSelectorParamType(this.classOutline.implClass, elementType), this.selectorGenerator.selectorParamName); includeMethod.body()._return(JOp.cond(fieldRef.eq(JExpr._null()), fieldRef.assign(JExpr._new(returnType).arg(JExpr._this().ref("_root")).arg(JExpr._this()).arg(JExpr.lit(definedField.name())).arg(includeParam)), fieldRef)); } else { includeMethod.body()._return(JOp.cond(fieldRef.eq(JExpr._null()), fieldRef.assign(JExpr._new(returnType).arg(JExpr._this().ref("_root")).arg(JExpr._this()).arg(JExpr.lit(definedField.name()))), fieldRef)); } this.buildChildrenMethod.body()._if(fieldRef.ne(JExpr._null()))._then().add(this.productMapVar.invoke("put").arg(JExpr.lit(definedField.name())).arg(fieldRef.invoke("init"))); } } } this.buildChildrenMethod.body()._return(this.productMapVar); }
@Override public JExpression compute(Outline outline){ JExpression expression = computeInit(outline); if((expression instanceof JFieldRef) || (expression instanceof JStringLiteral)){ setField(null); return expression; } return JExpr.ref(getField()); }
void addGetMethods(JDefinedClass jclass) { JFieldRef notFoundVar = getOrAddNotFoundVar(jclass); JMethod internalGetMethod = this.getInternalGetMethod(jclass); addPublicGetMethod(jclass, internalGetMethod, notFoundVar); }
JFieldRef getOrAddNotFoundVar(JDefinedClass jclass) { jclass.field(PROTECTED | STATIC | FINAL, Object.class, NOT_FOUND_VALUE_FIELD, _new(jclass.owner()._ref(Object.class))); return jclass.staticRef(NOT_FOUND_VALUE_FIELD); }
public HoldingContainer(MajorType t, JVar holder, JFieldRef value, JFieldRef isSet) { this(t, holder, value, isSet, false, false); }
public HoldingContainer(CompleteType t, JVar holder, JFieldRef value, JFieldRef isSet) { this(t, holder, value, isSet, false, false); }
public JFieldRef getIsSet() { Preconditions.checkNotNull(isSet, "You cannot access the isSet variable when operating on a non-nullable output value."); return isSet; }