本文整理匯總了Java中com.sun.codemodel.JFieldVar類的典型用法代碼示例。如果您正苦於以下問題:Java JFieldVar類的具體用法?Java JFieldVar怎麽用?Java JFieldVar使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
JFieldVar類屬於com.sun.codemodel包,在下文中一共展示了JFieldVar類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: addFactoryMethod
import com.sun.codemodel.JFieldVar; //導入依賴的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: apply
import com.sun.codemodel.JFieldVar; //導入依賴的package包/類
@Override
public JFieldVar apply(String nodeName, JsonNode node, JFieldVar field, Schema currentSchema) {
if (ruleFactory.getGenerationConfig().isIncludeJsr303Annotations()
&& (node.has("minItems") || node.has("maxItems"))) {
JAnnotationUse annotation = field.annotate(Size.class);
if (node.has("minItems")) {
annotation.param("min", node.get("minItems").asInt());
}
if (node.has("maxItems")) {
annotation.param("max", node.get("maxItems").asInt());
}
}
return field;
}
示例3: dateField
import com.sun.codemodel.JFieldVar; //導入依賴的package包/類
@Override
public void dateField(JFieldVar field, JsonNode node) {
String pattern = null;
if (node.has("customDatePattern")) {
pattern = node.get("customDatePattern").asText();
} else if (node.has("customPattern")) {
pattern = node.get("customPattern").asText();
} else if (isNotEmpty(getGenerationConfig().getCustomDatePattern())) {
pattern = getGenerationConfig().getCustomDatePattern();
} else if (getGenerationConfig().isFormatDates()) {
pattern = FormatRule.ISO_8601_DATE_FORMAT;
}
if (pattern != null && !field.type().fullName().equals("java.lang.String")) {
field.annotate(JsonFormat.class).param("shape", JsonFormat.Shape.STRING).param("pattern", pattern);
}
}
示例4: apply
import com.sun.codemodel.JFieldVar; //導入依賴的package包/類
/**
* Applies this schema rule to take the required code generation steps.
* <p>
* Default values are implemented by assigning an expression to the given
* field (so when instances of the generated POJO are created, its fields
* will then contain their default values).
* <p>
* Collections (Lists and Sets) are initialized to an empty collection, even
* when no default value is present in the schema (node is null).
*
* @param nodeName
* the name of the property which has (or may have) a default
* @param node
* the default node (may be null if no default node was present
* for this property)
* @param field
* the Java field that has added to a generated type to represent
* this property
* @return field, which will have an init expression is appropriate
*/
@Override
public JFieldVar apply(String nodeName, JsonNode node, JFieldVar field, Schema currentSchema) {
boolean defaultPresent = node != null && isNotEmpty(node.asText());
String fieldType = field.type().fullName();
if (defaultPresent && !field.type().isPrimitive() && node.isNull()) {
field.init(JExpr._null());
} else if (fieldType.startsWith(List.class.getName())) {
field.init(getDefaultList(field.type(), node));
} else if (fieldType.startsWith(Set.class.getName())) {
field.init(getDefaultSet(field.type(), node));
} else if (fieldType.startsWith(String.class.getName()) && node != null ) {
field.init(getDefaultValue(field.type(), node));
} else if (defaultPresent) {
field.init(getDefaultValue(field.type(), node));
}
return field;
}
示例5: apply
import com.sun.codemodel.JFieldVar; //導入依賴的package包/類
/**
* Applies this schema rule to take the not required code generation steps.
* <p>
* The not required rule adds a Nullable annotation if JSR-305 annotations are desired.
*
* @param nodeName
* the name of the schema node for which this "required" rule has
* been added
* @param node
* the "not required" node, having a value <code>false</code> or
* <code>no value</code>
* @param generatableType
* the class or method which may be marked as "not required"
* @return the JavaDoc comment attached to the generatableType, which
* <em>may</em> have an added not to mark this construct as
* not required.
*/
@Override
public JDocCommentable apply(String nodeName, JsonNode node, JDocCommentable generatableType, Schema schema) {
// Since NotRequiredRule is executed for all fields that do not have "required" present,
// we need to recognize whether the field is part of the RequiredArrayRule.
JsonNode requiredArray = schema.getContent().get("required");
if (requiredArray != null) {
for (Iterator<JsonNode> iterator = requiredArray.elements(); iterator.hasNext(); ) {
String requiredArrayItem = iterator.next().asText();
if (nodeName.equals(requiredArrayItem)) {
return generatableType;
}
}
}
if (ruleFactory.getGenerationConfig().isIncludeJsr305Annotations()
&& generatableType instanceof JFieldVar) {
generatableType.javadoc().append(NOT_REQUIRED_COMMENT_TEXT);
((JFieldVar) generatableType).annotate(Nullable.class);
}
return generatableType;
}
示例6: apply
import com.sun.codemodel.JFieldVar; //導入依賴的package包/類
/**
* Applies this schema rule to take the required code generation steps.
* <p>
* The required rule simply adds a note to the JavaDoc comment to mark a
* property as required.
*
* @param nodeName
* the name of the schema node for which this "required" rule has
* been added
* @param node
* the "required" node, having a value <code>true</code> or
* <code>false</code>
* @param generatableType
* the class or method which may be marked as "required"
* @return the JavaDoc comment attached to the generatableType, which
* <em>may</em> have an added not to mark this construct as
* required.
*/
@Override
public JDocCommentable apply(String nodeName, JsonNode node, JDocCommentable generatableType, Schema schema) {
if (node.asBoolean()) {
generatableType.javadoc().append("\n(Required)");
if (ruleFactory.getGenerationConfig().isIncludeJsr303Annotations()
&& generatableType instanceof JFieldVar) {
((JFieldVar) generatableType).annotate(NotNull.class);
}
if (ruleFactory.getGenerationConfig().isIncludeJsr305Annotations()
&& generatableType instanceof JFieldVar) {
((JFieldVar) generatableType).annotate(Nonnull.class);
}
} else {
if (ruleFactory.getGenerationConfig().isIncludeJsr305Annotations()
&& generatableType instanceof JFieldVar) {
((JFieldVar) generatableType).annotate(Nullable.class);
}
}
return generatableType;
}
示例7: apply
import com.sun.codemodel.JFieldVar; //導入依賴的package包/類
@Override
public JFieldVar apply(String nodeName, JsonNode node, JFieldVar field, Schema currentSchema) {
if (ruleFactory.getGenerationConfig().isIncludeJsr303Annotations()
&& (node.has("minLength") || node.has("maxLength"))) {
JAnnotationUse annotation = field.annotate(Size.class);
if (node.has("minLength")) {
annotation.param("min", node.get("minLength").asInt());
}
if (node.has("maxLength")) {
annotation.param("max", node.get("maxLength").asInt());
}
}
return field;
}
示例8: addToString
import com.sun.codemodel.JFieldVar; //導入依賴的package包/類
private void addToString(JDefinedClass jclass) {
Map<String, JFieldVar> fields = jclass.fields();
JMethod toString = jclass.method(JMod.PUBLIC, String.class, "toString");
Set<String> excludes = new HashSet<String>(Arrays.asList(ruleFactory.getGenerationConfig().getToStringExcludes()));
JBlock body = toString.body();
Class<?> toStringBuilder = ruleFactory.getGenerationConfig().isUseCommonsLang3() ? org.apache.commons.lang3.builder.ToStringBuilder.class : org.apache.commons.lang.builder.ToStringBuilder.class;
JClass toStringBuilderClass = jclass.owner().ref(toStringBuilder);
JInvocation toStringBuilderInvocation = JExpr._new(toStringBuilderClass).arg(JExpr._this());
if (!jclass._extends().fullName().equals(Object.class.getName())) {
toStringBuilderInvocation = toStringBuilderInvocation.invoke("appendSuper").arg(JExpr._super().invoke("toString"));
}
for (JFieldVar fieldVar : fields.values()) {
if (excludes.contains(fieldVar.name()) || (fieldVar.mods().getValue() & JMod.STATIC) == JMod.STATIC) {
continue;
}
toStringBuilderInvocation = toStringBuilderInvocation.invoke("append").arg(fieldVar.name()).arg(fieldVar);
}
body._return(toStringBuilderInvocation.invoke("toString"));
toString.annotate(Override.class);
}
示例9: addHashCode
import com.sun.codemodel.JFieldVar; //導入依賴的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);
}
示例10: propertyField
import com.sun.codemodel.JFieldVar; //導入依賴的package包/類
@Override
public void propertyField(JFieldVar field, JDefinedClass clazz, String propertyName, JsonNode propertyNode) {
field.annotate(JsonProperty.class).param("value", propertyName);
if (field.type().erasure().equals(field.type().owner().ref(Set.class))) {
field.annotate(JsonDeserialize.class).param("as", LinkedHashSet.class);
}
if (propertyNode.has("javaJsonView")) {
field.annotate(JsonView.class).param(
"value", field.type().owner().ref(propertyNode.get("javaJsonView").asText()));
}
if (propertyNode.has("description")) {
field.annotate(JsonPropertyDescription.class).param("value", propertyNode.get("description").asText());
}
}
示例11: timeField
import com.sun.codemodel.JFieldVar; //導入依賴的package包/類
@Override
public void timeField(JFieldVar field, JsonNode node) {
String pattern = null;
if (node.has("customTimePattern")) {
pattern = node.get("customTimePattern").asText();
} else if (node.has("customPattern")) {
pattern = node.get("customPattern").asText();
} else if (isNotEmpty(getGenerationConfig().getCustomTimePattern())) {
pattern = getGenerationConfig().getCustomTimePattern();
} else if (getGenerationConfig().isFormatDates()) {
pattern = FormatRule.ISO_8601_TIME_FORMAT;
}
if (pattern != null && !field.type().fullName().equals("java.lang.String")) {
field.annotate(JsonFormat.class).param("shape", JsonFormat.Shape.STRING).param("pattern", pattern);
}
}
示例12: dateTimeField
import com.sun.codemodel.JFieldVar; //導入依賴的package包/類
@Override
public void dateTimeField(JFieldVar field, JsonNode node) {
String timezone = node.has("customTimezone") ? node.get("customTimezone").asText() : "UTC";
String pattern = null;
if (node.has("customDateTimePattern")) {
pattern = node.get("customDateTimePattern").asText();
} else if (node.has("customPattern")) {
pattern = node.get("customPattern").asText();
} else if (isNotEmpty(getGenerationConfig().getCustomDateTimePattern())) {
pattern = getGenerationConfig().getCustomDateTimePattern();
} else if (getGenerationConfig().isFormatDateTimes()) {
pattern = FormatRule.ISO_8601_DATETIME_FORMAT;
}
if (pattern != null && !field.type().fullName().equals("java.lang.String")) {
field.annotate(JsonFormat.class).param("shape", JsonFormat.Shape.STRING).param("pattern", pattern).param("timezone", timezone);
}
}
示例13: create
import com.sun.codemodel.JFieldVar; //導入依賴的package包/類
public JFieldVar create(
final JDefinedClass instance,
final Iterable<Annotation> annotation,
final Type type,
final String name,
final Object value,
final boolean isImutable,
final boolean isNullable,
final boolean isPrimitivesEnabled,
final boolean isArrayNullable,
final boolean isCollectionNullable) throws CreationException {
final String fieldName = createFieldName(name);
final JType clazz = _class(type, isPrimitivesEnabled);
final JFieldVar field = isInstanceOfMap(clazz) //
? mapMember(instance, clazz, fieldName, type.generics())
: isInstanceOfList(clazz) //
? listMember(instance, clazz, fieldName, type.generics(), isNullable, isCollectionNullable)
: objectMember(instance, clazz, fieldName, value, isImutable, isArrayNullable);
annotate(field, annotation);
return field;
}
示例14: listMember
import com.sun.codemodel.JFieldVar; //導入依賴的package包/類
private JFieldVar listMember(
final JDefinedClass instance,
final JType clazz,
final String name,
final String[] generics,
final boolean isNullable,
final boolean isCollectionNullable) {
final JFieldVar field = instance.field(JMod.FINAL | JMod.PRIVATE, clazz, name);
if (isNullable && isCollectionNullable) {
return field;
}
final JType type = withoutGenerics(clazz.fullName()).startsWith(JAVA_UTIL_LIST)
? _type(JAVA_UTIL_ARRAYLIST, generics)
: clazz;
field.init(JExpr._new(type));
return field;
}
示例15: fields
import com.sun.codemodel.JFieldVar; //導入依賴的package包/類
public Map<Member, JFieldVar> fields(final JDefinedClass instance, final Bean configuration)
throws CreationException {
final Map<Member, JFieldVar> result = new LinkedHashMap<>();
for (final Member member : configuration.members()) {
if (!member.setter().isEnabled()) {
continue;
}
final JFieldVar field = this.memberFactory.create(
instance,
member.annotations(),
member.type(),
member.name(),
member.value(),
false,
member.isNullable(),
configuration.isPrimitivesEnabled(),
configuration.isArrayNullable(),
configuration.isCollectionNullable());
result.put(member, field);
}
return result;
}