Skip to content

Commit

Permalink
nested classes should be static
Browse files Browse the repository at this point in the history
  • Loading branch information
wigbam committed Sep 6, 2023
1 parent 7b090b5 commit 12b54ad
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -71,37 +74,42 @@ 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 {
assertThat(classLoader, canLoad("com.example.ExternalRef"));
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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Class<?>> {
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<Class<?>> hasModifiers(int modifiers) {
return new ClassModifiersMatcher(modifiers);
}

}

0 comments on commit 12b54ad

Please sign in to comment.