Java Code Examples for com.sun.codemodel.JType#boxify()

The following examples show how to use com.sun.codemodel.JType#boxify() . 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: CodeModelHelper.java    From springmvc-raml-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Searches inside a JCodeModel for a class with a specified name ignoring
 * package
 *
 * @param codeModels
 *            The codemodels which we will look inside
 * @param simpleClassName
 *            The class name to search for
 * @return the first class in any package that matches the simple class
 *         name.
 */
public static JClass findFirstClassBySimpleName(JCodeModel[] codeModels, String simpleClassName) {
	if (codeModels != null && codeModels.length > 0) {
		for (JCodeModel codeModel : codeModels) {
			Iterator<JPackage> packages = codeModel.packages();
			while (packages.hasNext()) {
				JPackage jPackage = packages.next();
				Iterator<JDefinedClass> classes = jPackage.classes();
				while (classes.hasNext()) {
					JDefinedClass aClass = classes.next();
					if (aClass.name().equals(simpleClassName)) {
						return aClass;
					}
				}
			}
		}
		// Is this a simple type?
		JType parseType;
		try {
			parseType = codeModels[0].parseType(simpleClassName);
			if (parseType != null) {
				if (parseType.isPrimitive()) {
					return parseType.boxify();
				} else if (parseType instanceof JClass) {
					return (JClass) parseType;
				}
			}
		} catch (ClassNotFoundException e) {
			; // Do nothing we will throw an exception further down
		}

		JClass boxedPrimitive = codeModels[0].ref("java.lang." + simpleClassName);
		if (boxedPrimitive != null) {
			return boxedPrimitive;
		}

		throw new InvalidCodeModelException("No unique class found for simple class name " + simpleClassName);
	}

	throw new InvalidCodeModelException("No code models provided for " + simpleClassName);

}
 
Example 2
Source File: BuilderGenerator.java    From jaxb2-rich-contract-plugin with MIT License 4 votes vote down vote up
private void generateAddMethods(final PropertyOutline propertyOutline,
                                final QName elementName, final JType jType,
								final String schemaAnnotation) {
	final JClass elementType = jType.boxify();
	final JClass iterableType = this.pluginContext.iterableClass.narrow(elementType.wildcard());
	final String fieldName = this.pluginContext.toVariableName(elementName.getLocalPart());
	final String propertyName = this.pluginContext.toPropertyName(elementName.getLocalPart());
	final JMethod addIterableMethod = this.builderClass.raw.method(JMod.PUBLIC, this.builderClass.type, PluginContext.ADD_METHOD_PREFIX + propertyName);
	final JVar addIterableParam = addIterableMethod.param(JMod.FINAL, iterableType, fieldName + "_");
	generateAddMethodJavadoc(addIterableMethod, addIterableParam, schemaAnnotation);
	final JMethod addVarargsMethod = this.builderClass.raw.method(JMod.PUBLIC, this.builderClass.type, PluginContext.ADD_METHOD_PREFIX + propertyName);
	final JVar addVarargsParam = addVarargsMethod.varParam(elementType, fieldName + "_");
	generateAddMethodJavadoc(addVarargsMethod, addVarargsParam, schemaAnnotation);
	final BuilderOutline childBuilderOutline = getBuilderDeclaration(elementType);
	final JMethod addMethod;
	if (childBuilderOutline != null && !childBuilderOutline.getClassOutline().getImplClass().isAbstract()) {
		final JClass builderWithMethodReturnType = childBuilderOutline.getBuilderClass().narrow(this.builderClass.type.wildcard());
		addMethod = this.builderClass.raw.method(JMod.PUBLIC, builderWithMethodReturnType, PluginContext.ADD_METHOD_PREFIX + propertyName);
		generateBuilderMethodJavadoc(addMethod, "add", fieldName, schemaAnnotation);
	} else {
		addMethod = null;
	}
	if (this.implement) {
		final BuilderOutline choiceChildBuilderOutline = getBuilderDeclaration(propertyOutline.getElementType());
		final JClass childBuilderType = childBuilderOutline == null ? this.pluginContext.buildableInterface : childBuilderOutline.getBuilderClass().narrow(this.builderClass.type);
		final JClass builderFieldElementType = choiceChildBuilderOutline == null ? this.pluginContext.buildableInterface : choiceChildBuilderOutline.getBuilderClass().narrow(this.builderClass.type);
		final JClass builderArrayListClass = this.pluginContext.arrayListClass.narrow(builderFieldElementType);
		final JFieldVar builderField = this.builderClass.raw.fields().get(propertyOutline.getFieldName());
		addVarargsMethod.body()._return(JExpr.invoke(addIterableMethod).arg(this.pluginContext.asList(addVarargsParam)));
		if (addMethod == null) {
			addIterableMethod.body()._return(JExpr.invoke(PluginContext.ADD_METHOD_PREFIX + propertyOutline.getBaseName()).arg(addIterableParam));
		} else {
			final JConditional addIterableIfParamNull = addIterableMethod.body()._if(addIterableParam.ne(JExpr._null()));
			final JConditional addIterableIfNull = addIterableIfParamNull._then()._if(JExpr._this().ref(builderField).eq(JExpr._null()));
			addIterableIfNull._then().assign(JExpr._this().ref(builderField), JExpr._new(builderArrayListClass));
			final JForEach addIterableForEach = addIterableIfParamNull._then().forEach(elementType, BuilderGenerator.ITEM_VAR_NAME, addIterableParam);
			final JExpression builderCreationExpression = JExpr._new(childBuilderType).arg(JExpr._this()).arg(addIterableForEach.var()).arg(this.settings.isCopyAlways() ? JExpr.TRUE : JExpr.FALSE);
			addIterableForEach.body().add(JExpr._this().ref(builderField).invoke("add").arg(builderCreationExpression));
			addIterableMethod.body()._return(JExpr._this());

			final JConditional addIfNull = addMethod.body()._if(JExpr._this().ref(builderField).eq(JExpr._null()));
			addIfNull._then().assign(JExpr._this().ref(builderField), JExpr._new(builderArrayListClass));
			final JVar childBuilderVar = addMethod.body().decl(JMod.FINAL, childBuilderType, fieldName + this.settings.getBuilderFieldSuffix(), JExpr._new(childBuilderType).arg(JExpr._this()).arg(JExpr._null()).arg(JExpr.FALSE));
			addMethod.body().add(JExpr._this().ref(builderField).invoke("add").arg(childBuilderVar));
			addMethod.body()._return(childBuilderVar);
		}
	}
}