use of com.sun.codemodel.JDefinedClass in project rest.li by linkedin.
the class JavaDataTemplateGenerator method generatePathSpecMethodsForUnion.
private void generatePathSpecMethodsForUnion(UnionTemplateSpec unionSpec, JDefinedClass unionClass) throws JClassAlreadyExistsException {
final JDefinedClass fieldsNestedClass = generatePathSpecNestedClass(unionClass);
for (UnionTemplateSpec.Member member : unionSpec.getMembers()) {
JClass fieldsRefType = _pathSpecClass;
if (hasNestedFields(member.getSchema())) {
final JClass unionMemberClass = generate(member.getClassTemplateSpec());
fieldsRefType = getCodeModel().ref(unionMemberClass.fullName() + ".Fields");
}
final JMethod accessorMethod = fieldsNestedClass.method(JMod.PUBLIC, fieldsRefType, CodeUtil.getUnionMemberName(member.getSchema()));
accessorMethod.body()._return(JExpr._new(fieldsRefType).arg(JExpr.invoke("getPathComponents")).arg(member.getSchema().getUnionMemberKey()));
}
}
use of com.sun.codemodel.JDefinedClass in project rest.li by linkedin.
the class JavaDataTemplateGenerator method defineClass.
private JDefinedClass defineClass(ClassTemplateSpec classTemplateSpec) throws JClassAlreadyExistsException {
JDefinedClass result = _definedClasses.get(classTemplateSpec);
if (result == null) {
final int jmodValue = getJModValue(classTemplateSpec.getModifiers());
final JClassContainer container;
if (classTemplateSpec.getEnclosingClass() == null) {
container = getPackage(classTemplateSpec.getPackage());
} else {
container = defineClass(classTemplateSpec.getEnclosingClass());
}
if (classTemplateSpec instanceof ArrayTemplateSpec || classTemplateSpec instanceof FixedTemplateSpec || classTemplateSpec instanceof MapTemplateSpec || classTemplateSpec instanceof RecordTemplateSpec || classTemplateSpec instanceof TyperefTemplateSpec || classTemplateSpec instanceof UnionTemplateSpec) {
result = container._class(jmodValue, escapeReserved(classTemplateSpec.getClassName()));
} else if (classTemplateSpec instanceof EnumTemplateSpec) {
result = container._class(jmodValue, escapeReserved(classTemplateSpec.getClassName()), ClassType.ENUM);
} else {
throw new RuntimeException();
}
_definedClasses.put(classTemplateSpec, result);
}
return result;
}
use of com.sun.codemodel.JDefinedClass in project rest.li by linkedin.
the class JavaDataTemplateGenerator method generatePathSpecNestedClass.
private JDefinedClass generatePathSpecNestedClass(JDefinedClass templateClass) throws JClassAlreadyExistsException {
final JDefinedClass fieldsNestedClass = templateClass._class(JMod.PUBLIC | JMod.STATIC, "Fields");
fieldsNestedClass._extends(_pathSpecClass);
final JMethod constructor = fieldsNestedClass.constructor(JMod.PUBLIC);
final JClass listString = getCodeModel().ref(List.class).narrow(String.class);
final JVar namespace = constructor.param(listString, "path");
final JVar name = constructor.param(String.class, "name");
constructor.body().invoke(SUPER).arg(namespace).arg(name);
fieldsNestedClass.constructor(JMod.PUBLIC).body().invoke(SUPER);
return fieldsNestedClass;
}
use of com.sun.codemodel.JDefinedClass in project jsonschema2pojo by joelittlejohn.
the class ObjectRule method apply.
/**
* Applies this schema rule to take the required code generation steps.
* <p>
* When this rule is applied for schemas of type object, the properties of
* the schema are used to generate a new Java class and determine its
* characteristics. See other implementers of {@link Rule} for details.
*/
@Override
public JType apply(String nodeName, JsonNode node, JPackage _package, Schema schema) {
JType superType = getSuperType(nodeName, node, _package, schema);
if (superType.isPrimitive() || isFinal(superType)) {
return superType;
}
JDefinedClass jclass;
try {
jclass = createClass(nodeName, node, _package);
} catch (ClassAlreadyExistsException e) {
return e.getExistingClass();
}
jclass._extends((JClass) superType);
schema.setJavaTypeIfEmpty(jclass);
if (node.has("deserializationClassProperty")) {
addJsonTypeInfoAnnotation(jclass, node);
}
if (node.has("title")) {
ruleFactory.getTitleRule().apply(nodeName, node.get("title"), jclass, schema);
}
if (node.has("description")) {
ruleFactory.getDescriptionRule().apply(nodeName, node.get("description"), jclass, schema);
}
ruleFactory.getPropertiesRule().apply(nodeName, node.get("properties"), jclass, schema);
if (ruleFactory.getGenerationConfig().isIncludeToString()) {
addToString(jclass);
}
if (node.has("javaInterfaces")) {
addInterfaces(jclass, node.get("javaInterfaces"));
}
ruleFactory.getAdditionalPropertiesRule().apply(nodeName, node.get("additionalProperties"), jclass, schema);
ruleFactory.getDynamicPropertiesRule().apply(nodeName, node.get("properties"), jclass, schema);
if (node.has("required")) {
ruleFactory.getRequiredArrayRule().apply(nodeName, node.get("required"), jclass, schema);
}
if (ruleFactory.getGenerationConfig().isIncludeHashcodeAndEquals()) {
addHashCode(jclass);
addEquals(jclass);
}
if (ruleFactory.getGenerationConfig().isParcelable()) {
addParcelSupport(jclass);
}
if (ruleFactory.getGenerationConfig().isIncludeConstructors()) {
addConstructors(jclass, node, schema, ruleFactory.getGenerationConfig().isConstructorsRequiredPropertiesOnly());
}
if (ruleFactory.getGenerationConfig().isSerializable()) {
SerializableHelper.addSerializableSupport(jclass);
}
return jclass;
}
use of com.sun.codemodel.JDefinedClass in project jsonschema2pojo by joelittlejohn.
the class ObjectRule method createClass.
/**
* Creates a new Java class that will be generated.
*
* @param nodeName
* the node name which may be used to dictate the new class name
* @param node
* the node representing the schema that caused the need for a
* new class. This node may include a 'javaType' property which
* if present will override the fully qualified name of the newly
* generated class.
* @param _package
* the package which may contain a new class after this method
* call
* @return a reference to a newly created class
* @throws ClassAlreadyExistsException
* if the given arguments cause an attempt to create a class
* that already exists, either on the classpath or in the
* current map of classes to be generated.
*/
private JDefinedClass createClass(String nodeName, JsonNode node, JPackage _package) throws ClassAlreadyExistsException {
JDefinedClass newType;
try {
boolean usePolymorphicDeserialization = usesPolymorphicDeserialization(node);
if (node.has("javaType")) {
String fqn = substringBefore(node.get("javaType").asText(), "<");
if (isPrimitive(fqn, _package.owner())) {
throw new ClassAlreadyExistsException(primitiveType(fqn, _package.owner()));
}
JClass existingClass;
try {
_package.owner().ref(Thread.currentThread().getContextClassLoader().loadClass(fqn));
existingClass = resolveType(_package, fqn + (node.get("javaType").asText().contains("<") ? "<" + substringAfter(node.get("javaType").asText(), "<") : ""));
throw new ClassAlreadyExistsException(existingClass);
} catch (ClassNotFoundException e) {
}
int index = fqn.lastIndexOf(".") + 1;
if (index >= 0 && index < fqn.length()) {
fqn = fqn.substring(0, index) + ruleFactory.getGenerationConfig().getClassNamePrefix() + fqn.substring(index) + ruleFactory.getGenerationConfig().getClassNameSuffix();
}
try {
_package.owner().ref(Thread.currentThread().getContextClassLoader().loadClass(fqn));
existingClass = resolveType(_package, fqn + (node.get("javaType").asText().contains("<") ? "<" + substringAfter(node.get("javaType").asText(), "<") : ""));
throw new ClassAlreadyExistsException(existingClass);
} catch (ClassNotFoundException e) {
}
if (usePolymorphicDeserialization) {
newType = _package.owner()._class(JMod.PUBLIC, fqn, ClassType.CLASS);
} else {
newType = _package.owner()._class(fqn);
}
} else {
if (usePolymorphicDeserialization) {
newType = _package._class(JMod.PUBLIC, getClassName(nodeName, node, _package), ClassType.CLASS);
} else {
newType = _package._class(getClassName(nodeName, node, _package));
}
}
} catch (JClassAlreadyExistsException e) {
throw new ClassAlreadyExistsException(e.getExistingClass());
}
ruleFactory.getAnnotator().propertyInclusion(newType, node);
return newType;
}
Aggregations