Java Code Examples for com.sun.codemodel.JExpr#TRUE

The following examples show how to use com.sun.codemodel.JExpr#TRUE . 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: 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 2
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 3
Source File: EqualsArguments.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public EqualsArguments cast(String suffix, JBlock block,
		JType jaxbElementType, boolean suppressWarnings) {
	final JVar castedLeftValue = block.decl(JMod.FINAL, jaxbElementType,
			leftValue().name() + suffix,
			JExpr.cast(jaxbElementType, leftValue()));
	if (suppressWarnings) {
		castedLeftValue.annotate(SuppressWarnings.class).param("value",
				"unchecked");
	}
	final JVar castedRightValue = block.decl(JMod.FINAL, jaxbElementType,
			rightValue().name() + suffix,
			JExpr.cast(jaxbElementType, rightValue()));
	if (suppressWarnings) {
		castedRightValue.annotate(SuppressWarnings.class).param("value",
				"unchecked");
	}
	return new EqualsArguments(getCodeModel(), castedLeftValue, JExpr.TRUE,
			castedRightValue, JExpr.TRUE);
}
 
Example 4
Source File: PropertyFieldAccessorFactory.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public boolean isAlwaysSet() {
	if (constantField != null) {
		return true;
	} else {
		return JExpr.TRUE == fieldAccessor.hasSetValue();
	}
}
 
Example 5
Source File: PropertyFieldAccessorFactory.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public JExpression hasSetValue() {
	if (constantField != null) {
		return JExpr.TRUE;
	} else if (isSetter != null) {
		return targetObject.invoke(isSetter);
	} else {
		return fieldAccessor.hasSetValue();
	}
}
 
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: ChainedHashTable.java    From Bats with Apache License 2.0 4 votes vote down vote up
private void setupIsKeyMatchInternal(ClassGenerator<HashTable> cg, MappingSet incomingMapping, MappingSet htableMapping,
    LogicalExpression[] keyExprs, List<Comparator> comparators, TypedFieldId[] htKeyFieldIds, SetupWork work) {

  boolean checkIfBothNulls = work == SetupWork.CHECK_BOTH_NULLS;

  // Regular key matching may return false in the middle (i.e., some pair of columns did not match), and true only if all matched;
  // but "both nulls" check returns the opposite logic (i.e., true when one pair of nulls is found, need check no more)
  JExpression midPointResult = checkIfBothNulls ? JExpr.TRUE : JExpr.FALSE;
  JExpression finalResult = checkIfBothNulls ? JExpr.FALSE : JExpr.TRUE;

  cg.setMappingSet(incomingMapping);

  if (keyExprs == null || keyExprs.length == 0 ||
      checkIfBothNulls && ! comparators.contains(Comparator.EQUALS)) { // e.g. for Hash-Aggr, or non-equi join
    cg.getEvalBlock()._return(JExpr.FALSE);
    return;
  }

  for (int i = 0; i < keyExprs.length; i++) {
    final LogicalExpression expr = keyExprs[i];
    cg.setMappingSet(incomingMapping);
    HoldingContainer left = cg.addExpr(expr, ClassGenerator.BlkCreateMode.TRUE_IF_BOUND);

    cg.setMappingSet(htableMapping);
    ValueVectorReadExpression vvrExpr = new ValueVectorReadExpression(htKeyFieldIds[i]);
    HoldingContainer right = cg.addExpr(vvrExpr, ClassGenerator.BlkCreateMode.FALSE);

    JConditional jc;

    if ( work != SetupWork.DO_BUILD ) {  // BUILD runs this logic in a separate method - areBothKeysNull()
      // codegen for the special case when both columns are null (i.e., return early with midPointResult)
      if (comparators.get(i) == Comparator.EQUALS
          && left.isOptional() && right.isOptional()) {
        jc = cg.getEvalBlock()._if(left.getIsSet().eq(JExpr.lit(0)).
          cand(right.getIsSet().eq(JExpr.lit(0))));
        jc._then()._return(midPointResult);
      }
    }
    if ( ! checkIfBothNulls ) { // generate comparison code (at least one of the two columns' values is non-null)
      final LogicalExpression f = FunctionGenerationHelper.getOrderingComparatorNullsHigh(left, right, context.getFunctionRegistry());

      HoldingContainer out = cg.addExpr(f, ClassGenerator.BlkCreateMode.FALSE);

      // check if two values are not equal (comparator result != 0)
      jc = cg.getEvalBlock()._if(out.getValue().ne(JExpr.lit(0)));

      jc._then()._return(midPointResult);
    }
  }

  // All key expressions compared the same way, so return the appropriate final result
  cg.getEvalBlock()._return(finalResult);
}
 
Example 9
Source File: ConstantPropertyOutline.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public JExpression isSet() {
	return JExpr.TRUE;
}
 
Example 10
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 11
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 12
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 13
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;
}