diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/ClassWriter.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/ClassWriter.java index 95fe22984a..874a294e42 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/ClassWriter.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/ClassWriter.java @@ -31,6 +31,8 @@ import javax.lang.model.element.TypeElement; import javax.lang.model.type.DeclaredType; import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; +import javax.persistence.Entity; +import javax.persistence.MappedSuperclass; import javax.tools.Diagnostic; import javax.tools.FileObject; @@ -38,9 +40,13 @@ import org.hibernate.jpamodelgen.model.MetaAttribute; import org.hibernate.jpamodelgen.model.MetaEntity; /** + * Helper class to write the actual meta model class using the {@link javax.annotation.processing.Filer} API. + * * @author Emmanuel Bernard + * @author Hardy Ferentschik */ public final class ClassWriter { + private static final String META_MODEL_CLASS_NAME_SUFFIX = "_"; private ClassWriter() { } @@ -51,7 +57,7 @@ public final class ClassWriter { StringBuffer body = generateBody( entity, context ); FileObject fo = context.getProcessingEnvironment().getFiler().createSourceFile( - metaModelPackage + "." + entity.getSimpleName() + "_" + getFullyQualifiedClassName( entity, metaModelPackage ) ); OutputStream os = fo.openOutputStream(); PrintWriter pw = new PrintWriter( os ); @@ -63,7 +69,6 @@ public final class ClassWriter { pw.flush(); pw.close(); - } catch ( FilerException filerEx ) { context.logMessage( @@ -87,15 +92,14 @@ public final class ClassWriter { * @return body content */ private static StringBuffer generateBody(MetaEntity entity, Context context) { - StringWriter sw = new StringWriter(); PrintWriter pw = null; try { pw = new PrintWriter( sw ); if ( context.isAddGeneratedAnnotation() ) { - pw.println( "@" + entity.importType( Generated.class.getName() ) + "(\"JPA MetaModel for " + entity.getQualifiedName() + "\")" ); + pw.println( writeGeneratedAnnotation( entity ) ); } - pw.println( "@" + entity.importType( "javax.persistence.metamodel.StaticMetamodel" ) + "(" + entity.getSimpleName() + ".class)" ); + pw.println( writeStaticMetaModelAnnotation( entity ) ); printClassDeclaration( entity, pw, context ); pw.println(); List members = entity.getMembers(); @@ -114,19 +118,62 @@ public final class ClassWriter { } private static void printClassDeclaration(MetaEntity entity, PrintWriter pw, Context context) { - pw.print( "public abstract class " + entity.getSimpleName() + "_" ); + pw.print( "public abstract class " + entity.getSimpleName() + META_MODEL_CLASS_NAME_SUFFIX ); final TypeMirror superClass = entity.getTypeElement().getSuperclass(); //superclass of Object is of NoType which returns some other kind if ( superClass.getKind() == TypeKind.DECLARED ) { //F..king Ch...t Have those people used their horrible APIs even once? - final Element superClassElement = ( ( DeclaredType ) superClass ).asElement(); - String superClassName = ( ( TypeElement ) superClassElement ).getQualifiedName().toString(); - if ( context.containsMetaEntity( superClassName ) - || context.containsMetaEmbeddable( superClassName ) ) { - pw.print( " extends " + superClassName + "_" ); + final Element superClassElement = ( (DeclaredType) superClass ).asElement(); + String superClassName = ( (TypeElement) superClassElement ).getQualifiedName().toString(); + if ( extendsSuperMetaModel( superClassElement, entity.isMetaComplete(), context ) ) { + pw.print( " extends " + superClassName + META_MODEL_CLASS_NAME_SUFFIX ); } } pw.println( " {" ); } + + /** + * Checks whether this metamodel class needs to extend another metamodel class. + * This methods checks whether the processor has generated a metamodel class for the super class, but it also + * allows for the possibility that the metamodel class was generated in a previous compilation (eg it could be + * part of a separate jar. See also METAGEN-35). + * + * @param superClassElement the super class element + * @param entityMetaComplete flag indicating if the entity for which the metamodel should be generarted is metamodel + * complete. If so we cannot use reflection to decide whether we have to add the extend clause + * @param context the execution context + * + * @return {@code true} in case there is super class meta model to extend from {@code false} otherwise. + */ + private static boolean extendsSuperMetaModel(Element superClassElement, boolean entityMetaComplete, Context context) { + // if we processed the superclass in the same run we definitely need to extend + String superClassName = ( (TypeElement) superClassElement ).getQualifiedName().toString(); + if ( context.containsMetaEntity( superClassName ) + || context.containsMetaEmbeddable( superClassName ) ) { + return true; + } + + // to allow for the case that the metamodel class for the super entity is for example contained in another + // jar file we use reflection. However, we need to consider the fact that there is xml configuration + // and annotations should be ignored + if ( !entityMetaComplete && ( superClassElement.getAnnotation( Entity.class ) != null + || superClassElement.getAnnotation( MappedSuperclass.class ) != null ) ) { + return true; + } + + return false; + } + + private static String getFullyQualifiedClassName(MetaEntity entity, String metaModelPackage) { + return metaModelPackage + "." + entity.getSimpleName() + META_MODEL_CLASS_NAME_SUFFIX; + } + + private static String writeGeneratedAnnotation(MetaEntity entity) { + return "@" + entity.importType( Generated.class.getName() ) + "(\"JPA MetaModel for " + entity.getQualifiedName() + "\")"; + } + + private static String writeStaticMetaModelAnnotation(MetaEntity entity) { + return "@" + entity.importType( "javax.persistence.metamodel.StaticMetamodel" ) + "(" + entity.getSimpleName() + ".class)"; + } } diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/Constants.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/Constants.java index b1e1b0a4e0..cada8f4495 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/Constants.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/Constants.java @@ -27,7 +27,7 @@ import java.util.Map; /** * @author Hardy Ferentschik */ -public class Constants { +public final class Constants { public static Map COLLECTIONS = new HashMap(); static { diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/StringUtil.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/StringUtil.java index bd43fc855b..ee56ef40e6 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/StringUtil.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/StringUtil.java @@ -22,7 +22,7 @@ package org.hibernate.jpamodelgen.util; /** * @author Hardy Ferentschik */ -public class StringUtil { +public final class StringUtil { private static final String NAME_SEPARATOR = "."; private static final String PROPERTY_PREFIX_GET = "get"; private static final String PROPERTY_PREFIX_IS = "is"; @@ -63,16 +63,17 @@ public class StringUtil { return null; } + String tmp = name; if ( name.startsWith( PROPERTY_PREFIX_GET ) ) { - name = name.replaceFirst( PROPERTY_PREFIX_GET, "" ); + tmp = name.replaceFirst( PROPERTY_PREFIX_GET, "" ); } else if ( name.startsWith( PROPERTY_PREFIX_IS ) ) { - name = name.replaceFirst( PROPERTY_PREFIX_IS, "" ); + tmp = name.replaceFirst( PROPERTY_PREFIX_IS, "" ); } else if ( name.startsWith( PROPERTY_PREFIX_HAS ) ) { - name = name.replaceFirst( PROPERTY_PREFIX_HAS, "" ); + tmp = name.replaceFirst( PROPERTY_PREFIX_HAS, "" ); } - return name.substring(0,1).toLowerCase() + name.substring(1); + return tmp.substring(0,1).toLowerCase() + tmp.substring(1); } } diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/xml/XmlMetaEntity.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/xml/XmlMetaEntity.java index d9b3f97a40..da7d0c4e30 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/xml/XmlMetaEntity.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/xml/XmlMetaEntity.java @@ -124,7 +124,7 @@ public class XmlMetaEntity implements MetaEntity { this.isMetaComplete = initIsMetaComplete( metaComplete ); } - protected void init() { + protected final void init() { this.accessTypeInfo = context.getAccessTypeInfo( getQualifiedName() ); if ( attributes != null ) { parseAttributes( attributes ); diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/accesstype/AccessTypeTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/accesstype/AccessTypeTest.java index f43cd7213f..824b83dc71 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/accesstype/AccessTypeTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/accesstype/AccessTypeTest.java @@ -99,7 +99,7 @@ public class AccessTypeTest extends CompilationTest { } @Override - protected String getPackageNameOfTestSources() { + protected String getPackageNameOfCurrentTest() { return AccessTypeTest.class.getPackage().getName(); } diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/arraytype/ArrayTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/arraytype/ArrayTest.java index b83db1affe..c2acb2e034 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/arraytype/ArrayTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/arraytype/ArrayTest.java @@ -49,7 +49,7 @@ public class ArrayTest extends CompilationTest { } @Override - protected String getPackageNameOfTestSources() { + protected String getPackageNameOfCurrentTest() { return Image.class.getPackage().getName(); } } \ No newline at end of file diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/blob/BlobTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/blob/BlobTest.java index 6a942b8fe1..ee8017697b 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/blob/BlobTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/blob/BlobTest.java @@ -40,7 +40,7 @@ public class BlobTest extends CompilationTest { } @Override - protected String getPackageNameOfTestSources() { + protected String getPackageNameOfCurrentTest() { return BlobTest.class.getPackage().getName(); } } \ No newline at end of file diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/elementcollection/ElementCollectionTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/elementcollection/ElementCollectionTest.java index caf6a37f20..6977acb0d8 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/elementcollection/ElementCollectionTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/elementcollection/ElementCollectionTest.java @@ -78,7 +78,7 @@ public class ElementCollectionTest extends CompilationTest { } @Override - protected String getPackageNameOfTestSources() { + protected String getPackageNameOfCurrentTest() { return ElementCollectionTest.class.getPackage().getName(); } diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/embeddablemappedsuperclass/EmbeddableMappedSuperClassTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/embeddablemappedsuperclass/EmbeddableMappedSuperClassTest.java index c3e3a6b6f2..cf66373d67 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/embeddablemappedsuperclass/EmbeddableMappedSuperClassTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/embeddablemappedsuperclass/EmbeddableMappedSuperClassTest.java @@ -40,7 +40,7 @@ public class EmbeddableMappedSuperClassTest extends CompilationTest { } @Override - protected String getPackageNameOfTestSources() { + protected String getPackageNameOfCurrentTest() { return EmbeddableMappedSuperClassTest.class.getPackage().getName(); } } \ No newline at end of file diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest.java index dda6061987..31fdd1eae2 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest.java @@ -42,7 +42,7 @@ public class GeneratedAnnotationTest extends CompilationTest { } @Override - protected String getPackageNameOfTestSources() { + protected String getPackageNameOfCurrentTest() { return GeneratedAnnotationTest.class.getPackage().getName(); } } \ No newline at end of file diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest2.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest2.java index 8dac48d90c..4eee9d9772 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest2.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest2.java @@ -56,7 +56,7 @@ public class GeneratedAnnotationTest2 extends CompilationTest { } @Override - protected String getPackageNameOfTestSources() { + protected String getPackageNameOfCurrentTest() { return GeneratedAnnotationTest2.class.getPackage().getName(); } } \ No newline at end of file diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/generics/GenericsTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/generics/GenericsTest.java index 2de42488cb..5c06a07259 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/generics/GenericsTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/generics/GenericsTest.java @@ -37,7 +37,7 @@ public class GenericsTest extends CompilationTest { } @Override - protected String getPackageNameOfTestSources() { + protected String getPackageNameOfCurrentTest() { return GenericsTest.class.getPackage().getName(); } } \ No newline at end of file diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/inheritance/InheritanceTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/inheritance/InheritanceTest.java index 95efc2f529..28671ca959 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/inheritance/InheritanceTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/inheritance/InheritanceTest.java @@ -52,7 +52,7 @@ public class InheritanceTest extends CompilationTest { } @Override - protected String getPackageNameOfTestSources() { + protected String getPackageNameOfCurrentTest() { return InheritanceTest.class.getPackage().getName(); } } diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/MixedConfigurationTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/MixedConfigurationTest.java index 3656683fc5..94131decde 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/MixedConfigurationTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/MixedConfigurationTest.java @@ -96,7 +96,7 @@ public class MixedConfigurationTest extends CompilationTest { } @Override - protected String getPackageNameOfTestSources() { + protected String getPackageNameOfCurrentTest() { return MixedConfigurationTest.class.getPackage().getName(); } diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/XmlMetaCompleteTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/XmlMetaCompleteTest.java index c2f48e05a2..afae5e68b6 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/XmlMetaCompleteTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/XmlMetaCompleteTest.java @@ -42,7 +42,7 @@ public class XmlMetaCompleteTest extends CompilationTest { } @Override - protected String getPackageNameOfTestSources() { + protected String getPackageNameOfCurrentTest() { return XmlMetaCompleteTest.class.getPackage().getName(); } diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/rawtypes/RawTypesTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/rawtypes/RawTypesTest.java index 97bcaad680..4dcf8db7a6 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/rawtypes/RawTypesTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/rawtypes/RawTypesTest.java @@ -37,7 +37,7 @@ public class RawTypesTest extends CompilationTest { } @Override - protected String getPackageNameOfTestSources() { + protected String getPackageNameOfCurrentTest() { return DeskWithRawType.class.getPackage().getName(); } } \ No newline at end of file diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/separatecompilationunits/Entity.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/separatecompilationunits/Entity.java new file mode 100644 index 0000000000..daed039fc5 --- /dev/null +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/separatecompilationunits/Entity.java @@ -0,0 +1,31 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2010, Red Hat Middleware LLC, and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * 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. + */ + +// $Id:$ +package org.hibernate.jpamodelgen.test.separatecompilationunits; + +import org.hibernate.jpamodelgen.test.separatecompilationunits.superclass.MappedSuperclass; + +/** + * @author Hardy Ferentschik + */ +@javax.persistence.Entity +public class Entity extends MappedSuperclass { + private String name; +} + + diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/separatecompilationunits/SeparateCompilationUnitsTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/separatecompilationunits/SeparateCompilationUnitsTest.java new file mode 100644 index 0000000000..cafa71da5b --- /dev/null +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/separatecompilationunits/SeparateCompilationUnitsTest.java @@ -0,0 +1,68 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2010, Red Hat Middleware LLC, and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * 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. + */ + +// $Id$ + +package org.hibernate.jpamodelgen.test.separatecompilationunits; + +import java.io.File; +import java.util.List; + +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import org.hibernate.jpamodelgen.test.util.CompilationTest; + +import static org.hibernate.jpamodelgen.test.util.TestUtil.getMetaModelSourceAsString; +import static org.testng.Assert.assertTrue; + +/** + * @author Hardy Ferentschik + * @see METAGEN-35 + */ +public class SeparateCompilationUnitsTest extends CompilationTest { + @Test + public void testInheritance() throws Exception { + // need to work with the source file. Entity_.class won't get generated, because the mapped superclass + // will not be on the classpath + String entityMetaModel = getMetaModelSourceAsString( Entity.class ); + assertTrue( + entityMetaModel.contains( + "extends org.hibernate.jpamodelgen.test.separatecompilationunits.superclass.MappedSuperclass" + ) + ); + } + + @Override + @BeforeClass + // override compileAllTestEntities to compile the mapped super class explicitly + protected void compileAllTestEntities() throws Exception { + String superClassPackageName = getPackageNameOfCurrentTest() + ".superclass"; + List sourceFiles = getCompilationUnits( + CompilationTest.getSourceBaseDir(), superClassPackageName + ); + compile( sourceFiles, superClassPackageName ); + + sourceFiles = getCompilationUnits( getSourceBaseDir(), getPackageNameOfCurrentTest() ); + compile( sourceFiles, getPackageNameOfCurrentTest() ); + } + + @Override + protected String getPackageNameOfCurrentTest() { + return SeparateCompilationUnitsTest.class.getPackage().getName(); + } +} diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/separatecompilationunits/superclass/MappedSuperclass.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/separatecompilationunits/superclass/MappedSuperclass.java new file mode 100644 index 0000000000..e16bb23aff --- /dev/null +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/separatecompilationunits/superclass/MappedSuperclass.java @@ -0,0 +1,32 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2010, Red Hat Middleware LLC, and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * 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. + */ + +// $Id:$ +package org.hibernate.jpamodelgen.test.separatecompilationunits.superclass; + +import javax.persistence.Id; + +/** + * @author Hardy Ferentschik + */ +@javax.persistence.MappedSuperclass +public class MappedSuperclass { + @Id + private long id; +} + + diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/targetannotation/TargetAnnotationTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/targetannotation/TargetAnnotationTest.java index ddcc2144a3..da44b0e01a 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/targetannotation/TargetAnnotationTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/targetannotation/TargetAnnotationTest.java @@ -42,7 +42,7 @@ public class TargetAnnotationTest extends CompilationTest { } @Override - protected String getPackageNameOfTestSources() { + protected String getPackageNameOfCurrentTest() { return TargetAnnotationTest.class.getPackage().getName(); } } \ No newline at end of file diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/util/CompilationTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/util/CompilationTest.java index 8e697a70dd..41adf2c5c2 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/util/CompilationTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/util/CompilationTest.java @@ -49,7 +49,6 @@ import static org.testng.FileAssert.fail; */ public abstract class CompilationTest { private static final Logger log = LoggerFactory.getLogger( CompilationTest.class ); - private static final String PATH_SEPARATOR = System.getProperty( "file.separator" ); private static final String ANNOTATION_PROCESSOR_OPTION_PREFIX = "-A"; private static final String PROC_NONE = "-proc:none"; private static final String SOURCE_BASE_DIR_PROPERTY = "sourceBaseDir"; @@ -57,6 +56,8 @@ public abstract class CompilationTest { private static final String sourceBaseDir; private static final String outBaseDir; + public static final String PATH_SEPARATOR = System.getProperty( "file.separator" ); + private List compilationDiagnostics; static { @@ -81,16 +82,34 @@ public abstract class CompilationTest { return compilationDiagnostics; } + public static String getSourceBaseDir() { + return sourceBaseDir; + } + @BeforeClass - protected void compile() throws Exception { + protected void compileAllTestEntities() throws Exception { + List sourceFiles = getCompilationUnits( sourceBaseDir, getPackageNameOfCurrentTest() ); + // make sure there are no relics from previous runs TestUtil.deleteGeneratedSourceFiles( new File( outBaseDir ) ); + compile( sourceFiles, getPackageNameOfCurrentTest() ); + } + + /** + * Compiles the specified Java classes and generated the meta model java files which in turn get also compiled. + * + * @param sourceFiles the files containing the java source files to compile. + * @param packageName the package name of the source files + * + * @throws Exception in case the compilation fails + */ + protected void compile(List sourceFiles, String packageName) throws Exception { List options = createJavaOptions(); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector diagnostics = new DiagnosticCollector(); StandardJavaFileManager fileManager = compiler.getStandardFileManager( diagnostics, null, null ); Iterable compilationUnits = fileManager.getJavaFileObjectsFromFiles( - getCompilationUnits( sourceBaseDir ) + sourceFiles ); // TODO - need to call the compiler twice. Once to compile the test classes and generate the java files @@ -99,7 +118,7 @@ public abstract class CompilationTest { compileSources( options, compiler, diagnostics, fileManager, compilationUnits ); compilationUnits = fileManager.getJavaFileObjectsFromFiles( - getCompilationUnits( outBaseDir ) + getCompilationUnits( outBaseDir, packageName ) ); options.add( PROC_NONE ); // for the second compile skip the processor compileSources( options, compiler, diagnostics, fileManager, compilationUnits ); @@ -107,6 +126,34 @@ public abstract class CompilationTest { fileManager.close(); } + protected List getCompilationUnits(String baseDir, String packageName) { + List javaFiles = new ArrayList(); + String packageDirName = baseDir + PATH_SEPARATOR + packageName.replace( ".", PATH_SEPARATOR ); + File packageDir = new File( packageDirName ); + FilenameFilter javaFileFilter = new FilenameFilter() { + @Override + public boolean accept(File dir, String name) { + return name.endsWith( ".java" ) && !name.endsWith( "Test.java" ); + } + }; + final File[] files = packageDir.listFiles( javaFileFilter ); + if ( files == null ) { + throw new RuntimeException( "Cannot find package directory (is your base dir correct?): " + packageDirName ); + } + javaFiles.addAll( Arrays.asList( files ) ); + return javaFiles; + } + + abstract protected String getPackageNameOfCurrentTest(); + + protected Map getProcessorOptions() { + return Collections.emptyMap(); + } + + protected Collection getOrmFiles() { + return Collections.emptyList(); + } + private void compileSources(List options, JavaCompiler compiler, DiagnosticCollector diagnostics, StandardJavaFileManager fileManager, Iterable compilationUnits) { JavaCompiler.CompilationTask task = compiler.getTask( null, fileManager, diagnostics, options, null, compilationUnits @@ -147,34 +194,6 @@ public abstract class CompilationTest { } return options; } - - private List getCompilationUnits(String baseDir) { - List javaFiles = new ArrayList(); - String packageDirName = baseDir + PATH_SEPARATOR + getPackageNameOfTestSources().replace( ".", PATH_SEPARATOR ); - File packageDir = new File( packageDirName ); - FilenameFilter javaFileFilter = new FilenameFilter() { - @Override - public boolean accept(File dir, String name) { - return name.endsWith( ".java" ) && !name.endsWith( "Test.java" ); - } - }; - final File[] files = packageDir.listFiles( javaFileFilter ); - if ( files == null ) { - throw new RuntimeException( "Cannot find package directory (is your base dir correct?): " + packageDirName ); - } - javaFiles.addAll( Arrays.asList( files ) ); - return javaFiles; - } - - abstract protected String getPackageNameOfTestSources(); - - protected Map getProcessorOptions() { - return Collections.emptyMap(); - } - - protected Collection getOrmFiles() { - return Collections.emptyList(); - } } diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/IgnoreInvalidXmlTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/IgnoreInvalidXmlTest.java index 33147da285..e72498e0fc 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/IgnoreInvalidXmlTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/IgnoreInvalidXmlTest.java @@ -42,7 +42,7 @@ public class IgnoreInvalidXmlTest extends CompilationTest { } @Override - protected String getPackageNameOfTestSources() { + protected String getPackageNameOfCurrentTest() { return IgnoreInvalidXmlTest.class.getPackage().getName(); } diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/XmlMappingTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/XmlMappingTest.java index bbab03fed3..778b9f64e9 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/XmlMappingTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/XmlMappingTest.java @@ -104,7 +104,7 @@ public class XmlMappingTest extends CompilationTest { } @Override - protected String getPackageNameOfTestSources() { + protected String getPackageNameOfCurrentTest() { return XmlMappingTest.class.getPackage().getName(); } diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlmetacomplete/XmlMetaDataCompleteTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlmetacomplete/XmlMetaDataCompleteTest.java index e9e3df220c..82674f2f61 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlmetacomplete/XmlMetaDataCompleteTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlmetacomplete/XmlMetaDataCompleteTest.java @@ -42,7 +42,7 @@ public class XmlMetaDataCompleteTest extends CompilationTest { } @Override - protected String getPackageNameOfTestSources() { + protected String getPackageNameOfCurrentTest() { return XmlMetaDataCompleteTest.class.getPackage().getName(); } diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlonly/XmlOnlyTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlonly/XmlOnlyTest.java index 719a802a1c..6c05858460 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlonly/XmlOnlyTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlonly/XmlOnlyTest.java @@ -40,7 +40,7 @@ public class XmlOnlyTest extends CompilationTest { } @Override - protected String getPackageNameOfTestSources() { + protected String getPackageNameOfCurrentTest() { return XmlOnlyTest.class.getPackage().getName(); }