com.sun.codemodel.JTypeVar Java Examples

The following examples show how to use com.sun.codemodel.JTypeVar. 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: CreateTraversingVisitorClass.java    From jaxb-visitor with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #2
Source File: BuilderGenerator.java    From jaxb2-rich-contract-plugin with MIT License 6 votes vote down vote up
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;
}
 
Example #3
Source File: BuilderGenerator.java    From jaxb2-rich-contract-plugin with MIT License 6 votes vote down vote up
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(JavadocUtils.hardWrapTextForJavadoc(getMessage("javadoc.method.copyTo")));
		copyToMethod.javadoc().addParam(otherParam).append(JavadocUtils.hardWrapTextForJavadoc(getMessage("javadoc.method.copyTo.param.other")));
	}
}
 
Example #4
Source File: CreateTraversingVisitorClass.java    From jaxb-visitor with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #5
Source File: CreateVisitableInterface.java    From jaxb-visitor with Apache License 2.0 6 votes vote down vote up
@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());
      }
  }
 
Example #6
Source File: CreateTraverserInterface.java    From jaxb-visitor with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example #7
Source File: AddAcceptMethod.java    From jaxb-visitor with Apache License 2.0 6 votes vote down vote up
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()));
        });
    }
 
Example #8
Source File: CreateTraverserInterface.java    From jaxb-visitor with Apache License 2.0 5 votes vote down vote up
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");
}
 
Example #9
Source File: CreateBaseVisitorClass.java    From jaxb-visitor with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #10
Source File: CreateBaseVisitorClass.java    From jaxb-visitor with Apache License 2.0 5 votes vote down vote up
@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);
      }
  }
 
Example #11
Source File: CreateVisitorInterface.java    From jaxb-visitor with Apache License 2.0 5 votes vote down vote up
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");
}
 
Example #12
Source File: CreateVisitorInterface.java    From jaxb-visitor with Apache License 2.0 5 votes vote down vote up
@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);
        }
    }
 
Example #13
Source File: SelectorGenerator.java    From jaxb2-rich-contract-plugin with MIT License 5 votes vote down vote up
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;
}
 
Example #14
Source File: BuilderGenerator.java    From jaxb2-rich-contract-plugin with MIT License 5 votes vote down vote up
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;
}
 
Example #15
Source File: JCMTypeVar.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public JCMTypeVar(JCMTypeFactory factory, JTypeVar type) {
	super(factory, type);
}
 
Example #16
Source File: SelectorGenerator.java    From jaxb2-rich-contract-plugin with MIT License 4 votes vote down vote up
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;
	}
}
 
Example #17
Source File: JDirectInnerClassRef.java    From jaxb2-rich-contract-plugin with MIT License 4 votes vote down vote up
@Override
protected JClass substituteParams(final JTypeVar[] variables, final List<JClass> bindings) {
	return this;
}
 
Example #18
Source File: BuilderGenerator.java    From jaxb2-rich-contract-plugin with MIT License 4 votes vote down vote up
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;
	}
	generateDefinedClassJavadoc();

	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();
}