Skip to content

Commit

Permalink
Merge pull request #700 from DavidTanner/javaType-prefix
Browse files Browse the repository at this point in the history
Don't prefix a class if it already exists without the prefix.
  • Loading branch information
joelittlejohn authored Mar 6, 2017
2 parents 8c50832 + 3af4607 commit 9f1688e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,23 +259,35 @@ private JDefinedClass createClass(String nodeName, JsonNode node, JPackage _pack
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));
JClass existingClass = resolveType(_package, fqn + (node.get("javaType").asText().contains("<") ? "<" + substringAfter(node.get("javaType").asText(), "<") : ""));
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);
}

}
if (usePolymorphicDeserialization) {
newType = _package.owner()._class(JMod.PUBLIC, fqn, ClassType.CLASS);
} else {
newType = _package.owner()._class(fqn);
}
} else {
if (usePolymorphicDeserialization) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public void customClassPrefix() throws ClassNotFoundException{
ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/properties/primitiveProperties.json", "com.example", config("classNamePrefix","Abstract"));
resultsClassLoader.loadClass("com.example.AbstractPrimitiveProperties");
}

@Test(expected = ClassNotFoundException.class)
public void customClassPrefixExistingClass() throws ClassNotFoundException {

ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/properties/objectPropertiesJavaType.json",
"com.example", config("classNamePrefix", "SomePrefix"));
resultsClassLoader.loadClass("org.jsonschema2pojo.SomePrefixNoopAnnotator");
}

@Test
public void noCapsCustomClassPrefix() throws ClassNotFoundException{
Expand Down Expand Up @@ -130,4 +138,5 @@ public void customClassPrefixAndSuffixNoJavaType() throws ClassNotFoundException
"com.example", config("classNamePrefix", "Prefix", "classNameSuffix","Suffix"));
resultsClassLoader.loadClass("com.example.PrefixPrimitivePropertiesNoJavaTypeSuffix");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"type" : "object",
"properties" : {
"a" : {
"javaType": "org.jsonschema2pojo.NoopAnnotator",
"type": "object"
}
}
}

0 comments on commit 9f1688e

Please sign in to comment.