下面列出了怎么用 com.sun.codemodel.JFieldRef 的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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);
}
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;
}
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);
}
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;
}
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));
}
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;
}
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);
}
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);
}
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);
}
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));
}
}
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);
}
}
}
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);
}
}
}
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);
}
@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());
}
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"));
}
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);
}
public HoldingContainer(MajorType t, JVar holder, JFieldRef value, JFieldRef isSet) {
this(t, holder, value, isSet, false, false);
}
public JFieldRef f(String name) {
return holder.ref(name);
}
public JFieldRef getValue() {
return value;
}
public JFieldRef getIsSet() {
Preconditions.checkNotNull(isSet, "You cannot access the isSet variable when operating on a non-nullable output value.");
return isSet;
}
public HoldingContainer(CompleteType t, JVar holder, JFieldRef value, JFieldRef isSet) {
this(t, holder, value, isSet, false, false);
}
public JFieldRef f(String name) {
return holder.ref(name);
}
public JFieldRef getValue() {
return value;
}
public JFieldRef getIsSet() {
Preconditions.checkNotNull(isSet, "You cannot access the isSet variable when operating on a non-nullable output value.");
return isSet;
}
protected abstract JFieldRef createField();