Java Code Examples for com.sun.codemodel.JExpr#lit()

The following examples show how to use com.sun.codemodel.JExpr#lit() . 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: 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 2
Source File: EvaluationVisitor.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public HoldingContainer visitQuotedStringConstant(QuotedString e, ClassGenerator<?> generator)
    throws RuntimeException {
  CompleteType completeType = CompleteType.VARCHAR;
  JBlock setup = generator.getBlock(BlockType.SETUP);
  JType holderType = CodeModelArrowHelper.getHolderType(completeType, generator.getModel());
  JVar var = generator.declareClassField("string", holderType);
  JExpression stringLiteral = JExpr.lit(e.value);
  JExpression buffer = JExpr.direct("context").invoke("getManagedBuffer");
  setup.assign(var,
      generator.getModel().ref(ValueHolderHelper.class).staticInvoke("getNullableVarCharHolder").arg(buffer).arg(stringLiteral));
  return new HoldingContainer((completeType), var, var.ref("value"), var.ref("isSet"));
}
 
Example 3
Source File: EvaluationVisitor.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public HoldingContainer visitIntervalDayConstant(IntervalDayExpression e, ClassGenerator<?> generator)
    throws RuntimeException {
  CompleteType completeType = CompleteType.INTERVAL_DAY_SECONDS;
  JBlock setup = generator.getBlock(BlockType.SETUP);
  JType holderType = CodeModelArrowHelper.getHolderType(completeType, generator.getModel());
  JVar var = generator.declareClassField("intervalday", holderType);
  JExpression dayLiteral = JExpr.lit(e.getIntervalDay());
  JExpression millisLiteral = JExpr.lit(e.getIntervalMillis());
  setup.assign(
      var,
      generator.getModel().ref(ValueHolderHelper.class).staticInvoke("getNullableIntervalDayHolder").arg(dayLiteral)
          .arg(millisLiteral));
  return new HoldingContainer(completeType, var, var.ref("value"), var.ref("isSet"));
}
 
Example 4
Source File: EvaluationVisitor.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public HoldingContainer visitDecimalConstant(DecimalExpression e, ClassGenerator<?> generator)
  throws RuntimeException {
  CompleteType completeType = CompleteType.fromDecimalPrecisionScale(e.getPrecision(), e.getScale());
  JBlock setup = generator.getBlock(BlockType.SETUP);
  JType holderType = CodeModelArrowHelper.getHolderType(completeType, generator.getModel());
  JVar var = generator.declareClassField("dec38", holderType);
  JExpression decimal = JExpr.lit(e.getDecimal().toString());
  JExpression buffer = JExpr.direct("context").invoke("getManagedBuffer");
  setup.assign(var, generator.getModel().ref(ValueHolderHelper.class).staticInvoke
    ("getNullableDecimalHolder").arg(buffer).arg(decimal));
  return new HoldingContainer(completeType, var, var.ref("value"), var.ref("isSet"));
}
 
Example 5
Source File: DefaultTypeUse.java    From hyperjaxb3 with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public JExpression createConstant(Outline outline, XmlString lexical) {
	if (isCollection())
		return null;

	if (adapter == null)
		return coreType.createConstant(outline, lexical);

	// [RESULT] new Adapter().unmarshal(CONSTANT);
	JExpression cons = coreType.createConstant(outline, lexical);
	@SuppressWarnings("unchecked")
	Class<? extends XmlAdapter<?, ?>> atype = (Class<? extends XmlAdapter<?, ?>>) adapter
			.getAdapterIfKnown();

	// try to run the adapter now rather than later.
	if (cons instanceof JStringLiteral && atype != null) {
		JStringLiteral scons = (JStringLiteral) cons;
		@SuppressWarnings("unchecked")
		XmlAdapter<Object, String> a = (XmlAdapter<Object, String>) ClassFactory
				.create(atype);
		try {
			Object value = a.unmarshal(scons.str);
			if (value instanceof String) {
				return JExpr.lit((String) value);
			}
		} catch (Exception e) {
			// assume that we can't eagerly bind this
		}
	}

	return JExpr._new(adapter.getAdapterClass(outline)).invoke("unmarshal")
			.arg(cons);
}
 
Example 6
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 7
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;
}