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; }
JMethod generateNewCopyBuilderMethod(final boolean partial) { final JDefinedClass typeDefinition = this.typeOutline.isInterface() && ((DefinedInterfaceOutline)this.typeOutline).getSupportInterface() != null ? ((DefinedInterfaceOutline)this.typeOutline).getSupportInterface() : this.definedClass; final int mods = this.implement ? this.definedClass.isAbstract() ? JMod.PUBLIC | JMod.ABSTRACT : JMod.PUBLIC : JMod.NONE; final JMethod copyBuilderMethod = typeDefinition.method(mods, this.builderClass.raw, this.settings.getNewCopyBuilderMethodName()); final JTypeVar copyBuilderMethodTypeParam = copyBuilderMethod.generify(BuilderGenerator.PARENT_BUILDER_TYPE_PARAMETER_NAME); final JVar parentBuilderParam = copyBuilderMethod.param(JMod.FINAL, copyBuilderMethodTypeParam, BuilderGenerator.PARENT_BUILDER_PARAM_NAME); final CopyGenerator copyGenerator = this.pluginContext.createCopyGenerator(copyBuilderMethod, partial); copyBuilderMethod.type(this.builderClass.raw.narrow(copyBuilderMethodTypeParam)); final JMethod copyBuilderConvenienceMethod = typeDefinition.method(mods, this.builderClass.raw.narrow(this.pluginContext.voidClass), this.settings.getNewCopyBuilderMethodName()); final CopyGenerator copyConvenienceGenerator = this.pluginContext.createCopyGenerator(copyBuilderConvenienceMethod, partial); if (this.implement && !this.definedClass.isAbstract()) { copyBuilderMethod.body()._return(copyGenerator.generatePartialArgs(this.pluginContext._new((JClass)copyBuilderMethod.type()).arg(parentBuilderParam).arg(JExpr._this()).arg(JExpr.TRUE))); copyBuilderConvenienceMethod.body()._return(copyConvenienceGenerator.generatePartialArgs(this.pluginContext.invoke(this.settings.getNewCopyBuilderMethodName()).arg(JExpr._null()))); } if (this.typeOutline.getSuperClass() != null) { copyBuilderMethod.annotate(Override.class); copyBuilderConvenienceMethod.annotate(Override.class); } return copyBuilderMethod; }
final void generateCopyToMethod(final boolean partial) { if (this.implement) { final JDefinedClass typeDefinition = this.typeOutline.isInterface() && ((DefinedInterfaceOutline)this.typeOutline).getSupportInterface() != null ? ((DefinedInterfaceOutline)this.typeOutline).getSupportInterface() : this.definedClass; final JMethod copyToMethod = typeDefinition.method(JMod.PUBLIC, this.pluginContext.voidType, this.settings.getCopyToMethodName()); final JTypeVar typeVar = copyToMethod.generify(BuilderGenerator.PARENT_BUILDER_TYPE_PARAMETER_NAME); final JVar otherParam = copyToMethod.param(JMod.FINAL, this.builderClass.raw.narrow(typeVar), BuilderGenerator.OTHER_PARAM_NAME); final CopyGenerator cloneGenerator = this.pluginContext.createCopyGenerator(copyToMethod, partial); final JBlock body = copyToMethod.body(); final JVar otherRef; if (this.typeOutline.getSuperClass() != null) { body.add(cloneGenerator.generatePartialArgs(this.pluginContext.invoke(JExpr._super(), copyToMethod.name()).arg(otherParam))); } otherRef = otherParam; generateFieldCopyExpressions(cloneGenerator, body, otherRef, JExpr._this()); copyToMethod.javadoc().append(getMessage("javadoc.method.copyTo")); copyToMethod.javadoc().addParam(otherParam).append(getMessage("javadoc.method.copyTo.param.other")); } }
@Override protected void run(Set<ClassOutline> classes, Set<JClass> directClasses) { JDefinedClass scratch = getOutline().getClassFactory().createInterface(getPackage(), "_scratch", null); try { JDefinedClass _interface = getOutline().getClassFactory().createInterface(getPackage(), "Traverser", null); setOutput(_interface); final JTypeVar retType = scratch.generify("?"); final JTypeVar exceptionType = _interface.generify("E", Throwable.class); final JClass narrowedVisitor = visitor.narrow(retType).narrow(exceptionType); for (JClass jc : allConcreteClasses(classes, directClasses)) { implTraverse(exceptionType, narrowedVisitor, jc); } } finally { jpackage.remove(scratch); } }
public void run(Set<ClassOutline> sorted, JDefinedClass visitor) { // skip over abstract classes // add the accept method to the bean sorted.stream().filter(classOutline -> !classOutline.target.isAbstract()).forEach(classOutline -> { // add the accept method to the bean JDefinedClass beanImpl = classOutline.implClass; final JMethod acceptMethod = beanImpl.method(JMod.PUBLIC, void.class, "accept"); final JTypeVar returnType = acceptMethod.generify("R"); final JTypeVar exceptionType = acceptMethod.generify("E", Throwable.class); acceptMethod.type(returnType); acceptMethod._throws(exceptionType); final JClass narrowedVisitor = visitor.narrow(returnType, exceptionType); JVar vizParam = acceptMethod.param(narrowedVisitor, "aVisitor"); JBlock block = acceptMethod.body(); String methodName = visitMethodNamer.apply(beanImpl.name()); block._return(vizParam.invoke(methodName).arg(JExpr._this())); }); }
@Override protected void run(Set<ClassOutline> classes, Set<JClass> directClasses) { final JDefinedClass _interface = outline.getClassFactory().createInterface(jpackage, "Visitable", null); setOutput( _interface ); final JMethod _method = getOutput().method(JMod.NONE, void.class, "accept"); final JTypeVar returnType = _method.generify("R"); final JTypeVar exceptionType = _method.generify("E", Throwable.class); _method.type(returnType); _method._throws(exceptionType); final JClass narrowedVisitor = visitor.narrow(returnType, exceptionType); _method.param(narrowedVisitor, "aVisitor"); for(ClassOutline classOutline : classes) { classOutline.implClass._implements(getOutput()); } }
private void generateForDirectClass(JDefinedClass traversingVisitor, JTypeVar returnType, JTypeVar exceptionType, JClass implClass) { // add method impl to traversing visitor JMethod travViz; String visitMethodName = visitMethodNamer.apply(implClass.name()); travViz = traversingVisitor.method(JMod.PUBLIC, returnType, visitMethodName); travViz._throws(exceptionType); JVar beanVar = travViz.param(implClass, "aBean"); travViz.annotate(Override.class); JBlock travVizBloc = travViz.body(); addTraverseBlock(travViz, beanVar, true); JVar retVal = travVizBloc.decl(returnType, "returnVal"); travVizBloc.assign(retVal, JExpr.invoke(JExpr.invoke("getVisitor"), visitMethodName).arg(beanVar)); travVizBloc._if(JExpr.ref("progressMonitor").ne(JExpr._null()))._then().invoke(JExpr.ref("progressMonitor"), "visited").arg(beanVar); addTraverseBlock(travViz, beanVar, false); travVizBloc._return(retVal); }
private void generate(JDefinedClass traversingVisitor, JTypeVar returnType, JTypeVar exceptionType, JClass implClass) { // add method impl to traversing visitor JMethod travViz; travViz = traversingVisitor.method(JMod.PUBLIC, returnType, visitMethodNamer.apply(implClass.name())); travViz._throws(exceptionType); JVar beanVar = travViz.param(implClass, "aBean"); travViz.annotate(Override.class); JBlock travVizBloc = travViz.body(); addTraverseBlock(travViz, beanVar, true); JVar retVal = travVizBloc.decl(returnType, "returnVal"); travVizBloc.assign(retVal, JExpr.invoke(beanVar, "accept").arg(JExpr.invoke("getVisitor"))); travVizBloc._if(JExpr.ref("progressMonitor").ne(JExpr._null()))._then().invoke(JExpr.ref("progressMonitor"), "visited").arg(beanVar); // case to traverse after the visit addTraverseBlock(travViz, beanVar, false); travVizBloc._return(retVal); }
/** * Generates a delegator for the given class. * * @param delegateClass * delegate class */ public void generateDelegator(JDefinedClass delegateClass) { JPackage pkg = context.getDelegatorPackage(); try { JDefinedClass delegator = pkg ._class(delegateClass.name() + context.getConfig().getDelegatorSuffix()); context.annotateAsGenerated(delegator); JClass delegateType = delegateClass; if (delegateClass.typeParams().length > 0) { Stream.of(delegateClass.typeParams()).map(JTypeVar::name) .forEach(delegator::generify); delegateType = delegateClass.narrow(delegateClass.typeParams()); } JFieldVar delegate = delegator.field(JMod.PROTECTED, delegateType, context.getConfig().getDelegateFieldName()); delegate.init(JExpr._new(delegateType)); JDefinedClass klass = delegateClass; while (klass != null) { buildDelegatingMethods(klass, delegator, delegate); if (klass._extends() instanceof JDefinedClass) { klass = (JDefinedClass) klass._extends(); } else { klass = null; } } } catch (JClassAlreadyExistsException exc) { throw Exceptions.unchecked(exc); } }
protected void expectClass(String className, String... typeParams) { expectClass(className); JTypeVar[] typeVars = klass.typeParams(); assertThat(typeVars.length).isEqualTo(typeParams.length); for (int i = 0; i < typeVars.length; i++) { assertThat(typeVars[i].name()).isEqualTo(typeParams[i]); } }
@Override public JClass resolveType(String shortName) { Type t = types.get(shortName); if (t != null) { return t.definition; } JTypeVar jtv = generics.get(shortName); if (jtv != null) { return jtv; } return parent().resolveType(shortName); }
void addGeneric(String g, JTypeVar var) { if (current instanceof Scope.Generic) { Scope.Generic genericScope = (Scope.Generic) current; genericScope.addGeneric(g, var); } else { Logger.error("Cannot add generic (" + g + ") to a Scope (" + current.getClass().getName() + ") that doesn't allow generics!"); throw new RuntimeException("Invalid Scope for operation!"); } }
JMethod generateCopyOfMethod(final TypeOutline paramType, final boolean partial) { if (paramType.getSuperClass() != null) { generateCopyOfMethod(paramType.getSuperClass(), partial); } final JMethod copyOfMethod = this.definedClass.method(JMod.PUBLIC | JMod.STATIC, this.builderClass.raw.narrow(Void.class), this.pluginContext.buildCopyMethodName); final JTypeVar copyOfMethodTypeParam = copyOfMethod.generify(BuilderGenerator.PARENT_BUILDER_TYPE_PARAMETER_NAME); copyOfMethod.type(this.builderClass.raw.narrow(copyOfMethodTypeParam)); final JVar otherParam = copyOfMethod.param(JMod.FINAL, paramType.getImplClass(), BuilderGenerator.OTHER_PARAM_NAME); final CopyGenerator copyGenerator = this.pluginContext.createCopyGenerator(copyOfMethod, partial); final JVar newBuilderVar = copyOfMethod.body().decl(JMod.FINAL, copyOfMethod.type(), BuilderGenerator.NEW_BUILDER_VAR_NAME, JExpr._new(copyOfMethod.type()).arg(JExpr._null()).arg(JExpr._null()).arg(JExpr.FALSE)); copyOfMethod.body().add(copyGenerator.generatePartialArgs(this.pluginContext.invoke(otherParam, this.settings.getCopyToMethodName()).arg(newBuilderVar))); copyOfMethod.body()._return(newBuilderVar); return copyOfMethod; }
MetaInfoOutline(final SelectorGenerator selectorGenerator, final ClassOutline classOutline, final JDefinedClass selectorClass, final JTypeVar rootTypeParam, final JTypeVar parentTypeParam, final JMethod buildChildrenMethod, final JVar productMapVar) { this.selectorGenerator = selectorGenerator; this.classOutline = classOutline; this.selectorClass = selectorClass; this.rootTypeParam = rootTypeParam; this.parentTypeParam = parentTypeParam; this.buildChildrenMethod = buildChildrenMethod; this.buildChildrenMethod.body().add(productMapVar.invoke("putAll").arg(JExpr._super().invoke(buildChildrenMethod))); this.productMapVar = productMapVar; }
private void implTraverse(JTypeVar exceptionType, JClass narrowedVisitor, JClass implClass) { JMethod traverseMethod; String methodName = traverseMethodNamer.apply(implClass.name()); traverseMethod = getOutput().method(JMod.NONE, void.class, methodName); traverseMethod._throws(exceptionType); traverseMethod.param(implClass, "aBean"); traverseMethod.param(narrowedVisitor, "aVisitor"); }
@Override protected void run(Set<ClassOutline> classes, Set<JClass> directClasses) { final JDefinedClass _interface = outline.getClassFactory().createInterface(jpackage, "Visitor", null); final JTypeVar returnType = _interface.generify("R"); final JTypeVar exceptionType = _interface.generify("E", Throwable.class); setOutput( _interface ); for(JClass jc : allConcreteClasses(classes, directClasses)) { // System.out.println("seeing class:" + jc.name()); declareVisitMethod(returnType, exceptionType, jc); } }
private void declareVisitMethod(JTypeVar returnType, JTypeVar exceptionType, JClass implClass) { JMethod vizMethod; String visitMethod = visitMethodNamer.apply(implClass.name()); vizMethod = getOutput().method(JMod.NONE, returnType, visitMethod); vizMethod._throws(exceptionType); vizMethod.param(implClass, "aBean"); }
@Override protected void run(Set<ClassOutline> classes, Set<JClass> directClasses) { JDefinedClass _class = getOutline().getClassFactory().createClass(getPackage(), "BaseVisitor", null); setOutput(_class); final JTypeVar returnType = _class.generify("R"); final JTypeVar exceptionType = _class.generify("E", Throwable.class); final JClass narrowedVisitor = visitor.narrow(returnType, exceptionType); getOutput()._implements(narrowedVisitor); for(JClass jc : allConcreteClasses(classes, directClasses)) { implementVisitMethod(returnType, exceptionType, jc); } }
private void implementVisitMethod(JTypeVar returnType, JTypeVar exceptionType, JClass implClass) { JMethod _method; String methodName = visitMethodNamer.apply(implClass.name()); _method = getOutput().method(JMod.PUBLIC, returnType, methodName); _method._throws(exceptionType); _method.param(implClass, "aBean"); _method.body()._return(JExpr._null()); _method.annotate(Override.class); }
@Override public void addGeneric(String g, JTypeVar var) { generics.put(g, var); }
public JCMTypeVar(JCMTypeFactory factory, JTypeVar type) { super(factory, type); }
public void buildProperties() throws SAXException { final TypeOutline superClass = this.typeOutline.getSuperClass(); final JMethod initMethod; final JVar productParam; final JBlock initBody; if (this.implement) { initMethod = this.builderClass.raw.method(JMod.PROTECTED, this.definedClass, PluginContext.INIT_METHOD_NAME); final JTypeVar typeVar = initMethod.generify(BuilderGenerator.PRODUCT_TYPE_PARAMETER_NAME, this.definedClass); initMethod.type(typeVar); productParam = initMethod.param(JMod.FINAL, typeVar, BuilderGenerator.PRODUCT_VAR_NAME); initBody = initMethod.body(); } else { initMethod = null; initBody = null; productParam = null; } if (this.typeOutline.getDeclaredFields() != null) { for (final PropertyOutline fieldOutline : this.typeOutline.getDeclaredFields()) { if (fieldOutline.hasGetter()) { generateBuilderMember(fieldOutline, initBody, productParam); } } } if (superClass != null) { generateExtendsClause(getBuilderDeclaration(superClass.getImplClass())); if (this.implement) initBody._return(JExpr._super().invoke(initMethod).arg(productParam)); generateBuilderMemberOverrides(superClass); } else if (this.implement) { initBody._return(productParam); } generateImplementsClause(); generateBuildMethod(initMethod); generateCopyToMethod(false); generateNewCopyBuilderMethod(false); if (this.implement && !this.definedClass.isAbstract()) { generateNewBuilderMethod(); generateCopyOfMethod(this.typeOutline, false); } if (this.settings.isGeneratingPartialCopy()) { generateCopyToMethod(true); generateNewCopyBuilderMethod(true); if (this.implement && !this.definedClass.isAbstract()) { final JMethod partialCopyOfMethod = generateCopyOfMethod(this.typeOutline, true); generateConveniencePartialCopyMethod(this.typeOutline, partialCopyOfMethod, this.pluginContext.copyExceptMethodName, this.pluginContext.excludeConst); generateConveniencePartialCopyMethod(this.typeOutline, partialCopyOfMethod, this.pluginContext.copyOnlyMethodName, this.pluginContext.includeConst); } } generateCopyOfBuilderMethods(); }
@Override protected JClass substituteParams(final JTypeVar[] variables, final List<JClass> bindings) { return this; }
private MetaInfoOutline generateMetaClass(final ClassOutline classOutline) { try { final JDefinedClass definedClass = classOutline.implClass; final JDefinedClass selectorClass = definedClass._class(JMod.PUBLIC | JMod.STATIC, this.selectorClassName); final JTypeVar rootTypeParam = selectorClass.generify("TRoot"); final JTypeVar parentTypeParam = selectorClass.generify("TParent"); rootTypeParam.bound(this.pluginContext.codeModel.ref(this.selectorBaseClass).narrow(rootTypeParam, this.pluginContext.codeModel.wildcard())); //parentTypeParam.bound(this.apiConstructs.codeModel.ref(Selector.class).narrow(parentTypeParam, this.apiConstructs.codeModel.wildcard())); final JMethod constructor = selectorClass.constructor(JMod.PUBLIC); final JVar rootParam = constructor.param(JMod.FINAL, rootTypeParam, "root"); final JVar parentParam = constructor.param(JMod.FINAL, parentTypeParam, "parent"); final JVar propertyNameParam = constructor.param(JMod.FINAL, this.pluginContext.stringClass, "propertyName"); if(this.selectorParamName != null) { final JVar includeParam = constructor.param(JMod.FINAL, getSelectorParamType(this.pluginContext.codeModel.wildcard(), definedClass.wildcard()), this.selectorParamName); constructor.body().invoke("super").arg(rootParam).arg(parentParam).arg(propertyNameParam).arg(includeParam); } else { constructor.body().invoke("super").arg(rootParam).arg(parentParam).arg(propertyNameParam); } final JClass productMapType = this.pluginContext.codeModel.ref(Map.class).narrow(String.class).narrow(this.propertyPathClass); final JMethod buildChildrenMethod = selectorClass.method(JMod.PUBLIC, productMapType, "buildChildren"); buildChildrenMethod.annotate(Override.class); final JVar productMapVar = buildChildrenMethod.body().decl(JMod.FINAL, productMapType, "products", JExpr._new(this.pluginContext.codeModel.ref(HashMap.class).narrow(String.class).narrow(this.propertyPathClass))); if(classOutline.getSuperClass() == null ) { selectorClass._extends(this.pluginContext.codeModel.ref(this.selectorBaseClass).narrow(rootTypeParam).narrow(parentTypeParam)); } final JDefinedClass rootSelectorClass = definedClass._class(JMod.PUBLIC | JMod.STATIC, this.rootSelectorClassName); rootSelectorClass._extends(selectorClass.narrow(rootSelectorClass).narrow(Void.class)); final JMethod rootSelectorConstructor = rootSelectorClass.constructor(JMod.NONE); if(this.selectorParamName != null) { final JVar rootSelectorClassIncludeParam = rootSelectorConstructor.param(JMod.FINAL, getSelectorParamType(this.pluginContext.voidClass, definedClass), this.selectorParamName); rootSelectorConstructor.body().invoke("super").arg(JExpr._null()).arg(JExpr._null()).arg(JExpr._null()).arg(rootSelectorClassIncludeParam); } else { rootSelectorConstructor.body().invoke("super").arg(JExpr._null()).arg(JExpr._null()).arg(JExpr._null()); } final JMethod rootMethod = rootSelectorClass.method(JMod.STATIC | JMod.PUBLIC, rootSelectorClass, "_root"); if(this.selectorParamName != null) { final JVar rootIncludeParam = rootMethod.param(JMod.FINAL, getSelectorParamType(this.pluginContext.voidClass, definedClass), this.selectorParamName); rootMethod.body()._return(JExpr._new(rootSelectorClass).arg(rootIncludeParam)); } else { rootMethod.body()._return(JExpr._new(rootSelectorClass)); } return new MetaInfoOutline(this, classOutline, selectorClass, rootTypeParam, parentTypeParam, buildChildrenMethod, productMapVar); } catch (final JClassAlreadyExistsException e) { SelectorGenerator.LOGGER.warning("Attempt to generate already existing class"); return null; } }
@Override protected JClass substituteParams(JTypeVar[] variables, List<JClass> bindings) { return null; }
JStaticClass() { super(pkg.owner()); // TODO: allow those to be specified typeParams = new JTypeVar[0]; }
public JTypeVar[] typeParams() { return typeParams; }
protected JClass substituteParams(JTypeVar[] variables, List<JClass> bindings) { return this; }
public void addGeneric(String g, JTypeVar var);