diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/ObjectRule.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/ObjectRule.java index 7fbb3cd99..93f39cd51 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/ObjectRule.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/ObjectRule.java @@ -243,10 +243,18 @@ private JDefinedClass createClass(String nodeName, JsonNode node, JClassContaine ruleFactory.getLogger().debug("Adding " + newType.fullName()); } else { final String className = ruleFactory.getNameHelper().getUniqueClassName(nodeName, node, container); + + final int mods; + if (container.isClass()) { + mods = JMod.PUBLIC | JMod.STATIC; + } else { + mods = JMod.PUBLIC; + } + if (usePolymorphicDeserialization) { - newType = container._class(JMod.PUBLIC, className, ClassType.CLASS); + newType = container._class(mods, className, ClassType.CLASS); } else { - newType = container._class(className); + newType = container._class(mods, className); } ruleFactory.getLogger().debug("Adding " + newType.fullName()); } diff --git a/jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/rules/ObjectRuleTest.java b/jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/rules/ObjectRuleTest.java index e80ac77ce..ffb9c4708 100644 --- a/jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/rules/ObjectRuleTest.java +++ b/jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/rules/ObjectRuleTest.java @@ -21,6 +21,7 @@ import static org.mockito.Mockito.*; import com.fasterxml.jackson.databind.node.ObjectNode; +import com.sun.codemodel.JMod; import com.sun.codemodel.JPackage; import com.sun.codemodel.JType; import org.jsonschema2pojo.Annotator; @@ -120,6 +121,7 @@ public void testSimpleObjectAsNested() throws Exception { assertThat(codeModel.countArtifacts(), equalTo(1)); assertThat(result.binaryName(), is("org.jsonschema2pojo.test.Parent$Child")); assertThat(result, instanceOf(JDefinedClass.class)); + assertThat(((JDefinedClass) result).mods().getValue() & JMod.STATIC, equalTo(JMod.STATIC)); final JDefinedClass definedClass = (JDefinedClass) result; assertThat(definedClass.fields().values(), empty()); diff --git a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/NestedClassesIT.java b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/NestedClassesIT.java index b6af310c0..96b480d8d 100644 --- a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/NestedClassesIT.java +++ b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/NestedClassesIT.java @@ -22,9 +22,12 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.jsonschema2pojo.integration.util.ClassLoaderMatcher.canLoad; +import static org.jsonschema2pojo.integration.util.ClassModifiersMatcher.*; import static org.jsonschema2pojo.integration.util.ClassPropertyMatcher.hasProperty; import static org.jsonschema2pojo.integration.util.CodeGenerationHelper.config; +import java.lang.reflect.Modifier; + public class NestedClassesIT { @Rule public Jsonschema2PojoRule schemaRule = new Jsonschema2PojoRule(); @@ -71,32 +74,29 @@ public void canGenerateTheWholeDirectory() throws Exception { } private static void assertSingleLevelEntityWasGeneratedCorrectly(ClassLoader classLoader) throws Exception { - assertThat(classLoader, canLoad("com.example.SingleLevel$NestedOne")); - assertThat(classLoader.loadClass("com.example.SingleLevel$NestedOne"), - hasProperty("bar", Boolean.class)); + final Class nestedOne = assertCanLoadNestedClass(classLoader, "com.example.SingleLevel$NestedOne"); + assertThat(nestedOne, hasProperty("bar", Boolean.class)); - assertThat(classLoader, canLoad("com.example.SingleLevel$NestedTwo")); - assertThat(classLoader.loadClass("com.example.SingleLevel$NestedTwo"), - hasProperty("foo", Integer.class)); + final Class nestedTwo = assertCanLoadNestedClass(classLoader, "com.example.SingleLevel$NestedTwo"); + assertThat(nestedTwo, hasProperty("foo", Integer.class)); } private static void assertMultipleLevelEntityWasGeneratedCorrectly(ClassLoader classLoader) throws Exception { - assertThat(classLoader, canLoad("com.example.MultipleLevel$NestedOne")); - assertThat(classLoader.loadClass("com.example.MultipleLevel$NestedOne"), - hasProperty("foo", String.class)); + final Class nestedOne = assertCanLoadNestedClass(classLoader, "com.example.MultipleLevel$NestedOne"); + assertThat(nestedOne, hasProperty("foo", String.class)); - assertThat(classLoader, canLoad("com.example.MultipleLevel$NestedTwo")); - assertThat(classLoader.loadClass("com.example.MultipleLevel$NestedTwo"), - hasProperty("bar", "com.example.MultipleLevel$NestedTwo$Bar")); + final Class nestedTwo = assertCanLoadNestedClass(classLoader, "com.example.MultipleLevel$NestedTwo"); + assertThat(nestedTwo, hasProperty("bar", "com.example.MultipleLevel$NestedTwo$Bar")); } private static void assertArrayRefWithDefinitionEntityWasGeneratedCorrectly(ClassLoader classLoader) throws Exception { assertThat(classLoader, canLoad("com.example.ArrayItemWithDefinition")); - assertThat(classLoader, canLoad("com.example.ArrayItemWithDefinition$Parent")); - assertThat(classLoader, canLoad("com.example.ArrayItemWithDefinition$Parent$Child")); - assertThat(classLoader.loadClass("com.example.ArrayItemWithDefinition$Parent$Child"), - hasProperty("external", "com.example.SingleLevel")); + assertCanLoadNestedClass(classLoader, "com.example.ArrayItemWithDefinition$Parent"); + + final Class child = assertCanLoadNestedClass(classLoader, + "com.example.ArrayItemWithDefinition$Parent$Child"); + assertThat(child, hasProperty("external", "com.example.SingleLevel")); } private static void assertExternalRefEntityWasGeneratedCorrectly(ClassLoader classLoader) throws Exception { @@ -104,4 +104,12 @@ private static void assertExternalRefEntityWasGeneratedCorrectly(ClassLoader cla assertThat(classLoader.loadClass("com.example.ExternalRef"), hasProperty("subschema", "com.example.SingleLevel")); } + + private static Class assertCanLoadNestedClass(ClassLoader classLoader, String name) throws Exception { + assertThat(classLoader, canLoad(name)); + final Class klazz = classLoader.loadClass(name); + assertThat(klazz, hasModifiers(Modifier.STATIC)); + + return klazz; + } } diff --git a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/util/ClassModifiersMatcher.java b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/util/ClassModifiersMatcher.java new file mode 100644 index 000000000..00b70de6f --- /dev/null +++ b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/util/ClassModifiersMatcher.java @@ -0,0 +1,74 @@ +/** + * Copyright © 2010-2020 Nokia + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jsonschema2pojo.integration.util; + +import java.lang.reflect.Modifier; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeMatcher; + +/** + * Matcher that is successful if the provided class has given modifiers. + */ +public class ClassModifiersMatcher extends TypeSafeMatcher> { + private final int modifiers; + + /** + * Create a new matcher for the given modifiers. + * + * @param modifiers + * the modifiers to be matched + */ + public ClassModifiersMatcher(int modifiers) { + if ((modifiers | Modifier.classModifiers()) != Modifier.classModifiers()) { + throw new IllegalArgumentException("Invalid class modifiers"); + } + this.modifiers = modifiers; + } + + @Override + public void describeTo(Description description) { + description.appendText("the class has the following modifiers: " + Modifier.toString(modifiers)); + } + + @Override + protected void describeMismatchSafely(Class clazz, Description mismatchDescription) { + final int matchedModifiers = clazz.getModifiers() & modifiers; + if (matchedModifiers != modifiers) { + mismatchDescription.appendText("the class does not have following modifiers: " + + Modifier.toString(matchedModifiers ^ modifiers)); + } + } + + @Override + protected boolean matchesSafely(Class clazz) { + final int matchedModifiers = clazz.getModifiers() & modifiers; + return matchedModifiers == modifiers; + } + + /** + * Create a new matcher for the given modifiers. + * + * @param modifiers + * the modifiers to be matched + */ + public static Matcher> hasModifiers(int modifiers) { + return new ClassModifiersMatcher(modifiers); + } + +}