Skip to content

Commit

Permalink
Issue #42.
Browse files Browse the repository at this point in the history
  • Loading branch information
highsource committed Dec 16, 2015
1 parent 5b0bbac commit 828f231
Show file tree
Hide file tree
Showing 16 changed files with 379 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.lang.reflect.Modifier;

import org.jvnet.jaxb2_commons.plugin.Ignoring;
import org.jvnet.jaxb2_commons.util.JClassUtils;

import com.sun.codemodel.JClass;
import com.sun.codemodel.JCodeModel;
Expand Down Expand Up @@ -55,14 +56,24 @@ public static <T> JExpression createStrategyInstanceExpression(

public static <T> Boolean superClassImplements(ClassOutline classOutline,
Ignoring ignoring, Class<? extends T> theInterface) {
if (classOutline.implClass != null
&& classOutline.implClass._extends() != null) {
if (JClassUtils.isInstanceOf(classOutline.implClass._extends(),
theInterface)) {
return Boolean.TRUE;
}
}

if (classOutline.target.getBaseClass() != null) {
if (!ignoring.isIgnored(classOutline.parent().getClazz(
classOutline.target.getBaseClass()))) {
return Boolean.TRUE;
} else {
return Boolean.FALSE;
}
} else if (classOutline.target.getRefBaseClass() != null) {
}

if (classOutline.target.getRefBaseClass() != null) {
try {
if (theInterface.isAssignableFrom(Class
.forName(classOutline.target.getRefBaseClass()
Expand All @@ -75,9 +86,9 @@ public static <T> Boolean superClassImplements(ClassOutline classOutline,
// We'll assume it does implement
return Boolean.TRUE;
}
} else {
return null;
}
// Unknown
return null;
}

public static <T> Boolean superClassNotIgnored(ClassOutline classOutline,
Expand Down
2 changes: 2 additions & 0 deletions tests/superclass/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.settings
.project
4 changes: 4 additions & 0 deletions tests/superclass/a/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.settings
.project
.classpath
target
38 changes: 38 additions & 0 deletions tests/superclass/a/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-test-superclass</artifactId>
<version>0.10.1-SNAPSHOT</version>
</parent>
<artifactId>jaxb2-basics-test-superclass-a</artifactId>
<packaging>jar</packaging>
<name>JAXB2 Basics - Test [superclass-a]</name>
<build>
<defaultGoal>test</defaultGoal>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<configuration>
<extension>true</extension>
<args>
<arg>-XtoString</arg>
<arg>-Xequals</arg>
<arg>-XhashCode</arg>
<arg>-Xcopyable</arg>
<arg>-Xmergeable</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
</plugin>
</plugins>
</configuration>
</plugin>
</plugins>
</build>
</project>
11 changes: 11 additions & 0 deletions tests/superclass/a/src/main/resources/a.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="a"
xmlns:a="a"
elementFormDefault="qualified">

<xsd:complexType name="AbstractObject">
<xsd:attribute name="id" type="xsd:string" use="optional"/>
</xsd:complexType>

</xsd:schema>
15 changes: 15 additions & 0 deletions tests/superclass/a/src/main/resources/binding.xjb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<jaxb:bindings
version="1.0"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:hj="http://hyperjaxb3.jvnet.org/ejb/schemas/customizations"
jaxb:extensionBindingPrefixes="xjc"
xmlns:a="a">

<jaxb:bindings scd="x-schema::a">
<jaxb:schemaBindings>
<jaxb:package name="org.jvnet.jaxb2_commons.test.superclass.a"/>
</jaxb:schemaBindings>
</jaxb:bindings>
</jaxb:bindings>
4 changes: 4 additions & 0 deletions tests/superclass/b/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.settings
.project
.classpath
target
61 changes: 61 additions & 0 deletions tests/superclass/b/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-test-superclass</artifactId>
<version>0.10.1-SNAPSHOT</version>
</parent>
<artifactId>jaxb2-basics-test-superclass-b</artifactId>
<packaging>jar</packaging>
<name>JAXB2 Basics - Test [superclass-b]</name>
<dependencies>
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-test-superclass-a</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin-testing</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<defaultGoal>test</defaultGoal>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<configuration>
<extension>true</extension>
<args>
<arg>-XtoString</arg>
<arg>-Xequals</arg>
<arg>-XhashCode</arg>
<arg>-Xcopyable</arg>
<arg>-Xmergeable</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
</plugin>
</plugins>
<episodes>
<episode>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-test-superclass-a</artifactId>
</episode>
</episodes>
</configuration>
</plugin>
</plugins>
</build>
</project>
32 changes: 32 additions & 0 deletions tests/superclass/b/src/main/resources/binding.xjb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<jaxb:bindings
version="1.0"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:hj="http://hyperjaxb3.jvnet.org/ejb/schemas/customizations"
jaxb:extensionBindingPrefixes="xjc"
xmlns:b="b">

<jaxb:globalBindings>
<xjc:superClass name="org.jvnet.jaxb2_commons.test.superclass.a.AbstractObject"/>
</jaxb:globalBindings>

<jaxb:bindings scd="x-schema::b">
<jaxb:schemaBindings>
<jaxb:package name="org.jvnet.jaxb2_commons.test.superclass.b"/>
</jaxb:schemaBindings>
</jaxb:bindings>

<!--jaxb:bindings scd="x-schema::a">
<jaxb:schemaBindings>
<jaxb:package name="org.jvnet.hyperjaxb3.ejb.tests.episodes.a.tests"/>
</jaxb:schemaBindings>
<jaxb:bindings scd="~a:AType">
<jaxb:class ref="org.jvnet.hyperjaxb3.ejb.tests.episodes.a.tests.AType"/>
</jaxb:bindings>
<jaxb:bindings scd="~a:A1Type">
<jaxb:class ref="org.jvnet.hyperjaxb3.ejb.tests.episodes.a.tests.A1Type"/>
</jaxb:bindings>
</jaxb:bindings-->

</jaxb:bindings>
15 changes: 15 additions & 0 deletions tests/superclass/b/src/main/resources/schema.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="b"
xmlns:b="b"
elementFormDefault="qualified">

<xsd:element name="AnotherObject" type="b:AnotherObjectType"/>

<xsd:complexType name="AnotherObjectType">
<xsd:sequence>
<xsd:element name="data" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>

</xsd:schema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.jvnet.jaxb2_commons.test.superclass.b.tests;

import org.junit.Assert;
import org.junit.Test;
import org.jvnet.jaxb2_commons.test.superclass.b.AnotherObjectType;

public class CopyToTest {

@Test
public void correctlyCopies() {
final AnotherObjectType source = new AnotherObjectType();
source.setId("Id");
source.setData("Data");
final AnotherObjectType target = (AnotherObjectType) source.clone();
Assert.assertEquals("Id", target.getId());
Assert.assertEquals("Data", target.getData());
Assert.assertEquals(source, target);
Assert.assertEquals(source.hashCode(), target.hashCode());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.jvnet.jaxb2_commons.test.superclass.b.tests;

import java.io.File;
import java.net.URL;

import org.junit.Before;
import org.junit.Test;

import com.sun.codemodel.JCodeModel;
import com.sun.tools.xjc.ConsoleErrorReporter;
import com.sun.tools.xjc.ModelLoader;
import com.sun.tools.xjc.Options;
import com.sun.tools.xjc.model.Model;

public class RunPlugins {

@Before
public void setUp() {
System.setProperty("javax.xml.accessExternalSchema", "all");
}

@Test
public void compilesSchema() throws Exception {

new File("target/generated-sources/xjc").mkdirs();

URL schema = getClass().getResource("/schema.xsd");
URL binding = getClass().getResource("/binding.xjb");
final String[] arguments = new String[] { "-xmlschema",
schema.toExternalForm(), "-b", binding.toExternalForm(), "-d",
"target/generated-sources/xjc", "-extension", "-XtoString",
"-Xequals", "-XhashCode", "-Xcopyable", "-Xmergeable" };

Options options = new Options();
options.parseArguments(arguments);
ConsoleErrorReporter receiver = new ConsoleErrorReporter();
Model model = ModelLoader.load(options, new JCodeModel(), receiver);
model.generateCode(options, receiver);
com.sun.codemodel.CodeWriter cw = options.createCodeWriter();
model.codeModel.build(cw);
}
}
8 changes: 8 additions & 0 deletions tests/superclass/b/src/test/samples/1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<b xmlns:a="a" xmlns="b">
<a:a>
<a:a1>test</a:a1>
</a:a>
<b>
<b1>test</b1>
</b>
</b>
20 changes: 20 additions & 0 deletions tests/superclass/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-tests</artifactId>
<version>0.10.1-SNAPSHOT</version>
</parent>
<artifactId>jaxb2-basics-test-superclass</artifactId>
<packaging>pom</packaging>
<name>JAXB2 Basics - Test [superclass]</name>
<modules>
<module>a</module>
<module>b</module>
</modules>
<build>
<defaultGoal>test</defaultGoal>
</build>
</project>
45 changes: 45 additions & 0 deletions tools/src/main/java/org/jvnet/jaxb2_commons/util/JClassUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.jvnet.jaxb2_commons.util;

import java.util.Iterator;

import org.apache.commons.lang3.Validate;

import com.sun.codemodel.JClass;

public class JClassUtils {

public static <T> boolean isInstanceOf(JClass _class,
Class<? extends T> _interface) {
Validate.notNull(_class);
Validate.notNull(_interface);

final String className = _class.fullName();

try {
if (_interface.isAssignableFrom(Class.forName(className))) {
return true;
}
} catch (ClassNotFoundException cnfex) {
// Unknown
}

final JClass superClass = _class._extends();
if (superClass != null) {
if (isInstanceOf(superClass, _interface)) {
return true;
}
}

for (final Iterator<? extends JClass> implementsIterator = _class
._implements(); implementsIterator.hasNext();) {
final JClass superInterface = implementsIterator.next();

if (isInstanceOf(superInterface, _interface)) {
return true;
}
}

return false;
}

}
Loading

0 comments on commit 828f231

Please sign in to comment.