METAGEN-35 Actual code changes and tests for the issue.
This commit is contained in:
parent
d01ae40533
commit
9dcc29c6ef
|
@ -31,6 +31,8 @@ import javax.lang.model.element.TypeElement;
|
||||||
import javax.lang.model.type.DeclaredType;
|
import javax.lang.model.type.DeclaredType;
|
||||||
import javax.lang.model.type.TypeKind;
|
import javax.lang.model.type.TypeKind;
|
||||||
import javax.lang.model.type.TypeMirror;
|
import javax.lang.model.type.TypeMirror;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.MappedSuperclass;
|
||||||
import javax.tools.Diagnostic;
|
import javax.tools.Diagnostic;
|
||||||
import javax.tools.FileObject;
|
import javax.tools.FileObject;
|
||||||
|
|
||||||
|
@ -38,9 +40,13 @@ import org.hibernate.jpamodelgen.model.MetaAttribute;
|
||||||
import org.hibernate.jpamodelgen.model.MetaEntity;
|
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 Emmanuel Bernard
|
||||||
|
* @author Hardy Ferentschik
|
||||||
*/
|
*/
|
||||||
public final class ClassWriter {
|
public final class ClassWriter {
|
||||||
|
private static final String META_MODEL_CLASS_NAME_SUFFIX = "_";
|
||||||
|
|
||||||
private ClassWriter() {
|
private ClassWriter() {
|
||||||
}
|
}
|
||||||
|
@ -51,7 +57,7 @@ public final class ClassWriter {
|
||||||
StringBuffer body = generateBody( entity, context );
|
StringBuffer body = generateBody( entity, context );
|
||||||
|
|
||||||
FileObject fo = context.getProcessingEnvironment().getFiler().createSourceFile(
|
FileObject fo = context.getProcessingEnvironment().getFiler().createSourceFile(
|
||||||
metaModelPackage + "." + entity.getSimpleName() + "_"
|
getFullyQualifiedClassName( entity, metaModelPackage )
|
||||||
);
|
);
|
||||||
OutputStream os = fo.openOutputStream();
|
OutputStream os = fo.openOutputStream();
|
||||||
PrintWriter pw = new PrintWriter( os );
|
PrintWriter pw = new PrintWriter( os );
|
||||||
|
@ -63,7 +69,6 @@ public final class ClassWriter {
|
||||||
|
|
||||||
pw.flush();
|
pw.flush();
|
||||||
pw.close();
|
pw.close();
|
||||||
|
|
||||||
}
|
}
|
||||||
catch ( FilerException filerEx ) {
|
catch ( FilerException filerEx ) {
|
||||||
context.logMessage(
|
context.logMessage(
|
||||||
|
@ -87,15 +92,14 @@ public final class ClassWriter {
|
||||||
* @return body content
|
* @return body content
|
||||||
*/
|
*/
|
||||||
private static StringBuffer generateBody(MetaEntity entity, Context context) {
|
private static StringBuffer generateBody(MetaEntity entity, Context context) {
|
||||||
|
|
||||||
StringWriter sw = new StringWriter();
|
StringWriter sw = new StringWriter();
|
||||||
PrintWriter pw = null;
|
PrintWriter pw = null;
|
||||||
try {
|
try {
|
||||||
pw = new PrintWriter( sw );
|
pw = new PrintWriter( sw );
|
||||||
if ( context.isAddGeneratedAnnotation() ) {
|
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 );
|
printClassDeclaration( entity, pw, context );
|
||||||
pw.println();
|
pw.println();
|
||||||
List<MetaAttribute> members = entity.getMembers();
|
List<MetaAttribute> members = entity.getMembers();
|
||||||
|
@ -114,19 +118,62 @@ public final class ClassWriter {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void printClassDeclaration(MetaEntity entity, PrintWriter pw, Context context) {
|
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();
|
final TypeMirror superClass = entity.getTypeElement().getSuperclass();
|
||||||
//superclass of Object is of NoType which returns some other kind
|
//superclass of Object is of NoType which returns some other kind
|
||||||
if ( superClass.getKind() == TypeKind.DECLARED ) {
|
if ( superClass.getKind() == TypeKind.DECLARED ) {
|
||||||
//F..king Ch...t Have those people used their horrible APIs even once?
|
//F..king Ch...t Have those people used their horrible APIs even once?
|
||||||
final Element superClassElement = ( ( DeclaredType ) superClass ).asElement();
|
final Element superClassElement = ( (DeclaredType) superClass ).asElement();
|
||||||
String superClassName = ( ( TypeElement ) superClassElement ).getQualifiedName().toString();
|
String superClassName = ( (TypeElement) superClassElement ).getQualifiedName().toString();
|
||||||
if ( context.containsMetaEntity( superClassName )
|
if ( extendsSuperMetaModel( superClassElement, entity.isMetaComplete(), context ) ) {
|
||||||
|| context.containsMetaEmbeddable( superClassName ) ) {
|
pw.print( " extends " + superClassName + META_MODEL_CLASS_NAME_SUFFIX );
|
||||||
pw.print( " extends " + superClassName + "_" );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pw.println( " {" );
|
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)";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,7 @@ import java.util.Map;
|
||||||
/**
|
/**
|
||||||
* @author Hardy Ferentschik
|
* @author Hardy Ferentschik
|
||||||
*/
|
*/
|
||||||
public class Constants {
|
public final class Constants {
|
||||||
public static Map<String, String> COLLECTIONS = new HashMap<String, String>();
|
public static Map<String, String> COLLECTIONS = new HashMap<String, String>();
|
||||||
|
|
||||||
static {
|
static {
|
||||||
|
|
|
@ -22,7 +22,7 @@ package org.hibernate.jpamodelgen.util;
|
||||||
/**
|
/**
|
||||||
* @author Hardy Ferentschik
|
* @author Hardy Ferentschik
|
||||||
*/
|
*/
|
||||||
public class StringUtil {
|
public final class StringUtil {
|
||||||
private static final String NAME_SEPARATOR = ".";
|
private static final String NAME_SEPARATOR = ".";
|
||||||
private static final String PROPERTY_PREFIX_GET = "get";
|
private static final String PROPERTY_PREFIX_GET = "get";
|
||||||
private static final String PROPERTY_PREFIX_IS = "is";
|
private static final String PROPERTY_PREFIX_IS = "is";
|
||||||
|
@ -63,16 +63,17 @@ public class StringUtil {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String tmp = name;
|
||||||
if ( name.startsWith( PROPERTY_PREFIX_GET ) ) {
|
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 ) ) {
|
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 ) ) {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -124,7 +124,7 @@ public class XmlMetaEntity implements MetaEntity {
|
||||||
this.isMetaComplete = initIsMetaComplete( metaComplete );
|
this.isMetaComplete = initIsMetaComplete( metaComplete );
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void init() {
|
protected final void init() {
|
||||||
this.accessTypeInfo = context.getAccessTypeInfo( getQualifiedName() );
|
this.accessTypeInfo = context.getAccessTypeInfo( getQualifiedName() );
|
||||||
if ( attributes != null ) {
|
if ( attributes != null ) {
|
||||||
parseAttributes( attributes );
|
parseAttributes( attributes );
|
||||||
|
|
|
@ -99,7 +99,7 @@ public class AccessTypeTest extends CompilationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getPackageNameOfTestSources() {
|
protected String getPackageNameOfCurrentTest() {
|
||||||
return AccessTypeTest.class.getPackage().getName();
|
return AccessTypeTest.class.getPackage().getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,7 @@ public class ArrayTest extends CompilationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getPackageNameOfTestSources() {
|
protected String getPackageNameOfCurrentTest() {
|
||||||
return Image.class.getPackage().getName();
|
return Image.class.getPackage().getName();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -40,7 +40,7 @@ public class BlobTest extends CompilationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getPackageNameOfTestSources() {
|
protected String getPackageNameOfCurrentTest() {
|
||||||
return BlobTest.class.getPackage().getName();
|
return BlobTest.class.getPackage().getName();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -78,7 +78,7 @@ public class ElementCollectionTest extends CompilationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getPackageNameOfTestSources() {
|
protected String getPackageNameOfCurrentTest() {
|
||||||
return ElementCollectionTest.class.getPackage().getName();
|
return ElementCollectionTest.class.getPackage().getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,7 @@ public class EmbeddableMappedSuperClassTest extends CompilationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getPackageNameOfTestSources() {
|
protected String getPackageNameOfCurrentTest() {
|
||||||
return EmbeddableMappedSuperClassTest.class.getPackage().getName();
|
return EmbeddableMappedSuperClassTest.class.getPackage().getName();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -42,7 +42,7 @@ public class GeneratedAnnotationTest extends CompilationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getPackageNameOfTestSources() {
|
protected String getPackageNameOfCurrentTest() {
|
||||||
return GeneratedAnnotationTest.class.getPackage().getName();
|
return GeneratedAnnotationTest.class.getPackage().getName();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -56,7 +56,7 @@ public class GeneratedAnnotationTest2 extends CompilationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getPackageNameOfTestSources() {
|
protected String getPackageNameOfCurrentTest() {
|
||||||
return GeneratedAnnotationTest2.class.getPackage().getName();
|
return GeneratedAnnotationTest2.class.getPackage().getName();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -37,7 +37,7 @@ public class GenericsTest extends CompilationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getPackageNameOfTestSources() {
|
protected String getPackageNameOfCurrentTest() {
|
||||||
return GenericsTest.class.getPackage().getName();
|
return GenericsTest.class.getPackage().getName();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -52,7 +52,7 @@ public class InheritanceTest extends CompilationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getPackageNameOfTestSources() {
|
protected String getPackageNameOfCurrentTest() {
|
||||||
return InheritanceTest.class.getPackage().getName();
|
return InheritanceTest.class.getPackage().getName();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -96,7 +96,7 @@ public class MixedConfigurationTest extends CompilationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getPackageNameOfTestSources() {
|
protected String getPackageNameOfCurrentTest() {
|
||||||
return MixedConfigurationTest.class.getPackage().getName();
|
return MixedConfigurationTest.class.getPackage().getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,7 @@ public class XmlMetaCompleteTest extends CompilationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getPackageNameOfTestSources() {
|
protected String getPackageNameOfCurrentTest() {
|
||||||
return XmlMetaCompleteTest.class.getPackage().getName();
|
return XmlMetaCompleteTest.class.getPackage().getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -37,7 +37,7 @@ public class RawTypesTest extends CompilationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getPackageNameOfTestSources() {
|
protected String getPackageNameOfCurrentTest() {
|
||||||
return DeskWithRawType.class.getPackage().getName();
|
return DeskWithRawType.class.getPackage().getName();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -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<File> sourceFiles = getCompilationUnits(
|
||||||
|
CompilationTest.getSourceBaseDir(), superClassPackageName
|
||||||
|
);
|
||||||
|
compile( sourceFiles, superClassPackageName );
|
||||||
|
|
||||||
|
sourceFiles = getCompilationUnits( getSourceBaseDir(), getPackageNameOfCurrentTest() );
|
||||||
|
compile( sourceFiles, getPackageNameOfCurrentTest() );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getPackageNameOfCurrentTest() {
|
||||||
|
return SeparateCompilationUnitsTest.class.getPackage().getName();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,7 @@ public class TargetAnnotationTest extends CompilationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getPackageNameOfTestSources() {
|
protected String getPackageNameOfCurrentTest() {
|
||||||
return TargetAnnotationTest.class.getPackage().getName();
|
return TargetAnnotationTest.class.getPackage().getName();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -49,7 +49,6 @@ import static org.testng.FileAssert.fail;
|
||||||
*/
|
*/
|
||||||
public abstract class CompilationTest {
|
public abstract class CompilationTest {
|
||||||
private static final Logger log = LoggerFactory.getLogger( CompilationTest.class );
|
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 ANNOTATION_PROCESSOR_OPTION_PREFIX = "-A";
|
||||||
private static final String PROC_NONE = "-proc:none";
|
private static final String PROC_NONE = "-proc:none";
|
||||||
private static final String SOURCE_BASE_DIR_PROPERTY = "sourceBaseDir";
|
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 sourceBaseDir;
|
||||||
private static final String outBaseDir;
|
private static final String outBaseDir;
|
||||||
|
|
||||||
|
public static final String PATH_SEPARATOR = System.getProperty( "file.separator" );
|
||||||
|
|
||||||
private List<Diagnostic> compilationDiagnostics;
|
private List<Diagnostic> compilationDiagnostics;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
|
@ -81,16 +82,34 @@ public abstract class CompilationTest {
|
||||||
return compilationDiagnostics;
|
return compilationDiagnostics;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getSourceBaseDir() {
|
||||||
|
return sourceBaseDir;
|
||||||
|
}
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
protected void compile() throws Exception {
|
protected void compileAllTestEntities() throws Exception {
|
||||||
|
List<File> sourceFiles = getCompilationUnits( sourceBaseDir, getPackageNameOfCurrentTest() );
|
||||||
|
// make sure there are no relics from previous runs
|
||||||
TestUtil.deleteGeneratedSourceFiles( new File( outBaseDir ) );
|
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<File> sourceFiles, String packageName) throws Exception {
|
||||||
List<String> options = createJavaOptions();
|
List<String> options = createJavaOptions();
|
||||||
|
|
||||||
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
||||||
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
|
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
|
||||||
StandardJavaFileManager fileManager = compiler.getStandardFileManager( diagnostics, null, null );
|
StandardJavaFileManager fileManager = compiler.getStandardFileManager( diagnostics, null, null );
|
||||||
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(
|
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(
|
||||||
getCompilationUnits( sourceBaseDir )
|
sourceFiles
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO - need to call the compiler twice. Once to compile the test classes and generate the java files
|
// 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 );
|
compileSources( options, compiler, diagnostics, fileManager, compilationUnits );
|
||||||
|
|
||||||
compilationUnits = fileManager.getJavaFileObjectsFromFiles(
|
compilationUnits = fileManager.getJavaFileObjectsFromFiles(
|
||||||
getCompilationUnits( outBaseDir )
|
getCompilationUnits( outBaseDir, packageName )
|
||||||
);
|
);
|
||||||
options.add( PROC_NONE ); // for the second compile skip the processor
|
options.add( PROC_NONE ); // for the second compile skip the processor
|
||||||
compileSources( options, compiler, diagnostics, fileManager, compilationUnits );
|
compileSources( options, compiler, diagnostics, fileManager, compilationUnits );
|
||||||
|
@ -107,6 +126,34 @@ public abstract class CompilationTest {
|
||||||
fileManager.close();
|
fileManager.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected List<File> getCompilationUnits(String baseDir, String packageName) {
|
||||||
|
List<File> javaFiles = new ArrayList<File>();
|
||||||
|
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<String, String> getProcessorOptions() {
|
||||||
|
return Collections.emptyMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Collection<String> getOrmFiles() {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
private void compileSources(List<String> options, JavaCompiler compiler, DiagnosticCollector<JavaFileObject> diagnostics, StandardJavaFileManager fileManager, Iterable<? extends JavaFileObject> compilationUnits) {
|
private void compileSources(List<String> options, JavaCompiler compiler, DiagnosticCollector<JavaFileObject> diagnostics, StandardJavaFileManager fileManager, Iterable<? extends JavaFileObject> compilationUnits) {
|
||||||
JavaCompiler.CompilationTask task = compiler.getTask(
|
JavaCompiler.CompilationTask task = compiler.getTask(
|
||||||
null, fileManager, diagnostics, options, null, compilationUnits
|
null, fileManager, diagnostics, options, null, compilationUnits
|
||||||
|
@ -147,34 +194,6 @@ public abstract class CompilationTest {
|
||||||
}
|
}
|
||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<File> getCompilationUnits(String baseDir) {
|
|
||||||
List<File> javaFiles = new ArrayList<File>();
|
|
||||||
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<String, String> getProcessorOptions() {
|
|
||||||
return Collections.emptyMap();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Collection<String> getOrmFiles() {
|
|
||||||
return Collections.emptyList();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,7 @@ public class IgnoreInvalidXmlTest extends CompilationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getPackageNameOfTestSources() {
|
protected String getPackageNameOfCurrentTest() {
|
||||||
return IgnoreInvalidXmlTest.class.getPackage().getName();
|
return IgnoreInvalidXmlTest.class.getPackage().getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -104,7 +104,7 @@ public class XmlMappingTest extends CompilationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getPackageNameOfTestSources() {
|
protected String getPackageNameOfCurrentTest() {
|
||||||
return XmlMappingTest.class.getPackage().getName();
|
return XmlMappingTest.class.getPackage().getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,7 @@ public class XmlMetaDataCompleteTest extends CompilationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getPackageNameOfTestSources() {
|
protected String getPackageNameOfCurrentTest() {
|
||||||
return XmlMetaDataCompleteTest.class.getPackage().getName();
|
return XmlMetaDataCompleteTest.class.getPackage().getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,7 @@ public class XmlOnlyTest extends CompilationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getPackageNameOfTestSources() {
|
protected String getPackageNameOfCurrentTest() {
|
||||||
return XmlOnlyTest.class.getPackage().getName();
|
return XmlOnlyTest.class.getPackage().getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue