Java Code Examples for com.sun.codemodel.JDefinedClass#_class

The following examples show how to use com.sun.codemodel.JDefinedClass#_class . 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: ImmutableJaxbGenerator.java    From rice with Educational Community License v2.0 6 votes vote down vote up
private void renderConstantsClass(JDefinedClass classModel) throws Exception {
	
	// define constants class
	JDefinedClass constantsClass = classModel._class(JMod.STATIC, Util.CONSTANTS_CLASS_NAME);
	
	// generate the javadoc on the top of the Constants class
	JDocComment javadoc = constantsClass.javadoc();
	javadoc.append(Util.CONSTANTS_CLASS_JAVADOC);
	
	// render root element name
	JFieldVar rootElementField = constantsClass.field(JMod.FINAL | JMod.STATIC, String.class, Util.ROOT_ELEMENT_NAME_FIELD);
	rootElementField.init(JExpr.lit(Util.toLowerCaseFirstLetter(classModel.name())));
	
	// render type name
	JFieldVar typeNameField = constantsClass.field(JMod.FINAL | JMod.STATIC, String.class, Util.TYPE_NAME_FIELD);
	typeNameField.init(JExpr.lit(classModel.name() + Util.TYPE_NAME_SUFFIX));
          
}
 
Example 2
Source File: ImmutableJaxbGenerator.java    From rice with Educational Community License v2.0 6 votes vote down vote up
private void renderElementsClass(JDefinedClass classModel, List<FieldModel> fields) throws Exception {
	
	// define constants class
	JDefinedClass elementsClass = classModel._class(JMod.STATIC, Util.ELEMENTS_CLASS_NAME);
	
	// generate the javadoc on the top of the Elements class
	JDocComment javadoc = elementsClass.javadoc();
	javadoc.append(Util.ELEMENTS_CLASS_JAVADOC);
	
	// go through each field and create a corresponding constant
	for (FieldModel fieldModel : fields) {
		if (Util.isCommonElement(fieldModel.fieldName)) {
			continue;
		}
		JFieldVar elementFieldVar = elementsClass.field(JMod.FINAL | JMod.STATIC, String.class, Util.toConstantsVariable(fieldModel.fieldName));
		elementFieldVar.init(JExpr.lit(fieldModel.fieldName));
	}
}
 
Example 3
Source File: PluginImpl.java    From immutable-xjc with MIT License 6 votes vote down vote up
private JDefinedClass generateBuilderClass(JDefinedClass clazz) {
    JDefinedClass builderClass = null;
    String builderClassName = getBuilderClassName(clazz);
    try {
        builderClass = clazz._class(JMod.PUBLIC | JMod.STATIC, builderClassName);
        if (builderInheritance) {
            for (JClass superClass = clazz._extends(); superClass != null; superClass = superClass._extends()) {
                JClass superClassBuilderClass = getBuilderClass(superClass);
                if (superClassBuilderClass != null) {
                    builderClass._extends(superClassBuilderClass);
                    break;
                }
            }
        }
    } catch (JClassAlreadyExistsException e) {
        this.log(Level.WARNING, "builderClassExists", builderClassName);
    }
    return builderClass;
}
 
Example 4
Source File: ClassGenerator.java    From Bats with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
ClassGenerator(CodeGenerator<T> codeGenerator, MappingSet mappingSet, SignatureHolder signature,
               EvaluationVisitor eval, JDefinedClass clazz, JCodeModel model,
               OptionSet optionManager) throws JClassAlreadyExistsException {
  this.codeGenerator = codeGenerator;
  this.clazz = clazz;
  this.mappings = mappingSet;
  this.sig = signature;
  this.evaluationVisitor = eval;
  this.model = model;
  this.optionManager = optionManager;
  constantVars = new HashMap<>();

  blocks = (LinkedList<SizedJBlock>[]) new LinkedList[sig.size()];
  for (int i =0; i < sig.size(); i++) {
    blocks[i] = Lists.newLinkedList();
  }
  rotateBlock();

  for (SignatureHolder child : signature.getChildHolders()) {
    Class<?> innerClass = child.getSignatureClass();
    String innerClassName = innerClass.getSimpleName();

    // Create the inner class as private final. If the template (super) class
    // is static, then make the subclass static as well. Note the conversion
    // from the JDK Modifier values to the JCodeModel JMod values: the
    // values are different.

    int mods = JMod.FINAL;
    if ((innerClass.getModifiers() & Modifier.STATIC) != 0) {
      mods += JMod.STATIC;
    }
    JDefinedClass innerClazz = clazz._class(mods, innerClassName);
    innerClasses.put(innerClassName, new ClassGenerator<>(codeGenerator, mappingSet, child, eval, innerClazz, model, optionManager));
  }
  long maxExprsNumber = optionManager != null ? optionManager.getOption(ExecConstants.CODE_GEN_EXP_IN_METHOD_SIZE_VALIDATOR) : 50;
  maxIndex = Math.round((0xFFFF / (1 + 3. / (3 * sig.size() + maxExprsNumber)) - 1000) / 3);
}
 
Example 5
Source File: ModifierGenerator.java    From jaxb2-rich-contract-plugin with MIT License 5 votes vote down vote up
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));
	}

}
 
Example 6
Source File: ImmutableJaxbGenerator.java    From rice with Educational Community License v2.0 5 votes vote down vote up
private void renderBuilderClass(JDefinedClass classModel, List<FieldModel> fields, Class<?> contractInterface) throws Exception {
	
	// define constants class
	JDefinedClass builderClass = classModel._class(JMod.PUBLIC | JMod.STATIC | JMod.FINAL, Util.BUILDER_CLASS_NAME);
	
	// create a literal version of the Builder class so that the code generator won't pre-pend Builder class references with outermost class
	JClass literalBuilderClass = codeModel.ref("Builder");
	
	// generate the javadoc on the top of the Elements class
	JDocComment javadoc = builderClass.javadoc();
	javadoc.append(Util.generateBuilderJavadoc(classModel.name(), contractInterface.getSimpleName()));
	
	builderClass._implements(contractInterface);
	builderClass._implements(ModelBuilder.class);
	builderClass._implements(Serializable.class);
	
	// render the builder fields
	for (FieldModel fieldModel : fields) {
		builderClass.field(JMod.PRIVATE, fieldModel.fieldType, fieldModel.fieldName);
	}
	
	// render default empty constructor for builder
	JMethod constructor = builderClass.constructor(JMod.PRIVATE);
	constructor.body().directStatement("// TODO modify this constructor as needed to pass any required values and invoke the appropriate 'setter' methods");

	renderBuilderDefaultCreate(builderClass, literalBuilderClass);
	renderBuilderCreateContract(builderClass, literalBuilderClass, fields, contractInterface);
	renderBuild(builderClass);
	renderGetters(builderClass, fields);
	renderSetters(builderClass, fields);
}
 
Example 7
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;
	}
}