本文整理汇总了Java中com.sun.codemodel.JExpr._new方法的典型用法代码示例。如果您正苦于以下问题:Java JExpr._new方法的具体用法?Java JExpr._new怎么用?Java JExpr._new使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.codemodel.JExpr
的用法示例。
在下文中一共展示了JExpr._new方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addFactoryMethod
import com.sun.codemodel.JExpr; //导入方法依赖的package包/类
private void addFactoryMethod(JDefinedClass _enum, JType backingType) {
JFieldVar quickLookupMap = addQuickLookupMap(_enum, backingType);
JMethod fromValue = _enum.method(JMod.PUBLIC | JMod.STATIC, _enum, "fromValue");
JVar valueParam = fromValue.param(backingType, "value");
JBlock body = fromValue.body();
JVar constant = body.decl(_enum, "constant");
constant.init(quickLookupMap.invoke("get").arg(valueParam));
JConditional _if = body._if(constant.eq(JExpr._null()));
JInvocation illegalArgumentException = JExpr._new(_enum.owner().ref(IllegalArgumentException.class));
JExpression expr = valueParam;
// if string no need to add ""
if(!isString(backingType)){
expr = expr.plus(JExpr.lit(""));
}
illegalArgumentException.arg(expr);
_if._then()._throw(illegalArgumentException);
_if._else()._return(constant);
ruleFactory.getAnnotator().enumCreatorMethod(fromValue);
}
示例2: getDefaultSet
import com.sun.codemodel.JExpr; //导入方法依赖的package包/类
/**
* Creates a default value for a set property by:
* <ol>
* <li>Creating a new {@link LinkedHashSet} with the correct generic type
* <li>Using {@link Arrays#asList(Object...)} to initialize the set with the
* correct default values
* </ol>
*
* @param fieldType
* the java type that applies for this field ({@link Set} with
* some generic type argument)
* @param node
* the node containing default values for this set
* @return an expression that creates a default value that can be assigned
* to this field
*/
private JExpression getDefaultSet(JType fieldType, JsonNode node) {
JClass setGenericType = ((JClass) fieldType).getTypeParameters().get(0);
JClass setImplClass = fieldType.owner().ref(LinkedHashSet.class);
setImplClass = setImplClass.narrow(setGenericType);
JInvocation newSetImpl = JExpr._new(setImplClass);
if (node instanceof ArrayNode && node.size() > 0) {
JInvocation invokeAsList = fieldType.owner().ref(Arrays.class).staticInvoke("asList");
for (JsonNode defaultValue : node) {
invokeAsList.arg(getDefaultValue(setGenericType, defaultValue));
}
newSetImpl.arg(invokeAsList);
} else if (!ruleFactory.getGenerationConfig().isInitializeCollections()) {
return JExpr._null();
}
return newSetImpl;
}
示例3: getDefaultList
import com.sun.codemodel.JExpr; //导入方法依赖的package包/类
/**
* Creates a default value for a list property by:
* <ol>
* <li>Creating a new {@link ArrayList} with the correct generic type
* <li>Using {@link Arrays#asList(Object...)} to initialize the list with
* the correct default values
* </ol>
*
* @param fieldType
* the java type that applies for this field ({@link List} with
* some generic type argument)
* @param node
* the node containing default values for this list
* @return an expression that creates a default value that can be assigned
* to this field
*/
private JExpression getDefaultList(JType fieldType, JsonNode node) {
JClass listGenericType = ((JClass) fieldType).getTypeParameters().get(0);
JClass listImplClass = fieldType.owner().ref(ArrayList.class);
listImplClass = listImplClass.narrow(listGenericType);
JInvocation newListImpl = JExpr._new(listImplClass);
if (node instanceof ArrayNode && node.size() > 0) {
JInvocation invokeAsList = fieldType.owner().ref(Arrays.class).staticInvoke("asList");
for (JsonNode defaultValue : node) {
invokeAsList.arg(getDefaultValue(listGenericType, defaultValue));
}
newListImpl.arg(invokeAsList);
} else if (!ruleFactory.getGenerationConfig().isInitializeCollections()) {
return JExpr._null();
}
return newListImpl;
}
示例4: addHashCode
import com.sun.codemodel.JExpr; //导入方法依赖的package包/类
private void addHashCode(JDefinedClass jclass, JsonNode node) {
Map<String, JFieldVar> fields = removeFieldsExcludedFromEqualsAndHashCode(jclass.fields(), node);
JMethod hashCode = jclass.method(JMod.PUBLIC, int.class, "hashCode");
Class<?> hashCodeBuilder = ruleFactory.getGenerationConfig().isUseCommonsLang3() ? org.apache.commons.lang3.builder.HashCodeBuilder.class : org.apache.commons.lang.builder.HashCodeBuilder.class;
JBlock body = hashCode.body();
JClass hashCodeBuilderClass = jclass.owner().ref(hashCodeBuilder);
JInvocation hashCodeBuilderInvocation = JExpr._new(hashCodeBuilderClass);
if (!jclass._extends().fullName().equals(Object.class.getName())) {
hashCodeBuilderInvocation = hashCodeBuilderInvocation.invoke("appendSuper").arg(JExpr._super().invoke("hashCode"));
}
for (JFieldVar fieldVar : fields.values()) {
if ((fieldVar.mods().getValue() & JMod.STATIC) == JMod.STATIC) {
continue;
}
hashCodeBuilderInvocation = hashCodeBuilderInvocation.invoke("append").arg(fieldVar);
}
body._return(hashCodeBuilderInvocation.invoke("toHashCode"));
hashCode.annotate(Override.class);
}
示例5: getUDFInstance
import com.sun.codemodel.JExpr; //导入方法依赖的package包/类
private JInvocation getUDFInstance(JCodeModel m) {
if (isGenericUDF) {
return JExpr._new(m.directClass(genericUdfClazz.getCanonicalName()));
} else {
return JExpr._new(m.directClass(GenericUDFBridge.class.getCanonicalName()))
.arg(JExpr.lit(udfName))
.arg(JExpr.lit(false))
.arg(JExpr.lit(udfClazz.getCanonicalName().toString()));
}
}
示例6: addEquals
import com.sun.codemodel.JExpr; //导入方法依赖的package包/类
private void addEquals(JDefinedClass jclass, JsonNode node) {
Map<String, JFieldVar> fields = removeFieldsExcludedFromEqualsAndHashCode(jclass.fields(), node);
JMethod equals = jclass.method(JMod.PUBLIC, boolean.class, "equals");
JVar otherObject = equals.param(Object.class, "other");
Class<?> equalsBuilder = ruleFactory.getGenerationConfig().isUseCommonsLang3() ? org.apache.commons.lang3.builder.EqualsBuilder.class : org.apache.commons.lang.builder.EqualsBuilder.class;
JBlock body = equals.body();
body._if(otherObject.eq(JExpr._this()))._then()._return(JExpr.TRUE);
body._if(otherObject._instanceof(jclass).eq(JExpr.FALSE))._then()._return(JExpr.FALSE);
JVar rhsVar = body.decl(jclass, "rhs").init(JExpr.cast(jclass, otherObject));
JClass equalsBuilderClass = jclass.owner().ref(equalsBuilder);
JInvocation equalsBuilderInvocation = JExpr._new(equalsBuilderClass);
if (!jclass._extends().fullName().equals(Object.class.getName())) {
equalsBuilderInvocation = equalsBuilderInvocation.invoke("appendSuper").arg(JExpr._super().invoke("equals").arg(otherObject));
}
for (JFieldVar fieldVar : fields.values()) {
if ((fieldVar.mods().getValue() & JMod.STATIC) == JMod.STATIC) {
continue;
}
equalsBuilderInvocation = equalsBuilderInvocation.invoke("append")
.arg(fieldVar)
.arg(rhsVar.ref(fieldVar.name()));
}
JInvocation reflectionEquals = jclass.owner().ref(equalsBuilder).staticInvoke("reflectionEquals");
reflectionEquals.arg(JExpr._this());
reflectionEquals.arg(otherObject);
body._return(equalsBuilderInvocation.invoke("isEquals"));
equals.annotate(Override.class);
}
示例7: getDefaultValue
import com.sun.codemodel.JExpr; //导入方法依赖的package包/类
static JExpression getDefaultValue(JType fieldType, JsonNode node) {
fieldType = fieldType.unboxify();
if (fieldType.fullName().equals(String.class.getName())) {
return JExpr.lit(node.asText());
} else if (fieldType.fullName().equals(int.class.getName())) {
return JExpr.lit(Integer.parseInt(node.asText()));
} else if (fieldType.fullName().equals(BigInteger.class.getName())) {
return JExpr._new(fieldType).arg(JExpr.lit(node.asText()));
} else if (fieldType.fullName().equals(double.class.getName())) {
return JExpr.lit(Double.parseDouble(node.asText()));
} else if (fieldType.fullName().equals(BigDecimal.class.getName())) {
return JExpr._new(fieldType).arg(JExpr.lit(node.asText()));
} else if (fieldType.fullName().equals(boolean.class.getName())) {
return JExpr.lit(Boolean.parseBoolean(node.asText()));
} else if (fieldType.fullName().equals(DateTime.class.getName()) || fieldType.fullName().equals(Date.class.getName())) {
long millisecs = parseDateToMillisecs(node.asText());
JInvocation newDateTime = JExpr._new(fieldType);
newDateTime.arg(JExpr.lit(millisecs));
return newDateTime;
} else if (fieldType.fullName().equals(LocalDate.class.getName()) || fieldType.fullName().equals(LocalTime.class.getName())) {
JInvocation stringParseableTypeInstance = JExpr._new(fieldType);
stringParseableTypeInstance.arg(JExpr.lit(node.asText()));
return stringParseableTypeInstance;
} else if (fieldType.fullName().equals(long.class.getName())) {
return JExpr.lit(Long.parseLong(node.asText()));
} else if (fieldType.fullName().equals(float.class.getName())) {
return JExpr.lit(Float.parseFloat(node.asText()));
} else if (fieldType.fullName().equals(URI.class.getName())) {
JInvocation invokeCreate = fieldType.owner().ref(URI.class).staticInvoke("create");
return invokeCreate.arg(JExpr.lit(node.asText()));
} else if (fieldType instanceof JDefinedClass && ((JDefinedClass) fieldType).getClassType().equals(ClassType.ENUM)) {
return getDefaultEnum(fieldType, node);
} else {
return JExpr._null();
}
}