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
This commit is contained in:
Hardy Ferentschik 2012-01-16 15:21:33 +01:00 committed by Strong Liu
parent 105748e7dd
commit 54411e47b6
10 changed files with 221 additions and 11 deletions

View File

@ -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)";
}

View File

@ -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();
}

View File

@ -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 );

View File

@ -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 );

View File

@ -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<File> sourceFiles = getCompilationUnits(
CompilationTest.getSourceBaseDir(), superClassPackageName
);
compile( sourceFiles, superClassPackageName );
compile( sourceFiles );
sourceFiles = getCompilationUnits( getSourceBaseDir(), getPackageNameOfCurrentTest() );
compile( sourceFiles, getPackageNameOfCurrentTest() );
compile( sourceFiles );
}
@Override

View File

@ -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<String, String> getProcessorOptions() {
Map<String, String> properties = new HashMap<String, String>();
properties.put(
JPAMetaModelEntityProcessor.ADD_SUPPRESS_WARNINGS_ANNOTATION,
"true"
);
return properties;
}
@Override
protected String getPackageNameOfCurrentTest() {
return SuppressWarningsAnnotationGeneratedTest.class.getPackage().getName();
}
}

View File

@ -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();
}
}

View File

@ -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;
}

View File

@ -88,18 +88,17 @@ public abstract class CompilationTest {
List<File> 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<File> sourceFiles, String packageName) throws Exception {
protected void compile(List<File> sourceFiles) throws Exception {
List<String> options = createJavaOptions();
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
@ -117,8 +116,8 @@ public abstract class CompilationTest {
protected List<File> getCompilationUnits(String baseDir, String packageName) {
List<File> javaFiles = new ArrayList<File>();
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 );

View File

@ -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();
}