Skip to content

Commit

Permalink
joelittlejohn#402 support of existing java class inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Hanin committed Aug 20, 2015
1 parent f1f4446 commit 66fe648
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Copyright © 2010-2014 Nokia
*
* <p>
* 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
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.
Expand Down Expand Up @@ -44,7 +44,6 @@
import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JClassAlreadyExistsException;
import com.sun.codemodel.JClassContainer;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JFieldVar;
Expand Down Expand Up @@ -166,7 +165,7 @@ private void addParcelSupport(JDefinedClass jclass) {
* Retrieve the list of properties to go in the constructor from node. This
* is all properties listed in node["properties"] if ! onlyRequired, and
* only required properties if onlyRequired.
*
*
* @param node
* @return
*/
Expand All @@ -179,7 +178,7 @@ private List<String> getConstructorProperties(JsonNode node, boolean onlyRequire
List<String> rtn = new ArrayList<String>();

NameHelper nameHelper = ruleFactory.getNameHelper();
for (Iterator<Map.Entry<String, JsonNode>> properties = node.get("properties").fields(); properties.hasNext();) {
for (Iterator<Map.Entry<String, JsonNode>> properties = node.get("properties").fields(); properties.hasNext(); ) {
Map.Entry<String, JsonNode> property = properties.next();

JsonNode propertyObj = property.getValue();
Expand Down Expand Up @@ -269,10 +268,15 @@ private boolean isFinal(JType superType) {
}
}

private JType getSuperType(String nodeName, JsonNode node, JClassContainer jClassContainer, Schema schema) {
JType superType = jClassContainer.owner().ref(Object.class);
private JType getSuperType(String nodeName, JsonNode node, JPackage jPackage, Schema schema) {
if (node.has("extends") && node.has("extendsJavaClass")) {
throw new IllegalStateException("'extends' and 'extendsJavaClass' defined simultaneously");
}
JType superType = jPackage.owner().ref(Object.class);
if (node.has("extends")) {
superType = ruleFactory.getSchemaRule().apply(nodeName + "Parent", node.get("extends"), jClassContainer, schema);
superType = ruleFactory.getSchemaRule().apply(nodeName + "Parent", node.get("extends"), jPackage, schema);
} else if (node.has("extendsJavaClass")) {
superType = resolveType(jPackage, node.get("extendsJavaClass").asText());
}
return superType;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Copyright © 2010-2014 Nokia
*
* <p>
* 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
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.
Expand All @@ -16,17 +16,26 @@

package org.jsonschema2pojo.integration;

import static org.hamcrest.Matchers.*;
import static org.jsonschema2pojo.integration.util.CodeGenerationHelper.*;
import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.File;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;

import org.junit.BeforeClass;
import org.junit.Test;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItemInArray;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.jsonschema2pojo.integration.util.CodeGenerationHelper.compile;
import static org.jsonschema2pojo.integration.util.CodeGenerationHelper.config;
import static org.jsonschema2pojo.integration.util.CodeGenerationHelper.generate;
import static org.jsonschema2pojo.integration.util.CodeGenerationHelper.generateAndCompile;
import static org.junit.Assert.assertThat;

public class TypeIT {

Expand Down Expand Up @@ -272,7 +281,35 @@ public void typeImplementsInterfacesWithGenericArgsCorrectly() throws NoSuchMeth
assertThat((Class[]) getterMethod.getReturnType().getInterfaces(), hasItemInArray((Class) InterfaceWithGenerics.class));
}

public static interface InterfaceWithGenerics<T, U, V> {
public interface InterfaceWithGenerics<T, U, V> {
}

@Test
public void typeExtendsJavaClass() throws NoSuchMethodException {
Method getterMethod = classWithManyTypes.getMethod("getTypeWithInheritedClass");

final Class<?> generatedClass = getterMethod.getReturnType();
assertThat(generatedClass.getName(), is("com.example.TypeWithInheritedClass"));
assertThat(generatedClass.getSuperclass().equals(InheritedClass.class), equalTo(true));
}

public static class InheritedClass {
}

@Test
public void typeExtendsJavaClassWithGenerics() throws NoSuchMethodException {
Method getterMethod = classWithManyTypes.getMethod("getTypeWithInheritedClassWithGenerics");

final Class<?> generatedClass = getterMethod.getReturnType();
assertThat(generatedClass.getName(), is("com.example.TypeWithInheritedClassWithGenerics"));
assertThat(generatedClass.getSuperclass().equals(InheritedClassWithGenerics.class), equalTo(true));
assertThat(((ParameterizedType) generatedClass.getGenericSuperclass()).getActualTypeArguments(), equalTo(new Type[]
{
String.class, Integer.class, Boolean.class
}));
}

public static class InheritedClassWithGenerics<X, Y, Z> {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@
"type" : "object",
"javaInterfaces" : ["org.jsonschema2pojo.integration.TypeIT.InterfaceWithGenerics<String,Integer,Boolean>"]
},
"typeWithInheritedClass" : {
"type" : "object",
"extendsJavaClass" : "org.jsonschema2pojo.integration.TypeIT.InheritedClass"
},
"typeWithInheritedClassWithGenerics" : {
"type" : "object",
"extendsJavaClass" : "org.jsonschema2pojo.integration.TypeIT.InheritedClassWithGenerics<String,Integer,Boolean>"
},
"nullableStringProperty" : {
"type" : ["null", "string"]
}
Expand Down

0 comments on commit 66fe648

Please sign in to comment.