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

The following examples show how to use com.sun.codemodel.JType#isPrimitive() . 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: JAXBElementCodeGenerator.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void append(JBlock block, String propertyName,
		String propertyMethod, JType propertyType,
		Collection<JType> possiblePropertyTypes, A arguments) {
	block = block.block();

	final JType declarablePropertyType = getTypeFactory().create(
			propertyType).getDeclarableType();

	// We assume that primitive properties are always set
	boolean isAlwaysSet = propertyType.isPrimitive();
	getCodeGenerator().generate(
			block,
			propertyType,
			possiblePropertyTypes,
			isAlwaysSet,

			arguments.property(block, propertyName, propertyMethod,
					declarablePropertyType, declarablePropertyType,
					possiblePropertyTypes));
}
 
Example 2
Source File: EqualsArguments.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public EqualsArguments property(JBlock block, String propertyName,
		String propertyMethod, JType declarablePropertyType,
		JType propertyType, Collection<JType> possiblePropertyTypes) {
	final JVar leftPropertyValue = block.decl(JMod.FINAL,
			declarablePropertyType, leftValue().name() + propertyName,
			leftValue().invoke(propertyMethod));
	final JVar rightPropertyValue = block.decl(JMod.FINAL,
			declarablePropertyType, rightValue().name() + propertyName,
			rightValue().invoke(propertyMethod));
	// We assume that primitive properties are always set
	boolean isAlwaysSet = propertyType.isPrimitive();
	final JExpression leftPropertyHasSetValue = isAlwaysSet ? JExpr.TRUE
			: leftPropertyValue.ne(JExpr._null());
	final JExpression rightPropertyHasSetValue = isAlwaysSet ? JExpr.TRUE
			: rightPropertyValue.ne(JExpr._null());
	return spawn(leftPropertyValue, leftPropertyHasSetValue,
			rightPropertyValue, rightPropertyHasSetValue);
}
 
Example 3
Source File: EqualsArguments.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public EqualsArguments element(JBlock subBlock, JType elementType) {
	final JVar leftElementValue = subBlock.decl(JMod.FINAL, elementType,
			leftValue().name() + "Element", leftValue().invoke("next"));
	final JVar rightElementValue = subBlock.decl(JMod.FINAL, elementType,
			rightValue().name() + "Element", rightValue().invoke("next"));
	// if (!(o1==null ? o2==null : o1.equals(o2)))
	// return false;
	final boolean isElementAlwaysSet = elementType.isPrimitive();
	final JExpression leftElementHasSetValue = isElementAlwaysSet ? JExpr.TRUE
			: leftElementValue.ne(JExpr._null());
	final JExpression rightElementHasSetValue = isElementAlwaysSet ? JExpr.TRUE
			: rightElementValue.ne(JExpr._null());
	return spawn(leftElementValue, leftElementHasSetValue,
			rightElementValue, rightElementHasSetValue);

}
 
Example 4
Source File: BuilderGenerator.java    From jaxb2-rich-contract-plugin with MIT License 6 votes vote down vote up
private BuilderOutline getReferencedBuilderOutline(final JType type) {
	BuilderOutline builderOutline = null;
	if (this.pluginContext.getClassOutline(type) == null && this.pluginContext.getEnumOutline(type) == null && type.isReference() && !type.isPrimitive() && !type.isArray() && type.fullName().contains(".")) {
		final Class<?> runtimeParentClass;
		try {
			runtimeParentClass = Class.forName(type.binaryName());
		} catch (final ClassNotFoundException e) {
			return null;
		}
		final JClass builderClass = reflectRuntimeInnerClass(runtimeParentClass, this.settings.getBuilderClassName());
		if (builderClass != null) {
			final ReferencedClassOutline referencedClassOutline = new ReferencedClassOutline(this.pluginContext.codeModel, runtimeParentClass);
			builderOutline = new BuilderOutline(referencedClassOutline, builderClass);
		}
	}
	return builderOutline;
}
 
Example 5
Source File: PluginImpl.java    From immutable-xjc with MIT License 6 votes vote down vote up
private JExpression defaultValue(JFieldVar field) {
    JType javaType = field.type();
    if (setDefaultValuesInConstructor) {
        Optional<JAnnotationUse> xmlElementAnnotation = getAnnotation(field.annotations(), javax.xml.bind.annotation.XmlElement.class.getCanonicalName());
        if (xmlElementAnnotation.isPresent()) {
            JAnnotationValue annotationValue = xmlElementAnnotation.get().getAnnotationMembers().get("defaultValue");
            if (annotationValue != null) {
                StringWriter sw = new StringWriter();
                JFormatter f = new JFormatter(sw);
                annotationValue.generate(f);
                return JExpr.lit(sw.toString().replaceAll("\"", ""));
            }
        }
    }
    if (javaType.isPrimitive()) {
        if (field.type().owner().BOOLEAN.equals(javaType)) {
            return JExpr.lit(false);
        } else if (javaType.owner().SHORT.equals(javaType)) {
            return JExpr.cast(javaType.owner().SHORT, JExpr.lit(0));
        } else {
            return JExpr.lit(0);
        }
    }
    return JExpr._null();
}
 
Example 6
Source File: HashCodeArguments.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public HashCodeArguments property(JBlock block, String propertyName,
		String propertyMethod, JType declarablePropertyType,
		JType propertyType, Collection<JType> possiblePropertyTypes) {
	block.assign(currentHashCode(),
			currentHashCode().mul(JExpr.lit(multiplier())));		
	final JVar propertyValue = block.decl(JMod.FINAL,
			declarablePropertyType, value().name() + propertyName, value()
					.invoke(propertyMethod));
	// We assume that primitive properties are always set
	boolean isAlwaysSet = propertyType.isPrimitive();
	final JExpression propertyHasSetValue = isAlwaysSet ? JExpr.TRUE
			: propertyValue.ne(JExpr._null());
	return spawn(propertyValue, propertyHasSetValue);
}
 
Example 7
Source File: HashCodeArguments.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public HashCodeArguments element(JBlock subBlock, JType elementType) {
	final JVar elementValue = subBlock.decl(JMod.FINAL, elementType,
			value().name() + "Element", value().invoke("next"));
	final boolean isElementAlwaysSet = elementType.isPrimitive();
	final JExpression elementHasSetValue = isElementAlwaysSet ? JExpr.TRUE
			: elementValue.ne(JExpr._null());
	return spawn(elementValue, elementHasSetValue);

}
 
Example 8
Source File: AbstractField.java    From hyperjaxb3 with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns contents to be added to javadoc.
 */
protected final List<Object> listPossibleTypes( CPropertyInfo prop ) {
    List<Object> r = new ArrayList<Object>();
    for( CTypeInfo tt : prop.ref() ) {
        JType t = tt.getType().toType(outline.parent(),Aspect.EXPOSED);
        if( t.isPrimitive() || t.isArray() )
            r.add(t.fullName());
        else {
            r.add(t);
            r.add("\n");
        }
    }

    return r;
}
 
Example 9
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);

}