METAGEN-36
This commit is contained in:
parent
ba43b863f5
commit
d01ae40533
|
@ -40,7 +40,7 @@ import org.hibernate.jpamodelgen.util.Constants;
|
||||||
* @author Hardy Ferentschik
|
* @author Hardy Ferentschik
|
||||||
* @author Emmanuel Bernard
|
* @author Emmanuel Bernard
|
||||||
*/
|
*/
|
||||||
public class Context {
|
public final class Context {
|
||||||
private static final String DEFAULT_PERSISTENCE_XML_LOCATION = "/META-INF/persistence.xml";
|
private static final String DEFAULT_PERSISTENCE_XML_LOCATION = "/META-INF/persistence.xml";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
package org.hibernate.jpamodelgen;
|
package org.hibernate.jpamodelgen;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -139,19 +140,28 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createMetaModelClasses() {
|
private void createMetaModelClasses() {
|
||||||
|
// keep track of all classes for which model have been generated
|
||||||
|
Collection<String> generatedModelClasses = new ArrayList<String>();
|
||||||
|
|
||||||
for ( MetaEntity entity : context.getMetaEntities() ) {
|
for ( MetaEntity entity : context.getMetaEntities() ) {
|
||||||
context.logMessage( Diagnostic.Kind.OTHER, "Writing meta model for entity " + entity );
|
context.logMessage( Diagnostic.Kind.OTHER, "Writing meta model for entity " + entity );
|
||||||
ClassWriter.writeFile( entity, context );
|
ClassWriter.writeFile( entity, context );
|
||||||
|
generatedModelClasses.add( entity.getQualifiedName() );
|
||||||
}
|
}
|
||||||
|
|
||||||
// we cannot process the delayed entities in any order. There might be dependencies between them.
|
// we cannot process the delayed entities in any order. There might be dependencies between them.
|
||||||
// we need to process the top level entities first
|
// we need to process the top level entities first
|
||||||
// TODO make sure that we don't run into circular dependencies here
|
|
||||||
Collection<MetaEntity> toProcessEntities = context.getMetaEmbeddables();
|
Collection<MetaEntity> toProcessEntities = context.getMetaEmbeddables();
|
||||||
while ( !toProcessEntities.isEmpty() ) {
|
while ( !toProcessEntities.isEmpty() ) {
|
||||||
Set<MetaEntity> processedEntities = new HashSet<MetaEntity>();
|
Set<MetaEntity> processedEntities = new HashSet<MetaEntity>();
|
||||||
|
int toProcessCountBeforeLoop = toProcessEntities.size();
|
||||||
for ( MetaEntity entity : toProcessEntities ) {
|
for ( MetaEntity entity : toProcessEntities ) {
|
||||||
if ( containedInEntity( toProcessEntities, entity ) ) {
|
// see METAGEN-36
|
||||||
|
if ( generatedModelClasses.contains( entity.getQualifiedName() ) ) {
|
||||||
|
toProcessEntities.remove( entity );
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ( modelGenerationNeedsToBeDeferred( toProcessEntities, entity ) ) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
context.logMessage(
|
context.logMessage(
|
||||||
|
@ -161,10 +171,15 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
|
||||||
processedEntities.add( entity );
|
processedEntities.add( entity );
|
||||||
}
|
}
|
||||||
toProcessEntities.removeAll( processedEntities );
|
toProcessEntities.removeAll( processedEntities );
|
||||||
|
if ( toProcessEntities.size() >= toProcessCountBeforeLoop ) {
|
||||||
|
context.logMessage(
|
||||||
|
Diagnostic.Kind.ERROR, "Potential endless loop in generation of entities."
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean containedInEntity(Collection<MetaEntity> entities, MetaEntity containedEntity) {
|
private boolean modelGenerationNeedsToBeDeferred(Collection<MetaEntity> entities, MetaEntity containedEntity) {
|
||||||
ContainsAttributeTypeVisitor visitor = new ContainsAttributeTypeVisitor(
|
ContainsAttributeTypeVisitor visitor = new ContainsAttributeTypeVisitor(
|
||||||
containedEntity.getTypeElement(), context
|
containedEntity.getTypeElement(), context
|
||||||
);
|
);
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
/*
|
||||||
|
* 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.embeddablemappedsuperclass;
|
||||||
|
|
||||||
|
import javax.persistence.Embeddable;
|
||||||
|
import javax.persistence.MappedSuperclass;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Hardy Ferentschik
|
||||||
|
*/
|
||||||
|
@Embeddable
|
||||||
|
@MappedSuperclass
|
||||||
|
public class EmbeddableAndMappedSuperClass {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,46 @@
|
||||||
|
/*
|
||||||
|
* 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: BlobTest.java 20721 2010-09-27 12:40:10Z hardy.ferentschik $
|
||||||
|
|
||||||
|
package org.hibernate.jpamodelgen.test.embeddablemappedsuperclass;
|
||||||
|
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import org.hibernate.jpamodelgen.test.util.CompilationTest;
|
||||||
|
|
||||||
|
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMetamodelClassGeneratedFor;
|
||||||
|
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertNoCompilationError;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Hardy Ferentschik
|
||||||
|
*/
|
||||||
|
public class EmbeddableMappedSuperClassTest extends CompilationTest {
|
||||||
|
/**
|
||||||
|
* METAGEN-36
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testMetaModelsGenerated() {
|
||||||
|
assertMetamodelClassGeneratedFor( EmbeddableAndMappedSuperClass.class );
|
||||||
|
assertNoCompilationError(getCompilationDiagnostics());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getPackageNameOfTestSources() {
|
||||||
|
return EmbeddableMappedSuperClassTest.class.getPackage().getName();
|
||||||
|
}
|
||||||
|
}
|
|
@ -43,17 +43,22 @@ import org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor;
|
||||||
import static org.testng.FileAssert.fail;
|
import static org.testng.FileAssert.fail;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Base class for annotation processor tests.
|
||||||
|
*
|
||||||
* @author Hardy Ferentschik
|
* @author Hardy Ferentschik
|
||||||
*/
|
*/
|
||||||
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 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 SOURCE_BASE_DIR_PROPERTY = "sourceBaseDir";
|
private static final String SOURCE_BASE_DIR_PROPERTY = "sourceBaseDir";
|
||||||
private static final String OUT_BASE_DIR_PROPERTY = "outBaseDir";
|
private static final String OUT_BASE_DIR_PROPERTY = "outBaseDir";
|
||||||
private static final String sourceBaseDir;
|
private static final String sourceBaseDir;
|
||||||
private static final String outBaseDir;
|
private static final String outBaseDir;
|
||||||
|
|
||||||
|
private List<Diagnostic> compilationDiagnostics;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
String tmp = System.getProperty( SOURCE_BASE_DIR_PROPERTY );
|
String tmp = System.getProperty( SOURCE_BASE_DIR_PROPERTY );
|
||||||
if ( tmp == null ) {
|
if ( tmp == null ) {
|
||||||
|
@ -69,6 +74,11 @@ public abstract class CompilationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
public CompilationTest() {
|
public CompilationTest() {
|
||||||
|
compilationDiagnostics = new ArrayList<Diagnostic>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public final List<Diagnostic> getCompilationDiagnostics() {
|
||||||
|
return compilationDiagnostics;
|
||||||
}
|
}
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
|
@ -91,8 +101,9 @@ public abstract class CompilationTest {
|
||||||
compilationUnits = fileManager.getJavaFileObjectsFromFiles(
|
compilationUnits = fileManager.getJavaFileObjectsFromFiles(
|
||||||
getCompilationUnits( outBaseDir )
|
getCompilationUnits( outBaseDir )
|
||||||
);
|
);
|
||||||
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 );
|
||||||
|
compilationDiagnostics.addAll( diagnostics.getDiagnostics() );
|
||||||
fileManager.close();
|
fileManager.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,8 @@ import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.GenericArrayType;
|
import java.lang.reflect.GenericArrayType;
|
||||||
import java.lang.reflect.ParameterizedType;
|
import java.lang.reflect.ParameterizedType;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.List;
|
||||||
|
import javax.tools.Diagnostic;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -209,10 +211,6 @@ public class TestUtil {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean hasFieldInMetamodelFor(Class<?> clazz, String fieldName) {
|
|
||||||
return getFieldFromMetamodelFor( clazz, fieldName ) != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Field getFieldFromMetamodelFor(Class<?> entityClass, String fieldName) {
|
public static Field getFieldFromMetamodelFor(Class<?> entityClass, String fieldName) {
|
||||||
Class<?> metaModelClass = getMetamodelClassFor( entityClass );
|
Class<?> metaModelClass = getMetamodelClassFor( entityClass );
|
||||||
Field field;
|
Field field;
|
||||||
|
@ -229,6 +227,18 @@ public class TestUtil {
|
||||||
return fcn.replace( PACKAGE_SEPARATOR, PATH_SEPARATOR );
|
return fcn.replace( PACKAGE_SEPARATOR, PATH_SEPARATOR );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void assertNoCompilationError(List<Diagnostic> diagnostics) {
|
||||||
|
for ( Diagnostic diagnostic : diagnostics ) {
|
||||||
|
if ( diagnostic.getKind().equals( Diagnostic.Kind.ERROR ) ) {
|
||||||
|
fail( "There was a compilation error. " + diagnostic.getMessage( null ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean hasFieldInMetamodelFor(Class<?> clazz, String fieldName) {
|
||||||
|
return getFieldFromMetamodelFor( clazz, fieldName ) != null;
|
||||||
|
}
|
||||||
|
|
||||||
private static class MetaModelFilenameFilter implements FileFilter {
|
private static class MetaModelFilenameFilter implements FileFilter {
|
||||||
@Override
|
@Override
|
||||||
public boolean accept(File pathName) {
|
public boolean accept(File pathName) {
|
||||||
|
|
Loading…
Reference in New Issue