From 54411e47b6ea93173703da29a7d45dcfd9abc5b6 Mon Sep 17 00:00:00 2001 From: Hardy Ferentschik Date: Mon, 16 Jan 2012 15:21:33 +0100 Subject: [PATCH] METAGEN-50, METAGEN-63 Added processor option to add @SupressWarnings annotation Updated documentation and made sure that documentation and implementation related to ormXml is in sync --- .../hibernate/jpamodelgen/ClassWriter.java | 7 ++ .../org/hibernate/jpamodelgen/Context.java | 9 +++ .../JPAMetaModelEntityProcessor.java | 10 ++- .../GeneratedAnnotationTest2.java | 1 - .../SeparateCompilationUnitsTest.java | 7 +- ...ppressWarningsAnnotationGeneratedTest.java | 65 +++++++++++++++++++ ...essWarningsAnnotationNotGeneratedTest.java | 50 ++++++++++++++ .../test/supresswarnings/TestEntity.java | 33 ++++++++++ .../test/util/CompilationTest.java | 9 ++- .../jpamodelgen/test/util/TestForIssue.java | 41 ++++++++++++ 10 files changed, 221 insertions(+), 11 deletions(-) create mode 100644 tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/supresswarnings/SuppressWarningsAnnotationGeneratedTest.java create mode 100644 tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/supresswarnings/SuppressWarningsAnnotationNotGeneratedTest.java create mode 100644 tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/supresswarnings/TestEntity.java create mode 100644 tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/util/TestForIssue.java 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 0773e2e220..70c5e1485e 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 @@ -98,6 +98,9 @@ public final class ClassWriter { if ( context.isAddGeneratedAnnotation() ) { pw.println( writeGeneratedAnnotation( entity ) ); } + if ( context.isAddSuppressWarningsAnnotation() ) { + pw.println( writeSuppressWarnings() ); + } pw.println( writeStaticMetaModelAnnotation( entity ) ); printClassDeclaration( entity, pw, context ); pw.println(); @@ -177,6 +180,10 @@ public final class ClassWriter { return "@" + entity.importType( Generated.class.getName() ) + "(\"JPA MetaModel for " + entity.getQualifiedName() + "\")"; } + private static String writeSuppressWarnings() { + return "@SuppressWarnings(\"all\")"; + } + 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/Context.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/Context.java index 6f46fc973d..7c9899313c 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/Context.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/Context.java @@ -61,6 +61,7 @@ public final class Context { private boolean isPersistenceUnitCompletelyXmlConfigured; private boolean addGeneratedAnnotation; + private boolean addSuppressWarningsAnnotation; private AccessType persistenceUnitDefaultAccessType; public Context(ProcessingEnvironment pe) { @@ -107,6 +108,14 @@ public final class Context { this.addGeneratedAnnotation = addGeneratedAnnotation; } + public boolean isAddSuppressWarningsAnnotation() { + return addSuppressWarningsAnnotation; + } + + public void setAddSuppressWarningsAnnotation(boolean addSuppressWarningsAnnotation) { + this.addSuppressWarningsAnnotation = addSuppressWarningsAnnotation; + } + public Elements getElementUtils() { return pe.getElementUtils(); } diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java index 1f800d5ee5..a24b10e72f 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java @@ -66,15 +66,17 @@ import org.hibernate.jpamodelgen.xml.XmlParser; JPAMetaModelEntityProcessor.ORM_XML_OPTION, JPAMetaModelEntityProcessor.FULLY_ANNOTATION_CONFIGURED_OPTION, JPAMetaModelEntityProcessor.LAZY_XML_PARSING, - JPAMetaModelEntityProcessor.ADD_GENERATED_ANNOTATION + JPAMetaModelEntityProcessor.ADD_GENERATED_ANNOTATION, + JPAMetaModelEntityProcessor.ADD_SUPPRESS_WARNINGS_ANNOTATION }) public class JPAMetaModelEntityProcessor extends AbstractProcessor { public static final String DEBUG_OPTION = "debug"; public static final String PERSISTENCE_XML_OPTION = "persistenceXml"; - public static final String ORM_XML_OPTION = "ormXmlList"; + public static final String ORM_XML_OPTION = "ormXml"; public static final String FULLY_ANNOTATION_CONFIGURED_OPTION = "fullyAnnotationConfigured"; public static final String LAZY_XML_PARSING = "lazyXmlParsing"; public static final String ADD_GENERATED_ANNOTATION = "addGeneratedAnnotation"; + public static final String ADD_SUPPRESS_WARNINGS_ANNOTATION = "addSuppressWarningsAnnotation"; private static final Boolean ALLOW_OTHER_PROCESSORS_TO_CLAIM_ANNOTATIONS = Boolean.FALSE; @@ -92,6 +94,10 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor { boolean addGeneratedAnnotation = Boolean.parseBoolean( tmp ); context.setAddGeneratedAnnotation( addGeneratedAnnotation ); + tmp = env.getOptions().get( JPAMetaModelEntityProcessor.ADD_SUPPRESS_WARNINGS_ANNOTATION ); + boolean addSuppressWarningsAnnotation = Boolean.parseBoolean( tmp ); + context.setAddSuppressWarningsAnnotation( addSuppressWarningsAnnotation ); + tmp = env.getOptions().get( JPAMetaModelEntityProcessor.FULLY_ANNOTATION_CONFIGURED_OPTION ); boolean fullyAnnotationConfigured = Boolean.parseBoolean( tmp ); 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 4f8442d8b8..ab9efb978d 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 @@ -34,7 +34,6 @@ import static org.testng.Assert.assertTrue; * @author Hardy Ferentschik */ public class GeneratedAnnotationTest2 extends CompilationTest { - @Test public void testGeneratedAnnotationGenerated() { assertMetamodelClassGeneratedFor( TestEntity.class ); 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 index 07a615d3fe..b5ef7de7a4 100644 --- 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 @@ -23,14 +23,15 @@ import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import org.hibernate.jpamodelgen.test.util.CompilationTest; +import org.hibernate.jpamodelgen.test.util.TestForIssue; import static org.hibernate.jpamodelgen.test.util.TestUtil.getMetaModelSourceAsString; import static org.testng.Assert.assertTrue; /** * @author Hardy Ferentschik - * @see METAGEN-35 */ +@TestForIssue(jiraKey = "METAGEN-35") public class SeparateCompilationUnitsTest extends CompilationTest { @Test public void testInheritance() throws Exception { @@ -52,10 +53,10 @@ public class SeparateCompilationUnitsTest extends CompilationTest { List sourceFiles = getCompilationUnits( CompilationTest.getSourceBaseDir(), superClassPackageName ); - compile( sourceFiles, superClassPackageName ); + compile( sourceFiles ); sourceFiles = getCompilationUnits( getSourceBaseDir(), getPackageNameOfCurrentTest() ); - compile( sourceFiles, getPackageNameOfCurrentTest() ); + compile( sourceFiles ); } @Override diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/supresswarnings/SuppressWarningsAnnotationGeneratedTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/supresswarnings/SuppressWarningsAnnotationGeneratedTest.java new file mode 100644 index 0000000000..8745a95335 --- /dev/null +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/supresswarnings/SuppressWarningsAnnotationGeneratedTest.java @@ -0,0 +1,65 @@ +/* + * 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: GenericsTest.java 20721 2010-09-27 12:40:10Z hardy.ferentschik $ +package org.hibernate.jpamodelgen.test.supresswarnings; + +import java.util.HashMap; +import java.util.Map; + +import org.testng.annotations.Test; + +import org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor; +import org.hibernate.jpamodelgen.test.util.CompilationTest; +import org.hibernate.jpamodelgen.test.util.TestForIssue; + +import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMetamodelClassGeneratedFor; +import static org.hibernate.jpamodelgen.test.util.TestUtil.getMetaModelSourceAsString; +import static org.testng.Assert.assertTrue; + +/** + * @author Hardy Ferentschik + */ +public class SuppressWarningsAnnotationGeneratedTest extends CompilationTest { + @Test + @TestForIssue(jiraKey = "METAGEN-50") + public void testSuppressedWarningsAnnotationGenerated() { + assertMetamodelClassGeneratedFor( TestEntity.class ); + + // need to check the source because @SuppressWarnings is not a runtime annotation + String metaModelSource = getMetaModelSourceAsString( TestEntity.class ); + assertTrue( + metaModelSource.contains( "@SuppressWarnings(\"all\")" ), + "@SuppressWarnings should be added to the metamodel." + ); + } + + @Override + protected Map getProcessorOptions() { + Map properties = new HashMap(); + properties.put( + JPAMetaModelEntityProcessor.ADD_SUPPRESS_WARNINGS_ANNOTATION, + "true" + ); + return properties; + } + + @Override + protected String getPackageNameOfCurrentTest() { + return SuppressWarningsAnnotationGeneratedTest.class.getPackage().getName(); + } +} \ No newline at end of file diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/supresswarnings/SuppressWarningsAnnotationNotGeneratedTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/supresswarnings/SuppressWarningsAnnotationNotGeneratedTest.java new file mode 100644 index 0000000000..9e6e414b30 --- /dev/null +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/supresswarnings/SuppressWarningsAnnotationNotGeneratedTest.java @@ -0,0 +1,50 @@ +/* + * 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. + */ +package org.hibernate.jpamodelgen.test.supresswarnings; + +import org.testng.annotations.Test; + +import org.hibernate.jpamodelgen.test.util.CompilationTest; +import org.hibernate.jpamodelgen.test.util.TestForIssue; + +import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMetamodelClassGeneratedFor; +import static org.hibernate.jpamodelgen.test.util.TestUtil.getMetaModelSourceAsString; +import static org.testng.Assert.assertFalse; + +/** + * @author Hardy Ferentschik + */ +public class SuppressWarningsAnnotationNotGeneratedTest extends CompilationTest { + + @Test + @TestForIssue(jiraKey = "METAGEN-50") + public void testSuppressedWarningsAnnotationNotGenerated() { + assertMetamodelClassGeneratedFor( TestEntity.class ); + + // need to check the source because @SuppressWarnings is not a runtime annotation + String metaModelSource = getMetaModelSourceAsString( TestEntity.class ); + assertFalse( + metaModelSource.contains( "@SuppressWarnings(\"all\")" ), + "@SuppressWarnings should not be added to the metamodel." + ); + } + + @Override + protected String getPackageNameOfCurrentTest() { + return SuppressWarningsAnnotationNotGeneratedTest.class.getPackage().getName(); + } +} \ No newline at end of file diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/supresswarnings/TestEntity.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/supresswarnings/TestEntity.java new file mode 100644 index 0000000000..9756e4c3b8 --- /dev/null +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/supresswarnings/TestEntity.java @@ -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.supresswarnings; + +import javax.persistence.Entity; +import javax.persistence.Id; + +/** + * @author Hardy Ferentschik + */ +@Entity +public class TestEntity { + @Id + private long id; +} + + 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 91fe8f1a8c..aac3e7719b 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 @@ -88,18 +88,17 @@ public abstract class CompilationTest { List sourceFiles = getCompilationUnits( sourceBaseDir, getPackageNameOfCurrentTest() ); // make sure there are no relics from previous runs TestUtil.deleteGeneratedSourceFiles( new File( outBaseDir ) ); - compile( sourceFiles, getPackageNameOfCurrentTest() ); + compile( sourceFiles ); } /** * 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 { + protected void compile(List sourceFiles) throws Exception { List options = createJavaOptions(); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); @@ -117,8 +116,8 @@ public abstract class CompilationTest { protected List getCompilationUnits(String baseDir, String packageName) { List javaFiles = new ArrayList(); String packageDirName = baseDir; - if(packageName != null) { - packageDirName = packageDirName + PATH_SEPARATOR + packageName.replace( ".", PATH_SEPARATOR ); + if ( packageName != null ) { + packageDirName = packageDirName + PATH_SEPARATOR + packageName.replace( ".", PATH_SEPARATOR ); } File packageDir = new File( packageDirName ); diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/util/TestForIssue.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/util/TestForIssue.java new file mode 100644 index 0000000000..ae27bc10ec --- /dev/null +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/util/TestForIssue.java @@ -0,0 +1,41 @@ +/* +* JBoss, Home of Professional Open Source +* Copyright 2011, Red Hat, Inc. and/or its affiliates, 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. +*/ +package org.hibernate.jpamodelgen.test.util; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * A documentation annotation for notating what JIRA issue is being tested. + * + * @author Steve Ebersole + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.METHOD, ElementType.TYPE }) +public @interface TestForIssue { + /** + * The key of a JIRA issue tested. + * + * @return The jira issue key + */ + String jiraKey(); +} + + +