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


Java JDefinedClass類代碼示例

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


JDefinedClass類屬於com.sun.codemodel包,在下文中一共展示了JDefinedClass類的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: 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

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

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

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

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

示例7: addEnumConstants

import com.sun.codemodel.JDefinedClass; //導入依賴的package包/類
private void addEnumConstants(JsonNode node, JDefinedClass _enum, JsonNode customNames, JType type) {
    Collection<String> existingConstantNames = new ArrayList<String>();
    for (int i = 0; i < node.size(); i++) {
        JsonNode value = node.path(i);

        if (!value.isNull()) {
            String constantName = getConstantName(value.asText(), customNames.path(i).asText());
            constantName = makeUnique(constantName, existingConstantNames);
            existingConstantNames.add(constantName);

            JEnumConstant constant = _enum.enumConstant(constantName);
            constant.arg(DefaultRule.getDefaultValue(type, value));
            ruleFactory.getAnnotator().enumConstant(constant, value.asText());
        }
    }
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:17,代碼來源:EnumRule.java

示例8: ClassGenerator

import com.sun.codemodel.JDefinedClass; //導入依賴的package包/類
@SuppressWarnings("unchecked")
ClassGenerator(CodeGenerator<T> codeGenerator, MappingSet mappingSet, SignatureHolder signature, EvaluationVisitor eval, JDefinedClass clazz, JCodeModel model) throws JClassAlreadyExistsException {
  this.codeGenerator = codeGenerator;
  this.clazz = clazz;
  this.mappings = mappingSet;
  this.sig = signature;
  this.evaluationVisitor = eval;
  this.model = model;
  blocks = (LinkedList<JBlock>[]) new LinkedList[sig.size()];
  for (int i =0; i < sig.size(); i++) {
    blocks[i] = Lists.newLinkedList();
  }
  rotateBlock();

  for (SignatureHolder child : signature.getChildHolders()) {
    String innerClassName = child.getSignatureClass().getSimpleName();
    JDefinedClass innerClazz = clazz._class(Modifier.FINAL + Modifier.PRIVATE, innerClassName);
    innerClasses.put(innerClassName, new ClassGenerator<>(codeGenerator, mappingSet, child, eval, innerClazz, model));
  }
}
 
開發者ID:skhalifa,項目名稱:QDrill,代碼行數:21,代碼來源:ClassGenerator.java

示例9: apply

import com.sun.codemodel.JDefinedClass; //導入依賴的package包/類
/**
 * Applies this schema rule to take the required code generation steps.
 * <p>
 * For each property present within the properties node, this rule will
 * invoke the 'property' rule provided by the given schema mapper.
 *
 * @param nodeName
 *            the name of the node for which properties are being added
 * @param node
 *            the properties node, containing property names and their
 *            definition
 * @param jclass
 *            the Java type which will have the given properties added
 * @return the given jclass
 */
@Override
public JDefinedClass apply(String nodeName, JsonNode node, JDefinedClass jclass, Schema schema) {
    if (node == null) {
        node = JsonNodeFactory.instance.objectNode();
    }

    for (Iterator<String> properties = node.fieldNames(); properties.hasNext(); ) {
        String property = properties.next();

        ruleFactory.getPropertyRule().apply(property, node.get(property), jclass, schema);
    }

    if (ruleFactory.getGenerationConfig().isGenerateBuilders() && !jclass._extends().name().equals("Object")) {
        addOverrideBuilders(jclass, jclass.owner()._getClass(jclass._extends().fullName()));
    }

    ruleFactory.getAnnotator().propertyOrder(jclass, node);

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

示例10: addToString

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

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

示例12: propertyField

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

示例13: CodeGenerator

import com.sun.codemodel.JDefinedClass; //導入依賴的package包/類
CodeGenerator(MappingSet mappingSet, TemplateClassDefinition<T> definition,
    FunctionImplementationRegistry funcRegistry) {
  Preconditions.checkNotNull(definition.getSignature(),
      "The signature for defintion %s was incorrectly initialized.", definition);
  this.definition = definition;
  this.className = definition.getExternalInterface().getSimpleName() + "Gen" + definition.getNextClassNumber();
  this.fqcn = PACKAGE_NAME + "." + className;
  try {
    this.model = new JCodeModel();
    JDefinedClass clazz = model._package(PACKAGE_NAME)._class(className);
    rootGenerator = new ClassGenerator<>(this, mappingSet, definition.getSignature(), new EvaluationVisitor(
        funcRegistry), clazz, model);
  } catch (JClassAlreadyExistsException e) {
    throw new IllegalStateException(e);
  }
}
 
開發者ID:skhalifa,項目名稱:QDrill,代碼行數:17,代碼來源:CodeGenerator.java

示例14: buildGroupSequenceAnnot

import com.sun.codemodel.JDefinedClass; //導入依賴的package包/類
public MetaAnnotation buildGroupSequenceAnnot() {
    if ( this.groups.isEmpty() ) {
        return null; // No group sequence needed
    }
    else {
        // Build the array of groups
        JDefinedClass[] gs = new JDefinedClass[groups.size()+1];
        int i=0;
        for ( MetaGroup group : groups ) {
            gs[i++] = group.getGeneratedClass();
        }
        gs[i] = getGeneratedClass(); // Add the class itself (acts as Default)
        HashMap<String,Object> gsParams = Maps.newHashMap();
        gsParams.put("value", gs);
        MetaAnnotation gsAnnot = new MetaAnnotation(getGeneratedClass().owner(), GroupSequence.class, AnnotationType.OTHER, gsParams);
        return gsAnnot;
    }
}
 
開發者ID:hibernate,項目名稱:beanvalidation-benchmark,代碼行數:19,代碼來源:MetaJavaBean.java

示例15: buildClassAnnotations

import com.sun.codemodel.JDefinedClass; //導入依賴的package包/類
/**
 * @return A list of the class level annotations that the annotator will
 *         use.
 */
private List<MetaAnnotation> buildClassAnnotations() {

    List<MetaAnnotation> anns = Lists.newArrayList();
    HashMap<String, Object> annotParams;

    // AlwaysValid
    JDefinedClass alwaysValid = buildTemplateConstraint("AlwaysValid");
    JDefinedClass alwaysValidValidator = buildTemplateConstraintValidator("AlwaysValidValidator", alwaysValid, Object.class);
    JMethod isValid = getIsValidMethod(alwaysValidValidator);
    isValid.body()._return(JExpr.TRUE);
    alwaysValid.annotate(Constraint.class).param("validatedBy", alwaysValidValidator);
    
    annotParams = Maps.newHashMap();
    anns.add(new MetaAnnotation(alwaysValid, AnnotationType.JSR_303, annotParams));
    
    return anns;
}
 
開發者ID:hibernate,項目名稱:beanvalidation-benchmark,代碼行數:22,代碼來源:Jsr303Annotator.java


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