com.sun.codemodel.JType Java Examples
The following examples show how to use
com.sun.codemodel.JType.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CodeModelUtils.java From jaxb2-basics with BSD 2-Clause "Simplified" License | 6 votes |
public static JMethod getMethod(JDefinedClass theClass, String name, JType[] arguments) { final JMethod method = theClass.getMethod(name, arguments); if (method != null) { return method; } else { final JClass draftSuperClass = theClass._extends(); if (draftSuperClass == null || !(draftSuperClass instanceof JDefinedClass)) { return null; } else { final JDefinedClass superClass = (JDefinedClass) draftSuperClass; return getMethod(superClass, name, arguments); } } }
Example #2
Source File: TypeToJTypeConvertingVisitor.java From jaxb2-basics with BSD 2-Clause "Simplified" License | 6 votes |
@Override public JType visit(PrimitiveType type, JCodeModel codeModel) { switch (type.getType()) { case Boolean: return codeModel.BOOLEAN; case Char: return codeModel.CHAR; case Byte: return codeModel.BYTE; case Short: return codeModel.SHORT; case Int: return codeModel.INT; case Long: return codeModel.LONG; case Float: return codeModel.FLOAT; case Double: return codeModel.DOUBLE; default: throw new AssertionError("Unknown primitive type [" + type.getType() + "]"); } }
Example #3
Source File: PluginImpl.java From immutable-xjc with MIT License | 6 votes |
private JExpression getDefensiveCopyExpression(JCodeModel codeModel, JType jType, JVar param) { List<JClass> typeParams = ((JClass) jType).getTypeParameters(); JClass typeParameter = null; if (typeParams.iterator().hasNext()) { typeParameter = typeParams.iterator().next(); } JClass newClass = null; if (param.type().erasure().equals(codeModel.ref(Collection.class))) { newClass = codeModel.ref(ArrayList.class); } else if (param.type().erasure().equals(codeModel.ref(List.class))) { newClass = codeModel.ref(ArrayList.class); } else if (param.type().erasure().equals(codeModel.ref(Map.class))) { newClass = codeModel.ref(HashMap.class); } else if (param.type().erasure().equals(codeModel.ref(Set.class))) { newClass = codeModel.ref(HashSet.class); } else if (param.type().erasure().equals(codeModel.ref(SortedMap.class))) { newClass = codeModel.ref(TreeMap.class); } else if (param.type().erasure().equals(codeModel.ref(SortedSet.class))) { newClass = codeModel.ref(TreeSet.class); } if (newClass != null && typeParameter != null) { newClass = newClass.narrow(typeParameter); } return newClass == null ? JExpr._null() : JExpr._new(newClass).arg(param); }
Example #4
Source File: SingleWrappingReferenceField.java From hyperjaxb3 with BSD 2-Clause "Simplified" License | 6 votes |
@Override protected JExpression unwrap(JExpression source) { final JType declaredType = getDeclaredType(); final JClass elementClass = codeModel.ref(JAXBElement.class).narrow( declaredType.boxify().wildcard()); // TODO remove if cast is not necessary final JExpression value = JExpr.cast(elementClass, source); if (xmlAdapterClass == null) { return XmlAdapterXjcUtils.unmarshallJAXBElement(codeModel, value); } else { return XmlAdapterXjcUtils.unmarshallJAXBElement(codeModel, xmlAdapterClass, value); } }
Example #5
Source File: TypeToJTypeConvertingVisitor.java From jaxb2-basics with BSD 2-Clause "Simplified" License | 6 votes |
@Override public JType visit(WildcardType type, JCodeModel codeModel) { if (type.getExtends() != null) { final ReferenceType _extends = type.getExtends(); final JType boundType = _extends.accept(this, codeModel); if (!(boundType instanceof JClass)) { throw new IllegalArgumentException("Bound type [" + _extends + "]in the wildcard type must be class."); } final JClass boundClass = (JClass) boundType; return boundClass.wildcard(); } else if (type.getSuper() != null) { // TODO throw new IllegalArgumentException( "Wildcard types with super clause are not supported at the moment."); } else { throw new IllegalArgumentException( "Wildcard type must have either extends or super clause."); } }
Example #6
Source File: ClassGenerator.java From Bats with Apache License 2.0 | 6 votes |
/** * declare a constant field for the class. * argument {@code function} holds the constant value which * returns a value holder must be set to the class field when the class instance created. * the class field innerClassField will be created if innerClassGenerator exists. * * @param prefix the prefix name of class field * @param t the type of class field * @param init init expression * @param function the function holds the constant value * @return the depth of nested class, class field */ public Pair<Integer, JVar> declareClassConstField(String prefix, JType t, JExpression init, Function<DrillBuf, ? extends ValueHolder> function) { JVar var; int depth = 1; if (innerClassGenerator != null) { Pair<Integer, JVar> nested = innerClassGenerator.declareClassConstField(prefix, t, init, function); depth = nested.getKey() + 1; var = nested.getValue(); } else { var = clazz.field(JMod.NONE, t, prefix + index++, init); } Pair<Integer, JVar> depthVar = Pair.of(depth, var); constantVars.put(depthVar, function); return depthVar; }
Example #7
Source File: JTypeUtils.java From hyperjaxb3 with BSD 2-Clause "Simplified" License | 6 votes |
public static JType[] getBasicTypes(final JCodeModel codeModel) { final JType[] basicTypes = new JType[] { codeModel.BOOLEAN, codeModel.BOOLEAN.boxify(), codeModel.BYTE, codeModel.BYTE.boxify(), codeModel.CHAR, codeModel.CHAR.boxify(), codeModel.DOUBLE, codeModel.DOUBLE.boxify(), codeModel.FLOAT, codeModel.FLOAT.boxify(), codeModel.INT, codeModel.INT.boxify(), codeModel.LONG, codeModel.LONG.boxify(), codeModel.SHORT, codeModel.SHORT.boxify(), codeModel.ref(String.class), codeModel.ref(BigInteger.class), codeModel.ref(BigDecimal.class), codeModel.ref(java.util.Date.class), codeModel.ref(java.util.Calendar.class), codeModel.ref(java.sql.Date.class), codeModel.ref(java.sql.Time.class), codeModel.ref(java.sql.Timestamp.class), codeModel.BYTE.array(), codeModel.BYTE.boxify().array(), codeModel.CHAR.array(), codeModel.CHAR.boxify().array() } ; return basicTypes; }
Example #8
Source File: EqualsArguments.java From jaxb2-basics with BSD 2-Clause "Simplified" License | 6 votes |
public EqualsArguments property(JBlock block, String propertyName, String propertyMethod, JType declarablePropertyType, JType propertyType, Collection<JType> possiblePropertyTypes) { final JVar leftPropertyValue = block.decl(JMod.FINAL, declarablePropertyType, leftValue().name() + propertyName, leftValue().invoke(propertyMethod)); final JVar rightPropertyValue = block.decl(JMod.FINAL, declarablePropertyType, rightValue().name() + propertyName, rightValue().invoke(propertyMethod)); // We assume that primitive properties are always set boolean isAlwaysSet = propertyType.isPrimitive(); final JExpression leftPropertyHasSetValue = isAlwaysSet ? JExpr.TRUE : leftPropertyValue.ne(JExpr._null()); final JExpression rightPropertyHasSetValue = isAlwaysSet ? JExpr.TRUE : rightPropertyValue.ne(JExpr._null()); return spawn(leftPropertyValue, leftPropertyHasSetValue, rightPropertyValue, rightPropertyHasSetValue); }
Example #9
Source File: RamlJavaClientGenerator.java From raml-java-client-generator with Apache License 2.0 | 6 votes |
private JType getOrGeneratePojoFromJsonSchema(JCodeModel cm, String resourcePath, MimeType mimeType, String className) throws IOException { JType type = null; if (StringUtils.isNotBlank(mimeType.getSchema())) { if (globalTypes.containsKey(mimeType.getSchema())) { type = globalTypes.get(mimeType.getSchema()); } else { type = generatePojoFromSchema(cm, className, getModelPackage(resourcePath), mimeType.getSchema(), mimeType.getSchemaPath(), SourceType.JSONSCHEMA); } } else if (StringUtils.isNotBlank(mimeType.getExample())) { type = generatePojoFromSchema(cm, className, getModelPackage(resourcePath), mimeType.getExample(), SourceType.JSON); } if (type != null && type.fullName().equals("java.lang.Object") && StringUtils.isNotBlank(mimeType.getExample())) { type = generatePojoFromSchema(cm, className, getModelPackage(resourcePath), mimeType.getExample(), SourceType.JSON); } if (type == null) { type = cm.ref(Object.class); } return type; }
Example #10
Source File: AnnotateOutline.java From hyperjaxb3 with BSD 2-Clause "Simplified" License | 6 votes |
private void processAttributeWildcard(ClassOutline classOutline) { logger.debug("The class [" + OutlineUtils.getClassName(classOutline) + "] declares an attribute wildcard which will be made transient."); String FIELD_NAME = "otherAttributes"; String METHOD_SEED = classOutline.parent().getModel() .getNameConverter().toClassName(FIELD_NAME); final JMethod getOtherAttributesMethod = classOutline.ref.getMethod( "get" + METHOD_SEED, new JType[0]); if (getOtherAttributesMethod == null) { logger.error("Could not find the attribute wildcard method in the class [" + OutlineUtils.getClassName(classOutline) + "]."); } else { getOtherAttributesMethod.annotate(Transient.class); } }
Example #11
Source File: ClassModelBuilder.java From mxjc with MIT License | 5 votes |
/** * Build class name to element name mapping * * @param outline, JAXB schema/code model * @return class name to element name map */ private static Map<String, QName> buildClass2ElementMapping(Outline outline) { Map<String, QName> mapping = new HashMap<String, QName>(); for(CElementInfo ei : outline.getModel().getAllElements()) { JType exposedType = ei.getContentInMemoryType().toType(outline,Aspect.EXPOSED); mapping.put(exposedType.fullName(), ei.getElementName()); } return mapping; }
Example #12
Source File: BuilderGenerator.java From jaxb2-rich-contract-plugin with MIT License | 5 votes |
void generateArrayProperty(final JBlock initBody, final JVar productParam, final PropertyOutline fieldOutline, final JType elementType, final JType builderType) { final String fieldName = fieldOutline.getFieldName(); final String propertyName = fieldOutline.getBaseName(); final JType fieldType = fieldOutline.getRawType(); final JMethod withVarargsMethod = this.builderClass.raw.method(JMod.PUBLIC, builderType, PluginContext.WITH_METHOD_PREFIX + propertyName); final JVar withVarargsParam = withVarargsMethod.varParam(elementType, fieldName); if (this.implement) { final JFieldVar builderField = this.builderClass.raw.field(JMod.PRIVATE, fieldType, fieldName, JExpr._null()); withVarargsMethod.body().assign(JExpr._this().ref(builderField), withVarargsParam); withVarargsMethod.body()._return(JExpr._this()); initBody.assign(productParam.ref(fieldName), JExpr._this().ref(builderField)); } }
Example #13
Source File: DefinedPropertyOutline.java From jaxb2-rich-contract-plugin with MIT License | 5 votes |
@Override public JType getElementType() { if (isCollection() && !getRawType().isArray()) { return ((JClass) getRawType()).getTypeParameters().get(0); } else { return getRawType(); } }
Example #14
Source File: MetaPlugin.java From jaxb2-rich-contract-plugin with MIT License | 5 votes |
private void generateAccessors(final FieldOutline fieldOutline, final String propertyName, final JType returnType, final JDefinedClass declaringClass, final F1<JExpression, JVar> getMaker, final F3<JExpression, JBlock, JVar, JVar> setMaker) { final String constantName = getConstantName(fieldOutline); final JMethod getMethod = declaringClass.method(JMod.PUBLIC, returnType, "get"); getMethod.annotate(Override.class); final JVar instanceParam = getMethod.param(JMod.FINAL, fieldOutline.parent().implClass, "_instance_"); getMethod.body()._return(JOp.cond(instanceParam.eq(JExpr._null()), JExpr._null(), getMaker.f(instanceParam))); final JMethod setMethod = declaringClass.method(JMod.PUBLIC, void.class, "set"); setMethod.annotate(Override.class); final JVar setInstanceParam = setMethod.param(JMod.FINAL, fieldOutline.parent().implClass, "_instance_"); final JVar valueParam = setMethod.param(JMod.FINAL, returnType, "_value_"); if (constantName == null) { final JConditional ifNotNull = setMethod.body()._if(setInstanceParam.ne(JExpr._null())); setMaker.f(ifNotNull._then(), setInstanceParam, valueParam); } }
Example #15
Source File: Translator.java From groovy-cps with Apache License 2.0 | 5 votes |
private JType primitive(Object src, TypeKind k) { switch (k) { case BOOLEAN: return codeModel.BOOLEAN; case BYTE: return codeModel.BYTE; case SHORT: return codeModel.SHORT; case INT: return codeModel.INT; case LONG: return codeModel.LONG; case CHAR: return codeModel.CHAR; case FLOAT: return codeModel.FLOAT; case DOUBLE: return codeModel.DOUBLE; case VOID: return codeModel.VOID; } throw new UnsupportedOperationException(src.toString()); }
Example #16
Source File: SelectorGenerator.java From jaxb2-rich-contract-plugin with MIT License | 5 votes |
public void generateMetaFields() { if(this.classOutline.getSuperClass() != null) { this.selectorClass._extends(this.selectorGenerator.getInfoClass(this.classOutline.getSuperClass().implClass).selectorClass.narrow(this.rootTypeParam).narrow(this.parentTypeParam)); } for (final FieldOutline fieldOutline : this.classOutline.getDeclaredFields()) { final JFieldVar definedField = PluginUtil.getDeclaredField(fieldOutline); if (definedField != null) { final JType elementType = PluginUtil.getElementType(fieldOutline); if (elementType.isReference()) { final ClassOutline modelClass = this.selectorGenerator.getPluginContext().getClassOutline(elementType); final JClass returnType; if (modelClass != null) { returnType = this.selectorGenerator.getInfoClass(modelClass.implClass).selectorClass.narrow(this.rootTypeParam).narrow(this.selectorClass.narrow(this.rootTypeParam).narrow(this.parentTypeParam)); } else { returnType = this.selectorGenerator.getPluginContext().codeModel.ref(this.selectorGenerator.selectorBaseClass).narrow(this.rootTypeParam).narrow(this.selectorClass.narrow(this.rootTypeParam).narrow(this.parentTypeParam)); } final JFieldVar includeField = this.selectorClass.field(JMod.PRIVATE, returnType, definedField.name(), JExpr._null()); final JFieldRef fieldRef = JExpr._this().ref(includeField); final JMethod includeMethod = this.selectorClass.method(JMod.PUBLIC, returnType, definedField.name()); if(this.selectorGenerator.selectorParamName != null) { final JVar includeParam = includeMethod.param(JMod.FINAL, this.selectorGenerator.getSelectorParamType(this.classOutline.implClass, elementType), this.selectorGenerator.selectorParamName); includeMethod.body()._return(JOp.cond(fieldRef.eq(JExpr._null()), fieldRef.assign(JExpr._new(returnType).arg(JExpr._this().ref("_root")).arg(JExpr._this()).arg(JExpr.lit(definedField.name())).arg(includeParam)), fieldRef)); } else { includeMethod.body()._return(JOp.cond(fieldRef.eq(JExpr._null()), fieldRef.assign(JExpr._new(returnType).arg(JExpr._this().ref("_root")).arg(JExpr._this()).arg(JExpr.lit(definedField.name()))), fieldRef)); } this.buildChildrenMethod.body()._if(fieldRef.ne(JExpr._null()))._then().add(this.productMapVar.invoke("put").arg(JExpr.lit(definedField.name())).arg(fieldRef.invoke("init"))); } } } this.buildChildrenMethod.body()._return(this.productMapVar); }
Example #17
Source File: MetaPlugin.java From jaxb2-rich-contract-plugin with MIT License | 5 votes |
private JExpression dotClass(final JType cl) { if (cl instanceof JClass) { return ((JClass)cl).dotclass(); } else { return new JExpressionImpl() { public void generate(final JFormatter f) { f.g(cl).p(".class"); } }; } }
Example #18
Source File: EvaluationVisitor.java From dremio-oss with Apache License 2.0 | 5 votes |
@Override public HoldingContainer visitQuotedStringConstant(QuotedString e, ClassGenerator<?> generator) throws RuntimeException { CompleteType completeType = CompleteType.VARCHAR; JBlock setup = generator.getBlock(BlockType.SETUP); JType holderType = CodeModelArrowHelper.getHolderType(completeType, generator.getModel()); JVar var = generator.declareClassField("string", holderType); JExpression stringLiteral = JExpr.lit(e.value); JExpression buffer = JExpr.direct("context").invoke("getManagedBuffer"); setup.assign(var, generator.getModel().ref(ValueHolderHelper.class).staticInvoke("getNullableVarCharHolder").arg(buffer).arg(stringLiteral)); return new HoldingContainer((completeType), var, var.ref("value"), var.ref("isSet")); }
Example #19
Source File: BaseFunctionHolder.java From dremio-oss with Apache License 2.0 | 5 votes |
protected void addProtectedBlock(ClassGenerator<?> g, JBlock sub, String body, HoldingContainer[] inputVariables, JVar[] workspaceJVars, boolean decConstInputOnly) { if (inputVariables != null) { for (int i = 0; i < inputVariables.length; i++) { if (decConstInputOnly && !inputVariables[i].isConstant()) { continue; } ValueReference parameter = parameters[i]; HoldingContainer inputVariable = inputVariables[i]; if (parameter.isFieldReader && !inputVariable.isReader() && inputVariable.getCompleteType().isScalar()) { JType singularReaderClass = g.getModel()._ref(TypeHelper.getHolderReaderImpl(getArrowMinorType(inputVariable.getCompleteType().toMinorType()))); JType fieldReadClass = g.getModel()._ref(FieldReader.class); sub.decl(fieldReadClass, parameter.name, JExpr._new(singularReaderClass).arg(inputVariable.getHolder())); } else { sub.decl(inputVariable.getHolder().type(), parameter.name, inputVariable.getHolder()); } } } JVar[] internalVars = new JVar[workspaceJVars.length]; for (int i = 0; i < workspaceJVars.length; i++) { if (decConstInputOnly) { internalVars[i] = sub.decl(g.getModel()._ref(workspaceVars[i].type), workspaceVars[i].name, workspaceJVars[i]); } else { internalVars[i] = sub.decl(g.getModel()._ref(workspaceVars[i].type), workspaceVars[i].name, workspaceJVars[i]); } } Preconditions.checkNotNull(body); sub.directStatement(body); // reassign workspace variables back to global space. for (int i = 0; i < workspaceJVars.length; i++) { sub.assign(workspaceJVars[i], internalVars[i]); } }
Example #20
Source File: EvaluationVisitor.java From Bats with Apache License 2.0 | 5 votes |
private HoldingContainer getHoldingContainer(ClassGenerator<?> generator, MajorType majorType, Function<DrillBuf, ? extends ValueHolder> function) { JType holderType = generator.getHolderType(majorType); Pair<Integer, JVar> depthVar = generator.declareClassConstField("const", holderType, function); JFieldRef outputSet = null; JVar var = depthVar.getValue(); if (majorType.getMode() == TypeProtos.DataMode.OPTIONAL) { outputSet = var.ref("isSet"); } return new HoldingContainer(majorType, var, var.ref("value"), outputSet); }
Example #21
Source File: Translator.java From groovy-cps with Apache License 2.0 | 5 votes |
@Override public JType visitParameterizedType(ParameterizedTypeTree pt, Void __) { JClass base = (JClass)visit(pt.getType()); List<JClass> args = new ArrayList<>(); for (Tree arg : pt.getTypeArguments()) { args.add((JClass)visit(arg)); } return base.narrow(args); }
Example #22
Source File: HashCodeArguments.java From jaxb2-basics with BSD 2-Clause "Simplified" License | 5 votes |
public HashCodeArguments element(JBlock subBlock, JType elementType) { final JVar elementValue = subBlock.decl(JMod.FINAL, elementType, value().name() + "Element", value().invoke("next")); final boolean isElementAlwaysSet = elementType.isPrimitive(); final JExpression elementHasSetValue = isElementAlwaysSet ? JExpr.TRUE : elementValue.ne(JExpr._null()); return spawn(elementValue, elementHasSetValue); }
Example #23
Source File: TypeToJTypeConvertingVisitor.java From jaxb2-basics with BSD 2-Clause "Simplified" License | 5 votes |
@Override public JType visit(ReferenceType type, JCodeModel codeModel) { final JType referencedType = type.getType().accept(this, codeModel); JType referencedTypeArray = referencedType; for (int index = 0; index < type.getArrayCount(); index++) { referencedTypeArray = referencedTypeArray.array(); } return referencedTypeArray; }
Example #24
Source File: AbstractField.java From hyperjaxb3 with BSD 2-Clause "Simplified" License | 5 votes |
/** * Returns contents to be added to javadoc. */ protected final List<Object> listPossibleTypes( CPropertyInfo prop ) { List<Object> r = new ArrayList<Object>(); for( CTypeInfo tt : prop.ref() ) { JType t = tt.getType().toType(outline.parent(),Aspect.EXPOSED); if( t.isPrimitive() || t.isArray() ) r.add(t.fullName()); else { r.add(t); r.add("\n"); } } return r; }
Example #25
Source File: ClassModelBuilder.java From mxjc with MIT License | 5 votes |
private static boolean isNestClass(JType type) { if (type instanceof JClass) { JClass clazz = (JClass)type; JClass out = clazz.outer(); if (out == null) { return false; } if (out instanceof JClass) { return true; } } return false; }
Example #26
Source File: OperationProcessor.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 5 votes |
static private JType toType(JCodeModel codeModel, TypeMirror typeMirror){ String name = typeMirror.toString(); if(name.endsWith("Value<?>")){ name = name.substring(0, name.length() - "Value<?>".length()) + "Value<? extends java.lang.Number>"; } try { return codeModel.parseType(name); } catch(ClassNotFoundException cnfe){ throw new RuntimeException(cnfe); } }
Example #27
Source File: ControllerMethodSignatureRule.java From springmvc-raml-plugin with Apache License 2.0 | 5 votes |
@Override public JMethod apply(ApiActionMetadata endpointMetadata, JDefinedClass generatableType) { JType responseType = responseTypeRule.apply(endpointMetadata, generatableType); JMethod jMethod = generatableType.method(JMod.PUBLIC, responseType, endpointMetadata.getName()); jMethod = paramsRule.apply(endpointMetadata, ext(jMethod, generatableType.owner())); return jMethod; }
Example #28
Source File: SelectorGenerator.java From jaxb2-rich-contract-plugin with MIT License | 5 votes |
JType getSelectorParamType(final JClass instanceType, final JType propertyType) { if(this.selectorParamType.isReference()) { return ((JClass)this.selectorParamType).narrow(instanceType).narrow(propertyType); } else { return this.selectorParamType; } }
Example #29
Source File: PluginImpl.java From immutable-xjc with MIT License | 5 votes |
private JVar addProperty(JDefinedClass clazz, JFieldVar field) { JType jType = getJavaType(field); int builderFieldVisibility = builderInheritance ? JMod.PROTECTED : JMod.PRIVATE; if (isCollection(field)) { return clazz.field(builderFieldVisibility, jType, field.name(), getNewCollectionExpression(field.type().owner(), jType)); } else { return clazz.field(builderFieldVisibility, jType, field.name()); } }
Example #30
Source File: ClassDiscoverer.java From jaxb-visitor with Apache License 2.0 | 5 votes |
/** * Returns true if the type is a JAXBElement. In the case of JAXBElements, we want to traverse its * underlying value as opposed to the JAXBElement. * @param type element type to test to see if its a JAXBElement * @return true if the type is a JAXBElement */ static boolean isJAXBElement(JType type) { //noinspection RedundantIfStatement if (type.fullName().startsWith(JAXBElement.class.getName())) { return true; } return false; }