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


Java JDefinedClass.method方法代碼示例

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


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

示例1: addFactoryMethod

import com.sun.codemodel.JDefinedClass; //導入方法依賴的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);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:27,代碼來源:EnumRule.java

示例2: addPublicGetMethod

import com.sun.codemodel.JDefinedClass; //導入方法依賴的package包/類
private JMethod addPublicGetMethod(JDefinedClass jclass, JMethod internalGetMethod, JFieldRef notFoundValue) {
    JMethod method = jclass.method(PUBLIC, jclass.owner()._ref(Object.class), GETTER_NAME);
    JTypeVar returnType = method.generify("T");
    method.type(returnType);
    Models.suppressWarnings(method, "unchecked");
    JVar nameParam = method.param(String.class, "name");
    JBlock body = method.body();
    JVar valueVar = body.decl(jclass.owner()._ref(Object.class), "value",
            invoke(internalGetMethod).arg(nameParam).arg(notFoundValue));
    JConditional found = method.body()._if(notFoundValue.ne(valueVar));
    found._then()._return(cast(returnType, valueVar));
    JBlock notFound = found._else();

    JMethod getAdditionalProperties = jclass.getMethod("getAdditionalProperties", new JType[] {});
    if (getAdditionalProperties != null) {
        notFound._return(cast(returnType, invoke(getAdditionalProperties).invoke("get").arg(nameParam)));
    } else {
        notFound._throw(illegalArgumentInvocation(jclass, nameParam));
    }

    return method;
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:23,代碼來源:DynamicPropertiesRule.java

示例3: addPublicSetMethod

import com.sun.codemodel.JDefinedClass; //導入方法依賴的package包/類
private JMethod addPublicSetMethod(JDefinedClass jclass, JMethod internalSetMethod) {
    JMethod method = jclass.method(PUBLIC, jclass.owner().VOID, SETTER_NAME);
    JVar nameParam = method.param(String.class, "name");
    JVar valueParam = method.param(Object.class, "value");
    JBlock body = method.body();
    JBlock notFound = body._if(JOp.not(invoke(internalSetMethod).arg(nameParam).arg(valueParam)))._then();

    // if we have additional properties, then put value.
    JMethod getAdditionalProperties = jclass.getMethod("getAdditionalProperties", new JType[] {});
    if (getAdditionalProperties != null) {
        JType additionalPropertiesType = ((JClass) (getAdditionalProperties.type())).getTypeParameters().get(1);
        notFound.add(invoke(getAdditionalProperties).invoke("put").arg(nameParam)
                .arg(cast(additionalPropertiesType, valueParam)));
    }
    // else throw exception.
    else {
        notFound._throw(illegalArgumentInvocation(jclass, nameParam));
    }

    return method;
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:22,代碼來源:DynamicPropertiesRule.java

示例4: addPublicWithMethod

import com.sun.codemodel.JDefinedClass; //導入方法依賴的package包/類
private JMethod addPublicWithMethod(JDefinedClass jclass, JMethod internalSetMethod) {
    JMethod method = jclass.method(PUBLIC, jclass, BUILDER_NAME);
    JVar nameParam = method.param(String.class, "name");
    JVar valueParam = method.param(Object.class, "value");
    JBlock body = method.body();
    JBlock notFound = body._if(JOp.not(invoke(internalSetMethod).arg(nameParam).arg(valueParam)))._then();

    // if we have additional properties, then put value.
    JMethod getAdditionalProperties = jclass.getMethod("getAdditionalProperties", new JType[] {});
    if (getAdditionalProperties != null) {
        JType additionalPropertiesType = ((JClass) (getAdditionalProperties.type())).getTypeParameters().get(1);
        notFound.add(invoke(getAdditionalProperties).invoke("put").arg(nameParam)
                .arg(cast(additionalPropertiesType, valueParam)));
    }
    // else throw exception.
    else {
        notFound._throw(illegalArgumentInvocation(jclass, nameParam));
    }
    body._return(_this());

    return method;
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:23,代碼來源:DynamicPropertiesRule.java

示例5: 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

示例6: addHashCode

import com.sun.codemodel.JDefinedClass; //導入方法依賴的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);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:27,代碼來源:ObjectRule.java

示例7: 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

示例8: addInternalGetMethodJava7

import com.sun.codemodel.JDefinedClass; //導入方法依賴的package包/類
private JMethod addInternalGetMethodJava7(JDefinedClass jclass, JsonNode propertiesNode) {
    JMethod method = jclass.method(PROTECTED, jclass.owner()._ref(Object.class), DEFINED_GETTER_NAME);
    JVar nameParam = method.param(String.class, "name");
    JVar notFoundParam = method.param(jclass.owner()._ref(Object.class), "notFoundValue");
    JBlock body = method.body();
    JSwitch propertySwitch = body._switch(nameParam);
    if (propertiesNode != null) {
        for (Iterator<Map.Entry<String, JsonNode>> properties = propertiesNode.fields(); properties.hasNext();) {
            Map.Entry<String, JsonNode> property = properties.next();
            String propertyName = property.getKey();
            JsonNode node = property.getValue();
            String fieldName = ruleFactory.getNameHelper().getPropertyName(propertyName, node);
            JType propertyType = jclass.fields().get(fieldName).type();

            addGetPropertyCase(jclass, propertySwitch, propertyName, propertyType, node);
        }
    }
    JClass extendsType = jclass._extends();
    if (extendsType != null && extendsType instanceof JDefinedClass) {
        JDefinedClass parentClass = (JDefinedClass) extendsType;
        JMethod parentMethod = parentClass.getMethod(DEFINED_GETTER_NAME,
                new JType[] { parentClass.owner()._ref(String.class), parentClass.owner()._ref(Object.class) });
        propertySwitch._default().body()
        ._return(_super().invoke(parentMethod).arg(nameParam).arg(notFoundParam));
    } else {
        propertySwitch._default().body()
        ._return(notFoundParam);
    }

    return method;
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:32,代碼來源:DynamicPropertiesRule.java

示例9: addInternalGetMethodJava6

import com.sun.codemodel.JDefinedClass; //導入方法依賴的package包/類
private JMethod addInternalGetMethodJava6(JDefinedClass jclass, JsonNode propertiesNode) {
    JMethod method = jclass.method(PROTECTED, jclass.owner()._ref(Object.class), DEFINED_GETTER_NAME);
    JVar nameParam = method.param(String.class, "name");
    JVar notFoundParam = method.param(jclass.owner()._ref(Object.class), "notFoundValue");
    JBlock body = method.body();
    JConditional propertyConditional = null;

    if (propertiesNode != null) {
        for (Iterator<Map.Entry<String, JsonNode>> properties = propertiesNode.fields(); properties.hasNext();) {
            Map.Entry<String, JsonNode> property = properties.next();
            String propertyName = property.getKey();
            JsonNode node = property.getValue();
            String fieldName = ruleFactory.getNameHelper().getPropertyName(propertyName, node);
            JType propertyType = jclass.fields().get(fieldName).type();

            JExpression condition = lit(propertyName).invoke("equals").arg(nameParam);
            if (propertyConditional == null) {
                propertyConditional = body._if(condition);
            } else {
                propertyConditional = propertyConditional._elseif(condition);
            }
            JMethod propertyGetter = jclass.getMethod(getGetterName(propertyName, propertyType, node), new JType[] {});
            propertyConditional._then()._return(invoke(propertyGetter));
        }
    }

    JClass extendsType = jclass._extends();
    JBlock lastBlock = propertyConditional == null ? body : propertyConditional._else();
    if (extendsType != null && extendsType instanceof JDefinedClass) {
        JDefinedClass parentClass = (JDefinedClass) extendsType;
        JMethod parentMethod = parentClass.getMethod(DEFINED_GETTER_NAME,
                new JType[] { parentClass.owner()._ref(String.class), parentClass.owner()._ref(Object.class) });
        lastBlock._return(_super().invoke(parentMethod).arg(nameParam).arg(notFoundParam));
    } else {
        lastBlock._return(notFoundParam);
    }

    return method;
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:40,代碼來源:DynamicPropertiesRule.java

示例10: addInternalSetMethodJava7

import com.sun.codemodel.JDefinedClass; //導入方法依賴的package包/類
private JMethod addInternalSetMethodJava7(JDefinedClass jclass, JsonNode propertiesNode) {
    JMethod method = jclass.method(PROTECTED, jclass.owner().BOOLEAN, DEFINED_SETTER_NAME);
    JVar nameParam = method.param(String.class, "name");
    JVar valueParam = method.param(Object.class, "value");
    JBlock body = method.body();
    JSwitch propertySwitch = body._switch(nameParam);
    if (propertiesNode != null) {
        for (Iterator<Map.Entry<String, JsonNode>> properties = propertiesNode.fields(); properties.hasNext();) {
            Map.Entry<String, JsonNode> property = properties.next();
            String propertyName = property.getKey();
            JsonNode node = property.getValue();
            String fieldName = ruleFactory.getNameHelper().getPropertyName(propertyName, node);
            JType propertyType = jclass.fields().get(fieldName).type();

            addSetPropertyCase(jclass, propertySwitch, propertyName, propertyType, valueParam, node);
        }
    }
    JBlock defaultBlock = propertySwitch._default().body();
    JClass extendsType = jclass._extends();
    if (extendsType != null && extendsType instanceof JDefinedClass) {
        JDefinedClass parentClass = (JDefinedClass) extendsType;
        JMethod parentMethod = parentClass.getMethod(DEFINED_SETTER_NAME,
                new JType[] { parentClass.owner()._ref(String.class), parentClass.owner()._ref(Object.class) });
        defaultBlock._return(_super().invoke(parentMethod).arg(nameParam).arg(valueParam));
    } else {
        defaultBlock._return(FALSE);
    }
    return method;
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:30,代碼來源:DynamicPropertiesRule.java

示例11: addInternalSetMethodJava6

import com.sun.codemodel.JDefinedClass; //導入方法依賴的package包/類
private JMethod addInternalSetMethodJava6(JDefinedClass jclass, JsonNode propertiesNode) {
    JMethod method = jclass.method(PROTECTED, jclass.owner().BOOLEAN, DEFINED_SETTER_NAME);
    JVar nameParam = method.param(String.class, "name");
    JVar valueParam = method.param(Object.class, "value");
    JBlock body = method.body();
    JConditional propertyConditional = null;
    if (propertiesNode != null) {
        for (Iterator<Map.Entry<String, JsonNode>> properties = propertiesNode.fields(); properties.hasNext();) {
            Map.Entry<String, JsonNode> property = properties.next();
            String propertyName = property.getKey();
            JsonNode node = property.getValue();
            String fieldName = ruleFactory.getNameHelper().getPropertyName(propertyName, node);
            JType propertyType = jclass.fields().get(fieldName).type();
            JExpression condition = lit(propertyName).invoke("equals").arg(nameParam);
            propertyConditional = propertyConditional == null ? propertyConditional = body._if(condition)
                    : propertyConditional._elseif(condition);

            JBlock callSite = propertyConditional._then();
            addSetProperty(jclass, callSite, propertyName, propertyType, valueParam, node);
            callSite._return(TRUE);
        }
    }
    JClass extendsType = jclass._extends();
    JBlock lastBlock = propertyConditional == null ? body : propertyConditional._else();

    if (extendsType != null && extendsType instanceof JDefinedClass) {
        JDefinedClass parentClass = (JDefinedClass) extendsType;
        JMethod parentMethod = parentClass.getMethod(DEFINED_SETTER_NAME,
                new JType[] { parentClass.owner()._ref(String.class), parentClass.owner()._ref(Object.class) });
        lastBlock._return(_super().invoke(parentMethod).arg(nameParam).arg(valueParam));
    } else {
        lastBlock._return(FALSE);
    }
    return method;
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:36,代碼來源:DynamicPropertiesRule.java

示例12: addToString

import com.sun.codemodel.JDefinedClass; //導入方法依賴的package包/類
private void addToString(JDefinedClass _enum, JFieldVar valueField) {
    JMethod toString = _enum.method(JMod.PUBLIC, String.class, "toString");
    JBlock body = toString.body();

    JExpression toReturn = JExpr._this().ref(valueField);
    if(!isString(valueField.type())){
        toReturn = toReturn.plus(JExpr.lit(""));
    }

    body._return(toReturn);

    toString.annotate(Override.class);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:14,代碼來源:EnumRule.java

示例13: addValueMethod

import com.sun.codemodel.JDefinedClass; //導入方法依賴的package包/類
private void addValueMethod(JDefinedClass _enum, JFieldVar valueField) {
    JMethod fromValue = _enum.method(JMod.PUBLIC, valueField.type(), "value");

    JBlock body = fromValue.body();
    body._return(JExpr._this().ref(valueField));

    ruleFactory.getAnnotator().enumValueMethod(fromValue);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:9,代碼來源:EnumRule.java

示例14: addGetter

import com.sun.codemodel.JDefinedClass; //導入方法依賴的package包/類
private JMethod addGetter(JDefinedClass c, JFieldVar field, String jsonPropertyName, JsonNode node) {
    JMethod getter = c.method(JMod.PUBLIC, field.type(), getGetterName(jsonPropertyName, field.type(), node));

    JBlock body = getter.body();
    body._return(field);

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

示例15: buildTemplateConstraintValidator

import com.sun.codemodel.JDefinedClass; //導入方法依賴的package包/類
private JDefinedClass buildTemplateConstraintValidator(String name, JDefinedClass constraint, Class<?> param) {
    try {
        JClass cv = (JClass) codeModel._ref(ConstraintValidator.class);
        cv = cv.narrow(constraint, (JClass) codeModel._ref(param));
        JDefinedClass validator = constraint._class(JMod.STATIC | JMod.PUBLIC, name);
        validator._implements(cv);
        validator.method(JMod.PUBLIC, void.class, "initialize").param(constraint, "parameters");
        JMethod isValid = validator.method(JMod.PUBLIC, boolean.class, "isValid");
        isValid.param(Object.class, "value");
        isValid.param(ConstraintValidatorContext.class, "context");
        return validator;
    } catch (JClassAlreadyExistsException e) {
        throw new RuntimeException("Tried to create an already existing class: " + name, e);
    }
}
 
開發者ID:hibernate,項目名稱:beanvalidation-benchmark,代碼行數:16,代碼來源:Jsr303Annotator.java


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