Java Code Examples for com.sun.codemodel.JBlock#block()

The following examples show how to use com.sun.codemodel.JBlock#block() . 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: SimpleHashCodePlugin.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
	protected void generate(ClassOutline classOutline, JDefinedClass theClass) {

		final JCodeModel codeModel = theClass.owner();
		final JMethod object$hashCode = theClass.method(JMod.PUBLIC,
				codeModel.INT, "hashCode");
		object$hashCode.annotate(Override.class);
		{
			final JBlock body = object$hashCode.body();

			final JExpression currentHashCodeExpression = JExpr.lit(1);

			final JVar currentHashCode = body.decl(codeModel.INT,
					"currentHashCode", currentHashCodeExpression);

			final Boolean superClassImplementsHashCode = StrategyClassUtils
					.superClassNotIgnored(classOutline, getIgnoring());

			if (superClassImplementsHashCode != null) {
				body.assign(
						currentHashCode,
						currentHashCode.mul(JExpr.lit(getMultiplier())).plus(
								JExpr._super().invoke("hashCode")));
			}

			final FieldOutline[] declaredFields = FieldOutlineUtils.filter(
					classOutline.getDeclaredFields(), getIgnoring());

			if (declaredFields.length > 0) {

				for (final FieldOutline fieldOutline : declaredFields) {
					final FieldAccessorEx fieldAccessor = getFieldAccessorFactory()
							.createFieldAccessor(fieldOutline, JExpr._this());
					if (fieldAccessor.isConstant()) {
						continue;
					}
					final JBlock block = body.block();
					block.assign(currentHashCode,
							currentHashCode.mul(JExpr.lit(getMultiplier())));

					String propertyName = fieldOutline.getPropertyInfo()
							.getName(true);
					final JVar value = block.decl(fieldAccessor.getType(),
							"the" + propertyName);

					fieldAccessor.toRawValue(block, value);
					final JType exposedType = fieldAccessor.getType();

					final Collection<JType> possibleTypes = FieldUtils
							.getPossibleTypes(fieldOutline, Aspect.EXPOSED);
					final boolean isAlwaysSet = fieldAccessor.isAlwaysSet();
//					final JExpression hasSetValue = exposedType.isPrimitive() ? JExpr.TRUE
//							: value.ne(JExpr._null());
					
					final JExpression hasSetValue = (fieldAccessor.isAlwaysSet() || fieldAccessor
							.hasSetValue() == null) ? JExpr.TRUE
							: fieldAccessor.hasSetValue();					
					getCodeGenerator().generate(
							block,
							exposedType,
							possibleTypes,
							isAlwaysSet,
							new HashCodeArguments(codeModel, currentHashCode,
									getMultiplier(), value, hasSetValue));
				}
			}
			body._return(currentHashCode);
		}
	}
 
Example 3
Source File: ToStringPlugin.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 4 votes vote down vote up
protected JMethod generateToString$appendFields(ClassOutline classOutline,
		final JDefinedClass theClass) {
	final JCodeModel codeModel = theClass.owner();

	final JMethod toString$appendFields = theClass.method(JMod.PUBLIC,
			codeModel.ref(StringBuilder.class), "appendFields");
	toString$appendFields.annotate(Override.class);
	{
		final JVar locator = toString$appendFields.param(
				ObjectLocator.class, "locator");
		final JVar buffer = toString$appendFields.param(
				StringBuilder.class, "buffer");
		final JVar toStringStrategy = toString$appendFields.param(
				ToStringStrategy2.class, "strategy");
		final JBlock body = toString$appendFields.body();

		final Boolean superClassImplementsToString = StrategyClassUtils
				.superClassImplements(classOutline, ignoring,
						ToString2.class);

		if (superClassImplementsToString == null) {
			// No superclass
		} else if (superClassImplementsToString.booleanValue()) {
			body.invoke(JExpr._super(), "appendFields").arg(locator)
					.arg(buffer).arg(toStringStrategy);
		} else {
			// Superclass does not implement ToString
		}

		final FieldOutline[] declaredFields = FieldOutlineUtils.filter(
				classOutline.getDeclaredFields(), getIgnoring());

		if (declaredFields.length > 0) {

			for (final FieldOutline fieldOutline : declaredFields) {
				final JBlock block = body.block();
				final FieldAccessorEx fieldAccessor = getFieldAccessorFactory()
						.createFieldAccessor(fieldOutline, JExpr._this());
				final JVar theValue = block.decl(
						fieldAccessor.getType(),
						"the"
								+ fieldOutline.getPropertyInfo().getName(
										true));

				final JExpression valueIsSet = (fieldAccessor.isAlwaysSet() || fieldAccessor
						.hasSetValue() == null) ? JExpr.TRUE
						: fieldAccessor.hasSetValue();

				fieldAccessor.toRawValue(block, theValue);

				block.invoke(toStringStrategy, "appendField")
						.arg(locator)
						.arg(JExpr._this())
						.arg(JExpr.lit(fieldOutline.getPropertyInfo()
								.getName(false))).arg(buffer).arg(theValue)
						.arg(valueIsSet);
			}
		}
		body._return(buffer);
	}
	return toString$appendFields;
}
 
Example 4
Source File: HashCodePlugin.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 4 votes vote down vote up
protected JMethod generateHashCode$hashCode(ClassOutline classOutline,
		final JDefinedClass theClass) {

	JCodeModel codeModel = theClass.owner();
	final JMethod hashCode$hashCode = theClass.method(JMod.PUBLIC,
			codeModel.INT, "hashCode");
	hashCode$hashCode.annotate(Override.class);
	{
		final JVar locator = hashCode$hashCode.param(ObjectLocator.class,
				"locator");
		final JVar hashCodeStrategy = hashCode$hashCode.param(
				HashCodeStrategy2.class, "strategy");
		final JBlock body = hashCode$hashCode.body();

		final JExpression currentHashCodeExpression;

		final Boolean superClassImplementsHashCode = StrategyClassUtils
				.superClassImplements(classOutline, ignoring,
						HashCode2.class);

		if (superClassImplementsHashCode == null) {
			currentHashCodeExpression = JExpr.lit(1);
		} else if (superClassImplementsHashCode.booleanValue()) {
			currentHashCodeExpression = JExpr._super().invoke("hashCode")
					.arg(locator).arg(hashCodeStrategy);
		} else {
			currentHashCodeExpression = JExpr._super().invoke("hashCode");
		}

		final JVar currentHashCode = body.decl(codeModel.INT,
				"currentHashCode", currentHashCodeExpression);

		final FieldOutline[] declaredFields = FieldOutlineUtils.filter(
				classOutline.getDeclaredFields(), getIgnoring());

		if (declaredFields.length > 0) {

			for (final FieldOutline fieldOutline : declaredFields) {
				final FieldAccessorEx fieldAccessor = getFieldAccessorFactory()
						.createFieldAccessor(fieldOutline, JExpr._this());
				if (fieldAccessor.isConstant()) {
					continue;
				}
				final JBlock block = body.block();

				final JVar theValue = block.decl(
						fieldAccessor.getType(),
						"the"
								+ fieldOutline.getPropertyInfo().getName(
										true));

				final JExpression valueIsSet = (fieldAccessor.isAlwaysSet() || fieldAccessor
						.hasSetValue() == null) ? JExpr.TRUE
						: fieldAccessor.hasSetValue();

				fieldAccessor.toRawValue(block, theValue);

				block.assign(
						currentHashCode,
						hashCodeStrategy
								.invoke("hashCode")
								.arg(codeModel
										.ref(LocatorUtils.class)
										.staticInvoke("property")
										.arg(locator)
										.arg(fieldOutline.getPropertyInfo()
												.getName(false))
										.arg(theValue))
								.arg(currentHashCode).arg(theValue)
								.arg(valueIsSet));
			}
		}
		body._return(currentHashCode);
	}
	return hashCode$hashCode;
}
 
Example 5
Source File: SimpleToStringPlugin.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 4 votes vote down vote up
protected JMethod generateToString$appendFields(ClassOutline classOutline,
		final JDefinedClass theClass) {
	final JCodeModel codeModel = theClass.owner();

	final JMethod toString$appendFields = theClass.method(JMod.PUBLIC,
			codeModel.ref(StringBuilder.class), "appendFields");
	toString$appendFields.annotate(Override.class);
	{
		final JVar locator = toString$appendFields.param(
				ObjectLocator.class, "locator");
		final JVar buffer = toString$appendFields.param(
				StringBuilder.class, "buffer");
		final JVar toStringStrategy = toString$appendFields.param(
				ToStringStrategy2.class, "strategy");
		final JBlock body = toString$appendFields.body();

		final Boolean superClassImplementsToString = StrategyClassUtils
				.superClassImplements(classOutline, ignoring,
						ToString2.class);

		if (superClassImplementsToString == null) {
			// No superclass
		} else if (superClassImplementsToString.booleanValue()) {
			body.invoke(JExpr._super(), "appendFields").arg(locator)
					.arg(buffer).arg(toStringStrategy);
		} else {
			// Superclass does not implement ToString
		}

		final FieldOutline[] declaredFields = FieldOutlineUtils.filter(
				classOutline.getDeclaredFields(), getIgnoring());

		if (declaredFields.length > 0) {

			for (final FieldOutline fieldOutline : declaredFields) {
				final JBlock block = body.block();
				final FieldAccessorEx fieldAccessor = getFieldAccessorFactory()
						.createFieldAccessor(fieldOutline, JExpr._this());
				final JVar theValue = block.decl(
						fieldAccessor.getType(),
						"the"
								+ fieldOutline.getPropertyInfo().getName(
										true));
				final JExpression valueIsSet = (fieldAccessor.isAlwaysSet() || fieldAccessor
						.hasSetValue() == null) ? JExpr.TRUE
						: fieldAccessor.hasSetValue();

				fieldAccessor.toRawValue(block, theValue);

				block.invoke(toStringStrategy, "appendField")
						.arg(locator)
						.arg(JExpr._this())
						.arg(JExpr.lit(fieldOutline.getPropertyInfo()
								.getName(false))).arg(buffer).arg(theValue)
						.arg(valueIsSet);
			}
		}
		body._return(buffer);
	}
	return toString$appendFields;
}