com.sun.codemodel.JFieldRef Java Examples

The following examples show how to use com.sun.codemodel.JFieldRef. 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: ClassGenerator.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Adds local variable declaration based on given name and type.
 *
 * @param t major type
 * @param name variable name
 * @param includeNewInstance whether to create new instance
 * @return holder instance
 */
public HoldingContainer declare(MajorType t, String name, boolean includeNewInstance) {
  JType holderType = getHolderType(t);
  JVar var;
  if (includeNewInstance) {
    var = getEvalBlock().decl(holderType, name + index, JExpr._new(holderType));
  } else {
    var = getEvalBlock().decl(holderType, name + index);
  }
  JFieldRef outputSet = null;
  if (t.getMode() == DataMode.OPTIONAL) {
    outputSet = var.ref("isSet");
  }
  index++;
  return new HoldingContainer(t, var, var.ref("value"), outputSet);
}
 
Example #2
Source File: BoundPropertiesPlugin.java    From jaxb2-rich-contract-plugin with MIT License 6 votes vote down vote up
private JMethod generateLazyProxyInitGetter(final ClassOutline classOutline, final FieldOutline fieldOutline) {
	final JCodeModel m = classOutline.parent().getCodeModel();
	final JDefinedClass definedClass = classOutline.implClass;
	final String fieldName = fieldOutline.getPropertyInfo().getName(false);
	final String getterName = "get" + fieldOutline.getPropertyInfo().getName(true);
	final JFieldVar collectionField = definedClass.fields().get(fieldName);
	final JClass elementType = ((JClass) collectionField.type()).getTypeParameters().get(0);
	final JClass proxyFieldType = m.ref(BoundList.class).narrow(elementType);
	final JFieldRef collectionFieldRef = JExpr._this().ref(collectionField);
	final JFieldRef proxyField = JExpr._this().ref(collectionField.name() + BoundPropertiesPlugin.PROXY_SUFFIX);
	final JMethod oldGetter = definedClass.getMethod(getterName, new JType[0]);
	definedClass.methods().remove(oldGetter);
	final JMethod newGetter = definedClass.method(JMod.PUBLIC, proxyFieldType, getterName);
	newGetter.body()._if(collectionFieldRef.eq(JExpr._null()))._then().assign(collectionFieldRef, JExpr._new(m.ref(ArrayList.class).narrow(elementType)));
	final JBlock ifProxyNull = newGetter.body()._if(proxyField.eq(JExpr._null()))._then();
	ifProxyNull.assign(proxyField, JExpr._new(m.ref(BoundListProxy.class).narrow(elementType)).arg(collectionFieldRef));
	newGetter.body()._return(proxyField);
	return newGetter;
}
 
Example #3
Source File: ImmutableJaxbGenerator.java    From rice with Educational Community License v2.0 5 votes vote down vote up
private void renderField(JDefinedClass classModel, FieldModel fieldModel) {
	JFieldVar field = classModel.field(JMod.PRIVATE | JMod.FINAL, fieldModel.fieldType, fieldModel.fieldName);
	JAnnotationUse annotation = field.annotate(XmlElement.class);
	if (Util.isCommonElement(fieldModel.fieldName)) {
		JClass coreConstants = codeModel.ref(CoreConstants.class);
		JFieldRef commonElementsRef = coreConstants.staticRef("CommonElements");
		annotation.param("name", commonElementsRef.ref(Util.toConstantsVariable(fieldModel.fieldName)));
	} else {
		JClass elementsClass = codeModel.ref(Util.ELEMENTS_CLASS_NAME);
		JFieldRef fieldXmlNameRef = elementsClass.staticRef(Util.toConstantsVariable(fieldModel.fieldName));
		annotation.param("name", fieldXmlNameRef);
	}
	annotation.param("required", false);
}
 
Example #4
Source File: EvaluationVisitor.java    From Bats with Apache License 2.0 5 votes vote down vote up
private HoldingContainer getHoldingContainer(ClassGenerator<?> generator,
                                             MajorType majorType,
                                             Function<DrillBuf, ? extends ValueHolder> function) {
  JType holderType = generator.getHolderType(majorType);
  Pair<Integer, JVar> depthVar = generator.declareClassConstField("const", holderType, function);
  JFieldRef outputSet = null;
  JVar var = depthVar.getValue();
  if (majorType.getMode() == TypeProtos.DataMode.OPTIONAL) {
    outputSet = var.ref("isSet");
  }
  return new HoldingContainer(majorType, var, var.ref("value"), outputSet);
}
 
Example #5
Source File: ImmutableJaxbGenerator.java    From rice with Educational Community License v2.0 5 votes vote down vote up
private void renderClassLevelAnnotations(JDefinedClass classModel, List<FieldModel> fields) throws Exception {
	JFieldRef constantsClass = classModel.staticRef(Util.CONSTANTS_CLASS_NAME);
	JFieldRef elementsClass = classModel.staticRef(Util.ELEMENTS_CLASS_NAME);
	JClass coreConstants = codeModel.ref(CoreConstants.class);
	JFieldRef commonElementsRef = coreConstants.staticRef("CommonElements");
	
	// XmlRootElement
	JAnnotationUse rootElementAnnotation = classModel.annotate(XmlRootElement.class);
	rootElementAnnotation.param("name", constantsClass.ref(Util.ROOT_ELEMENT_NAME_FIELD));
	
	// XmlAccessorType
	JAnnotationUse xmlAccessorTypeAnnotation = classModel.annotate(XmlAccessorType.class);
	xmlAccessorTypeAnnotation.param("value", XmlAccessType.NONE);
	
	// XmlType
	JAnnotationUse xmlTypeAnnotation = classModel.annotate(XmlType.class);
	xmlTypeAnnotation.param("name", constantsClass.ref(Util.TYPE_NAME_FIELD));
	JAnnotationArrayMember propOrderMember = xmlTypeAnnotation.paramArray("propOrder");
	for (FieldModel field : fields) {
		if (Util.isCommonElement(field.fieldName)) {
			propOrderMember.param(commonElementsRef.ref(Util.toConstantsVariable(field.fieldName)));
		} else {
			propOrderMember.param(elementsClass.ref(Util.toConstantsVariable(field.fieldName)));
		}
	}
	propOrderMember.param(commonElementsRef.ref("FUTURE_ELEMENTS"));
}
 
Example #6
Source File: PMMLPlugin.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public JExpression compute(Outline outline){
	JExpression expression = computeInit(outline);

	if((expression instanceof JFieldRef) || (expression instanceof JStringLiteral)){
		setField(null);

		return expression;
	}

	return JExpr.ref(getField());
}
 
Example #7
Source File: SelectorGenerator.java    From jaxb2-rich-contract-plugin with MIT License 5 votes vote down vote up
public void generateMetaFields() {
	if(this.classOutline.getSuperClass() != null) {
		this.selectorClass._extends(this.selectorGenerator.getInfoClass(this.classOutline.getSuperClass().implClass).selectorClass.narrow(this.rootTypeParam).narrow(this.parentTypeParam));
	}
	for (final FieldOutline fieldOutline : this.classOutline.getDeclaredFields()) {
		final JFieldVar definedField = PluginUtil.getDeclaredField(fieldOutline);
		if (definedField != null) {
			final JType elementType = PluginUtil.getElementType(fieldOutline);
			if (elementType.isReference()) {
				final ClassOutline modelClass = this.selectorGenerator.getPluginContext().getClassOutline(elementType);
				final JClass returnType;
				if (modelClass != null) {
					returnType = this.selectorGenerator.getInfoClass(modelClass.implClass).selectorClass.narrow(this.rootTypeParam).narrow(this.selectorClass.narrow(this.rootTypeParam).narrow(this.parentTypeParam));
				} else {
					returnType = this.selectorGenerator.getPluginContext().codeModel.ref(this.selectorGenerator.selectorBaseClass).narrow(this.rootTypeParam).narrow(this.selectorClass.narrow(this.rootTypeParam).narrow(this.parentTypeParam));
				}
				final JFieldVar includeField = this.selectorClass.field(JMod.PRIVATE, returnType, definedField.name(), JExpr._null());
				final JFieldRef fieldRef = JExpr._this().ref(includeField);
				final JMethod includeMethod = this.selectorClass.method(JMod.PUBLIC, returnType, definedField.name());
				if(this.selectorGenerator.selectorParamName != null) {
					final JVar includeParam = includeMethod.param(JMod.FINAL, this.selectorGenerator.getSelectorParamType(this.classOutline.implClass, elementType), this.selectorGenerator.selectorParamName);
					includeMethod.body()._return(JOp.cond(fieldRef.eq(JExpr._null()), fieldRef.assign(JExpr._new(returnType).arg(JExpr._this().ref("_root")).arg(JExpr._this()).arg(JExpr.lit(definedField.name())).arg(includeParam)), fieldRef));
				} else {
					includeMethod.body()._return(JOp.cond(fieldRef.eq(JExpr._null()), fieldRef.assign(JExpr._new(returnType).arg(JExpr._this().ref("_root")).arg(JExpr._this()).arg(JExpr.lit(definedField.name()))), fieldRef));
				}

				this.buildChildrenMethod.body()._if(fieldRef.ne(JExpr._null()))._then().add(this.productMapVar.invoke("put").arg(JExpr.lit(definedField.name())).arg(fieldRef.invoke("init")));
			}
		}
	}
	this.buildChildrenMethod.body()._return(this.productMapVar);
}
 
Example #8
Source File: ImmutablePlugin.java    From jaxb2-rich-contract-plugin with MIT License 5 votes vote down vote up
private JInvocation generateImmutableListInstantiation(final PluginContext pluginContext, final JFieldRef fieldRef, final JType elementType) {
	if (this.overrideCollectionClass == null) {
		return pluginContext.unmodifiableList(fieldRef);
	} else {
		final JClass overrideCollection = pluginContext.codeModel.ref(this.overrideCollectionClass);
		if (overrideCollection.isAssignableFrom(pluginContext.codeModel.ref(Collection.class))) {
			return pluginContext.unmodifiableList(fieldRef);
		} else {
			return JExpr._new(overrideCollection.narrow(elementType)).arg(fieldRef);
		}
	}
}
 
Example #9
Source File: ModifierGenerator.java    From jaxb2-rich-contract-plugin with MIT License 5 votes vote down vote up
private void generateCollectionAccessor(final DefinedPropertyOutline fieldOutline) {
	final JFieldVar fieldVar = fieldOutline.getFieldVar();
	if(fieldVar != null) {
		final JMethod modifier = this.modifierClass.method(JMod.PUBLIC, fieldVar.type(), ModifierGenerator.GETTER_PREFIX + fieldOutline.getBaseName());
		if(this.implement) {
			final JFieldRef fieldRef = new NestedThisRef(this.classOutline.getImplClass()).ref(fieldVar);
			final JConditional ifNull = modifier.body()._if(fieldRef.eq(JExpr._null()));
			ifNull._then().assign(fieldRef, JExpr._new(this.classOutline.getImplClass().owner().ref(ArrayList.class).narrow(fieldOutline.getElementType())));
			modifier.body()._return(fieldRef);
		}
	}
}
 
Example #10
Source File: ModifierGenerator.java    From jaxb2-rich-contract-plugin with MIT License 5 votes vote down vote up
private ModifierGenerator(final PluginContext pluginContext, final DefinedTypeOutline classOutline, final String modifierClassName, final String modifierInterfaceName, final Collection<TypeOutline> interfaces, final String modifierMethodName, final boolean implement) throws JClassAlreadyExistsException {
	this.classOutline = classOutline;
	final JDefinedClass definedClass = classOutline.getImplClass();
	this.implement = implement;
	this.modifierClass = definedClass._class(JMod.PUBLIC, modifierClassName, classOutline.getImplClass().getClassType());
	if(interfaces != null) {
		for (final TypeOutline interfaceOutline : interfaces) {
			this.modifierClass._implements( pluginContext.ref(interfaceOutline.getImplClass(), modifierInterfaceName, true));
		}
	}
	final JFieldRef cachedModifierField;
	if(!"java.lang.Object".equals(definedClass._extends().fullName())) {
		this.modifierClass._extends(pluginContext.ref(definedClass._extends(), modifierClassName, false));
		cachedModifierField = JExpr.refthis(ModifierGenerator.MODIFIER_CACHE_FIELD_NAME);
	} else {
		if(implement) {
			cachedModifierField = JExpr._this().ref(definedClass.field(JMod.PROTECTED | JMod.TRANSIENT, this.modifierClass, ModifierGenerator.MODIFIER_CACHE_FIELD_NAME));
		} else {
			cachedModifierField = null;
		}
	}

	final JDefinedClass typeDefinition = classOutline.isInterface() && ((DefinedInterfaceOutline)classOutline).getSupportInterface() != null ? ((DefinedInterfaceOutline)classOutline).getSupportInterface() : definedClass;
	final JMethod modifierMethod = typeDefinition.method(JMod.PUBLIC, this.modifierClass, modifierMethodName);
	if(this.implement) {
		final JConditional ifCacheNull = modifierMethod.body()._if(JExpr._null().eq(cachedModifierField));
		ifCacheNull._then().assign(cachedModifierField, JExpr._new(this.modifierClass));
		modifierMethod.body()._return(JExpr.cast(this.modifierClass, cachedModifierField));
	}

}
 
Example #11
Source File: WrappingCollectionField.java    From hyperjaxb3 with BSD 2-Clause "Simplified" License 5 votes vote down vote up
protected JFieldRef createField() {

		final JFieldVar field = outline.implClass.field(JMod.PROTECTED
				+ JMod.TRANSIENT,

		propertyListType, property.getName(false));
//		field.annotate(XmlTransient.class);
		return JExpr._this().ref(field);
	}
 
Example #12
Source File: WrappedCollectionField.java    From hyperjaxb3 with BSD 2-Clause "Simplified" License 5 votes vote down vote up
protected JFieldRef createField() {

		final JFieldVar field = outline.implClass.field(JMod.PROTECTED
				+ JMod.TRANSIENT,

		propertyListType, property.getName(false));
		// field.annotate(XmlTransient.class);
		annotate(field);
		return JExpr._this().ref(field);
	}
 
Example #13
Source File: AdaptingWrappingCollectionField.java    From hyperjaxb3 with BSD 2-Clause "Simplified" License 5 votes vote down vote up
protected JFieldRef createField() {

		final JFieldVar field = outline.implClass.field(JMod.PROTECTED
				+ JMod.TRANSIENT,

		propertyListType, property.getName(false));
		// field.annotate(XmlTransient.class);
		return JExpr._this().ref(field);
	}
 
Example #14
Source File: ClassGenerator.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public HoldingContainer(CompleteType t, JVar holder, JFieldRef value, JFieldRef isSet, boolean singularRepeated, boolean isReader) {
  this.holder = holder;
  this.value = value;
  this.isSet = isSet;
  this.type = t;
  this.isConstant = false;
  this.singularRepeated = singularRepeated;
  this.isReader = isReader;
}
 
Example #15
Source File: ValueVectorHashHelper.java    From Bats with Apache License 2.0 5 votes vote down vote up
private void setupBuild64Hash(ClassGenerator<Hash64> cg, MappingSet incomingMapping, VectorAccessible batch, LogicalExpression[] keyExprs, TypedFieldId[] toHashKeyFieldIds) throws SchemaChangeException {
  cg.setMappingSet(incomingMapping);
  if (keyExprs == null || keyExprs.length == 0) {
    cg.getEvalBlock()._return(JExpr.lit(0));
  }
  String seedValue = "seedValue";
  String fieldId = "fieldId";
  LogicalExpression seed = ValueExpressions.getParameterExpression(seedValue, Types.required(TypeProtos.MinorType.INT));

  LogicalExpression fieldIdParamExpr = ValueExpressions.getParameterExpression(fieldId, Types.required(TypeProtos.MinorType.INT));
  ClassGenerator.HoldingContainer fieldIdParamHolder = cg.addExpr(fieldIdParamExpr);
  int i = 0;
  for (LogicalExpression expr : keyExprs) {
    TypedFieldId targetTypeFieldId = toHashKeyFieldIds[i];
    ValueExpressions.IntExpression targetBuildFieldIdExp = new ValueExpressions.IntExpression(targetTypeFieldId.getFieldIds()[0], ExpressionPosition.UNKNOWN);

    JFieldRef targetBuildSideFieldId = cg.addExpr(targetBuildFieldIdExp, ClassGenerator.BlkCreateMode.TRUE_IF_BOUND).getValue();
    JBlock ifBlock = cg.getEvalBlock()._if(fieldIdParamHolder.getValue().eq(targetBuildSideFieldId))._then();
    cg.nestEvalBlock(ifBlock);
    LogicalExpression hashExpression = HashPrelUtil.getHash64Expression(expr, seed, true);
    LogicalExpression materializedExpr = ExpressionTreeMaterializer.materializeAndCheckErrors(hashExpression, batch, context.getFunctionRegistry());
    ClassGenerator.HoldingContainer hash = cg.addExpr(materializedExpr, ClassGenerator.BlkCreateMode.TRUE_IF_BOUND);
    ifBlock._return(hash.getValue());
    cg.unNestEvalBlock();
    i++;
  }
  cg.getEvalBlock()._return(JExpr.lit(0));
}
 
Example #16
Source File: ClassGenerator.java    From Bats with Apache License 2.0 5 votes vote down vote up
public HoldingContainer(MajorType t, JVar holder, JFieldRef value, JFieldRef isSet, boolean singularRepeated, boolean isReader) {
  this.holder = holder;
  this.value = value;
  this.isSet = isSet;
  this.type = t;
  this.isConstant = false;
  this.singularRepeated = singularRepeated;
  this.isReader = isReader;
}
 
Example #17
Source File: ClassGenerator.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public JFieldRef getIsSet() {
  Preconditions.checkNotNull(isSet, "You cannot access the isSet variable when operating on a non-nullable output value.");
  return isSet;
}
 
Example #18
Source File: ClassGenerator.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public JFieldRef getValue() {
  return value;
}
 
Example #19
Source File: ClassGenerator.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public JFieldRef f(String name) {
  return holder.ref(name);
}
 
Example #20
Source File: ClassGenerator.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public HoldingContainer(CompleteType t, JVar holder, JFieldRef value, JFieldRef isSet) {
  this(t, holder, value, isSet, false, false);
}
 
Example #21
Source File: ClassGenerator.java    From Bats with Apache License 2.0 4 votes vote down vote up
public JFieldRef getIsSet() {
  Preconditions.checkNotNull(isSet, "You cannot access the isSet variable when operating on a non-nullable output value.");
  return isSet;
}
 
Example #22
Source File: ClassGenerator.java    From Bats with Apache License 2.0 4 votes vote down vote up
public JFieldRef getValue() {
  return value;
}
 
Example #23
Source File: ClassGenerator.java    From Bats with Apache License 2.0 4 votes vote down vote up
public JFieldRef f(String name) {
  return holder.ref(name);
}
 
Example #24
Source File: ClassGenerator.java    From Bats with Apache License 2.0 4 votes vote down vote up
public HoldingContainer(MajorType t, JVar holder, JFieldRef value, JFieldRef isSet) {
  this(t, holder, value, isSet, false, false);
}
 
Example #25
Source File: AbstractWrapCollectionField.java    From hyperjaxb3 with BSD 2-Clause "Simplified" License votes vote down vote up
protected abstract JFieldRef createField();