當前位置: 首頁>>代碼示例>>Java>>正文


Java JDefinedClass.field方法代碼示例

本文整理匯總了Java中com.sun.codemodel.JDefinedClass.field方法的典型用法代碼示例。如果您正苦於以下問題:Java JDefinedClass.field方法的具體用法?Java JDefinedClass.field怎麽用?Java JDefinedClass.field使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.sun.codemodel.JDefinedClass的用法示例。


在下文中一共展示了JDefinedClass.field方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: JavaBeanBasicField

import com.sun.codemodel.JDefinedClass; //導入方法依賴的package包/類
/**
 * Creates a random MetaField
 * 
 * @param owner
 *            The class that owns this field.
 * @param name
 *            The name of the meta field.
 */
public JavaBeanBasicField(MetaJavaBean owner, String name) {
    
    super(owner, name);
    
    this.basicType = BasicType.getRandom();

    // Generate the field declaration
    JDefinedClass ownerClass = owner.getGeneratedClass();
    this.generatedField = ownerClass.field(JMod.PRIVATE, basicType.getTypeClass(), name);
    
    // The getter
    getter = ownerClass.method(JMod.PUBLIC, basicType.getTypeClass(), "get" + name.substring(0, 1).toUpperCase() + name.substring(1));
    getter.body()._return(this.generatedField);
    
    // And the setter
    setter = ownerClass.method(JMod.PUBLIC, void.class, "set" + name.substring(0, 1).toUpperCase() + name.substring(1));
    JVar setterParam = setter.param(basicType.getTypeClass(), name);
    setter.body().assign(JExpr._this().ref(this.generatedField), setterParam);
}
 
開發者ID:hibernate,項目名稱:beanvalidation-benchmark,代碼行數:28,代碼來源:JavaBeanBasicField.java

示例2: JavaBeanRefField

import com.sun.codemodel.JDefinedClass; //導入方法依賴的package包/類
public JavaBeanRefField(MetaJavaBean owner, String name, MetaJavaBean refBean) {
    super(owner, name);
    
    this.refBean = refBean;
    
    // Generate the field declaration
    JDefinedClass ownerClass = owner.getGeneratedClass();
    this.generatedField = ownerClass.field(JMod.PRIVATE, refBean.getGeneratedClass(), name);
    
    // The getter
    getter = ownerClass.method(JMod.PUBLIC, refBean.getGeneratedClass(), "get"+name.substring(0, 1).toUpperCase()+name.substring(1));
    getter.body()._return(this.generatedField);
    
    // The setter
    setter = ownerClass.method(JMod.PUBLIC, void.class, "set"+name.substring(0, 1).toUpperCase()+name.substring(1));
    JVar setterParam = setter.param(refBean.getGeneratedClass(), name);
    setter.body().assign(JExpr._this().ref(this.generatedField), setterParam);
}
 
開發者ID:hibernate,項目名稱:beanvalidation-benchmark,代碼行數:19,代碼來源:JavaBeanRefField.java

示例3: addQuickLookupMap

import com.sun.codemodel.JDefinedClass; //導入方法依賴的package包/類
private JFieldVar addQuickLookupMap(JDefinedClass _enum, JType backingType) {

        JClass lookupType = _enum.owner().ref(Map.class).narrow(backingType.boxify(), _enum);
        JFieldVar lookupMap = _enum.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, lookupType, "CONSTANTS");

        JClass lookupImplType = _enum.owner().ref(HashMap.class).narrow(backingType.boxify(), _enum);
        lookupMap.init(JExpr._new(lookupImplType));

        JForEach forEach = _enum.init().forEach(_enum, "c", JExpr.invoke("values"));
        JInvocation put = forEach.body().invoke(lookupMap, "put");
        put.arg(forEach.var().ref("value"));
        put.arg(forEach.var());

        return lookupMap;
    }
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:16,代碼來源:EnumRule.java

示例4: addValueField

import com.sun.codemodel.JDefinedClass; //導入方法依賴的package包/類
private JFieldVar addValueField(JDefinedClass _enum, JType type) {
    JFieldVar valueField = _enum.field(JMod.PRIVATE | JMod.FINAL, type, VALUE_FIELD_NAME);

    JMethod constructor = _enum.constructor(JMod.PRIVATE);
    JVar valueParam = constructor.param(type, VALUE_FIELD_NAME);
    JBlock body = constructor.body();
    body.assign(JExpr._this().ref(valueField), valueParam);

    return valueField;
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:11,代碼來源:EnumRule.java

示例5: generatePopulationCode

import com.sun.codemodel.JDefinedClass; //導入方法依賴的package包/類
/**
 * Generates a Holder class that will hold an {@link ArrayList} with all the
 * first level beans in the graph (contents of {@link #beans}) on it.
 */
private void generatePopulationCode() {
    try {
        // Generate the holder class
        JDefinedClass holderClass = cm._class(Config.CFG.getBasePackageName() + ".Holder");
        JClass alObject = (JClass) cm._ref(ArrayList.class);
        alObject = alObject.narrow(Object.class);
        JFieldVar beansField = holderClass.field(JMod.PUBLIC, alObject, "beans", JExpr._new(alObject));
        JMethod defConstructor = holderClass.constructor(JMod.PUBLIC);
        JBlock body = defConstructor.body();

        // Init the beans and add them to the array
        for (MetaJavaBean mjb : beans) {
            mjb.generateStaticInitCode();
            JVar beanDecl = generateBeanNonStaticInitCode(mjb, body, 0);
            body.add(beansField.invoke("add").arg(beanDecl));
        }

        // Init the base beans but don't add them to the array
        for (MetaJavaBean bmjb : baseBeans) {
            bmjb.generateStaticInitCode();
        }

    } catch (JClassAlreadyExistsException e) {
        throw new RuntimeException("Error generating the holder class.", e);
    }
}
 
開發者ID:hibernate,項目名稱:beanvalidation-benchmark,代碼行數:31,代碼來源:Generator.java

示例6: getOrAddNotFoundVar

import com.sun.codemodel.JDefinedClass; //導入方法依賴的package包/類
JFieldRef getOrAddNotFoundVar(JDefinedClass jclass) {
    jclass.field(PROTECTED | STATIC | FINAL, Object.class, NOT_FOUND_VALUE_FIELD,
            _new(jclass.owner()._ref(Object.class)));
    return jclass.staticRef(NOT_FOUND_VALUE_FIELD);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:6,代碼來源:DynamicPropertiesRule.java

示例7: apply

import com.sun.codemodel.JDefinedClass; //導入方法依賴的package包/類
/**
 * Applies this schema rule to take the required code generation steps.
 * <p>
 * This rule adds a property to a given Java class according to the Java
 * Bean spec. A private field is added to the class, along with accompanying
 * accessor methods.
 * <p>
 * If this rule's schema mapper is configured to include builder methods
 * (see {@link GenerationConfig#isGenerateBuilders()} ),
 * then a builder method of the form <code>withFoo(Foo foo);</code> is also
 * added.
 *
 * @param nodeName
 *            the name of the property to be applied
 * @param node
 *            the node describing the characteristics of this property
 * @param jclass
 *            the Java class which should have this property added
 * @return the given jclass
 */
@Override
public JDefinedClass apply(String nodeName, JsonNode node, JDefinedClass jclass, Schema schema) {
    String propertyName = ruleFactory.getNameHelper().getPropertyName(nodeName, node);

    JType propertyType = ruleFactory.getSchemaRule().apply(nodeName, node, jclass, schema);

    boolean isIncludeAccessors = ruleFactory.getGenerationConfig().isIncludeAccessors();
    boolean isIncludeGetters = ruleFactory.getGenerationConfig().isIncludeGetters();
    boolean isIncludeSetters = ruleFactory.getGenerationConfig().isIncludeSetters();

    node = resolveRefs(node, schema);

    int accessModifier = isIncludeAccessors || isIncludeGetters || isIncludeSetters ? JMod.PRIVATE : JMod.PUBLIC;
    JFieldVar field = jclass.field(accessModifier, propertyType, propertyName);

    propertyAnnotations(nodeName, node, schema, field);

    formatAnnotation(field, node);

    ruleFactory.getAnnotator().propertyField(field, jclass, nodeName, node);

    if (isIncludeAccessors || isIncludeGetters) {
        JMethod getter = addGetter(jclass, field, nodeName, node);
        ruleFactory.getAnnotator().propertyGetter(getter, nodeName);
        propertyAnnotations(nodeName, node, schema, getter);
    }

    if (isIncludeAccessors || isIncludeSetters) {
        JMethod setter = addSetter(jclass, field, nodeName, node);
        ruleFactory.getAnnotator().propertySetter(setter, nodeName);
        propertyAnnotations(nodeName, node, schema, setter);
    }

    if (ruleFactory.getGenerationConfig().isGenerateBuilders()) {
        addBuilder(jclass, field);
    }

    if (node.has("pattern")) {
        ruleFactory.getPatternRule().apply(nodeName, node.get("pattern"), field, schema);
    }

    ruleFactory.getDefaultRule().apply(nodeName, node.get("default"), field, schema);

    ruleFactory.getMinimumMaximumRule().apply(nodeName, node, field, schema);

    ruleFactory.getMinItemsMaxItemsRule().apply(nodeName, node, field, schema);

    ruleFactory.getMinLengthMaxLengthRule().apply(nodeName, node, field, schema);

    if (isObject(node) || isArray(node)) {
        ruleFactory.getValidRule().apply(nodeName, node, field, schema);
    }

    return jclass;
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:76,代碼來源:PropertyRule.java

示例8: shouldUpdateJavaDoc

import com.sun.codemodel.JDefinedClass; //導入方法依賴的package包/類
@Test
public void shouldUpdateJavaDoc() throws JClassAlreadyExistsException {
    JDefinedClass jclass = new JCodeModel()._class(TARGET_CLASS_NAME);

    jclass.field(JMod.PRIVATE, jclass.owner().ref(String.class), "fooBar");
    jclass.field(JMod.PRIVATE, jclass.owner().ref(String.class), "foo");

    ObjectMapper mapper = new ObjectMapper();
    ArrayNode requiredNode = mapper.createArrayNode().add("fooBar");

    rule.apply("Class", requiredNode, jclass, new Schema(null, requiredNode, requiredNode));

    JDocComment fooBarJavaDoc = jclass.fields().get("fooBar").javadoc();
    JDocComment fooJavaDoc = jclass.fields().get("foo").javadoc();


    assertThat(fooBarJavaDoc.size(), is(1));
    assertThat((String) fooBarJavaDoc.get(0), is("\n(Required)"));

    assertThat(fooJavaDoc.size(), is(0));
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:22,代碼來源:RequiredArrayRuleTest.java

示例9: shouldUpdateAnnotations

import com.sun.codemodel.JDefinedClass; //導入方法依賴的package包/類
@Test
public void shouldUpdateAnnotations() throws JClassAlreadyExistsException {
    setupRuleFactoryToIncludeJsr303();
    
    JDefinedClass jclass = new JCodeModel()._class(TARGET_CLASS_NAME);

    jclass.field(JMod.PRIVATE, jclass.owner().ref(String.class), "fooBar");
    jclass.field(JMod.PRIVATE, jclass.owner().ref(String.class), "foo");

    ObjectMapper mapper = new ObjectMapper();
    ArrayNode requiredNode = mapper.createArrayNode().add("foo_bar");

    rule.apply("Class", requiredNode, jclass, new Schema(null, requiredNode, requiredNode));

    Collection<JAnnotationUse> fooBarAnnotations = jclass.fields().get("fooBar").annotations();
    Collection<JAnnotationUse> fooAnnotations = jclass.fields().get("foo").annotations();

    assertThat(fooBarAnnotations.size(), is(1));
    assertThat(fooBarAnnotations.iterator().next().getAnnotationClass().name(), is(NotNull.class.getSimpleName()));

    assertThat(fooAnnotations.size(), is(0));
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:23,代碼來源:RequiredArrayRuleTest.java

示例10: addAdditionalPropertiesField

import com.sun.codemodel.JDefinedClass; //導入方法依賴的package包/類
private JFieldVar addAdditionalPropertiesField(JDefinedClass jclass, JType propertyType) {
    JClass propertiesMapType = jclass.owner().ref(Map.class);
    propertiesMapType = propertiesMapType.narrow(jclass.owner().ref(String.class), propertyType.boxify());

    JClass propertiesMapImplType = jclass.owner().ref(HashMap.class);
    propertiesMapImplType = propertiesMapImplType.narrow(jclass.owner().ref(String.class), propertyType.boxify());

    JFieldVar field = jclass.field(JMod.PRIVATE, propertiesMapType, "additionalProperties");

    ruleFactory.getAnnotator().additionalPropertiesField(field, jclass, "additionalProperties");

    field.init(JExpr._new(propertiesMapImplType));

    return field;
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:16,代碼來源:AdditionalPropertiesRule.java


注:本文中的com.sun.codemodel.JDefinedClass.field方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。