OPENJPA-1662: Add openjpa. prefix to Annotation Processor -A options

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@932544 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Pinaki Poddar 2010-04-09 18:55:31 +00:00
parent aa0ba90782
commit 6ccd59c32b
3 changed files with 39 additions and 17 deletions

View File

@ -30,6 +30,7 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.annotation.Generated; import javax.annotation.Generated;
@ -45,6 +46,7 @@ import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements; import javax.lang.model.util.Elements;
import javax.persistence.metamodel.StaticMetamodel; import javax.persistence.metamodel.StaticMetamodel;
import javax.tools.Diagnostic;
import javax.tools.JavaCompiler; import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject; import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager; import javax.tools.StandardJavaFileManager;
@ -56,6 +58,8 @@ import org.apache.openjpa.meta.MetaDataFactory;
import org.apache.openjpa.persistence.PersistenceMetaDataFactory; import org.apache.openjpa.persistence.PersistenceMetaDataFactory;
import org.apache.openjpa.persistence.util.SourceCode; import org.apache.openjpa.persistence.util.SourceCode;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
/** /**
* Annotation processing tool generates source code for a meta-model class given * Annotation processing tool generates source code for a meta-model class given
* the annotated source code of persistent entity. * the annotated source code of persistent entity.
@ -70,7 +74,9 @@ import org.apache.openjpa.persistence.util.SourceCode;
* <code>$ javac -classpath path/to/openjpa-all.jar -Aopenjpa.generated=true mypackage/MyEntity.java</code><br> * <code>$ javac -classpath path/to/openjpa-all.jar -Aopenjpa.generated=true mypackage/MyEntity.java</code><br>
* will generate source code for canonical meta-model class <code>mypackage.MyEntity_.java</code>. * will generate source code for canonical meta-model class <code>mypackage.MyEntity_.java</code>.
* <p> * <p>
* The Annotation Processor also recognizes the following options (none of them are mandatory):<br> * The Annotation Processor also recognizes the following options (none of them are mandatory).
* Each of the following option key can also be prefixed with <code>openjpa.</code> to distinguish if multiple
* annotation processors are active during compilation:<br>
* <TABLE border="1"> * <TABLE border="1">
* <TR><TD>-Alog={log level}<TD>The logging level. Default is <code>WARN</code>. Permissible values are * <TR><TD>-Alog={log level}<TD>The logging level. Default is <code>WARN</code>. Permissible values are
* <code>TRACE</code>, <code>INFO</code>, <code>WARN</code> or <code> ERROR</code>. * <code>TRACE</code>, <code>INFO</code>, <code>WARN</code> or <code> ERROR</code>.
@ -96,6 +102,7 @@ import org.apache.openjpa.persistence.util.SourceCode;
* to the package structure. * to the package structure.
* </TABLE> * </TABLE>
* <br> * <br>
*
* @author Pinaki Poddar * @author Pinaki Poddar
* *
* @since 2.0.0 * @since 2.0.0
@ -105,7 +112,13 @@ import org.apache.openjpa.persistence.util.SourceCode;
"javax.persistence.Entity", "javax.persistence.Entity",
"javax.persistence.Embeddable", "javax.persistence.Embeddable",
"javax.persistence.MappedSuperclass" }) "javax.persistence.MappedSuperclass" })
@SupportedOptions( { "log", "out", "source", "naming", "header", "openjpa.generate" }) @SupportedOptions({ "openjpa.log", "log",
"openjpa.out", "out",
"openjpa.source", "source",
"openjpa.naming", "naming",
"openjpa.header", "header",
"openjpa.generate"
})
@SupportedSourceVersion(RELEASE_6) @SupportedSourceVersion(RELEASE_6)
public class AnnotationProcessor6 extends AbstractProcessor { public class AnnotationProcessor6 extends AbstractProcessor {
@ -194,11 +207,11 @@ public class AnnotationProcessor6 extends AbstractProcessor {
@Override @Override
public synchronized void init(ProcessingEnvironment processingEnv) { public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv); super.init(processingEnv);
active = "true".equalsIgnoreCase(processingEnv.getOptions().get("openjpa.generate")); active = "true".equalsIgnoreCase(getOptionValue("openjpa.generate"));
if (!active) if (!active)
return; return;
logger = new CompileTimeLogger(processingEnv); processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, _loc.get("mmg-tool-banner").toString());
logger.info(_loc.get("mmg-tool-banner")); logger = new CompileTimeLogger(processingEnv, getOptionValue("openjpa.log", "log"));
setSourceVersion(); setSourceVersion();
setFileManager(); setFileManager();
setNamingPolicy(); setNamingPolicy();
@ -211,9 +224,7 @@ public class AnnotationProcessor6 extends AbstractProcessor {
*/ */
@Override @Override
public boolean process(Set<? extends TypeElement> annos, RoundEnvironment roundEnv) { public boolean process(Set<? extends TypeElement> annos, RoundEnvironment roundEnv) {
if (!active) if (active && !roundEnv.processingOver()) {
return false;
if (!roundEnv.processingOver()) {
Set<? extends Element> elements = roundEnv.getRootElements(); Set<? extends Element> elements = roundEnv.getRootElements();
for (Element e : elements) { for (Element e : elements) {
process((TypeElement) e); process((TypeElement) e);
@ -322,7 +333,7 @@ public class AnnotationProcessor6 extends AbstractProcessor {
* n must be a integer. Default or wrong specification returns 6. * n must be a integer. Default or wrong specification returns 6.
*/ */
private void setSourceVersion() { private void setSourceVersion() {
String version = processingEnv.getOptions().get("source"); String version = getOptionValue("openjpa.source", "source");
if (version != null) { if (version != null) {
try { try {
generatedSourceVersion = Integer.parseInt(version); generatedSourceVersion = Integer.parseInt(version);
@ -336,7 +347,7 @@ public class AnnotationProcessor6 extends AbstractProcessor {
} }
private void setNamingPolicy() { private void setNamingPolicy() {
String policy = processingEnv.getOptions().get("naming"); String policy = getOptionValue("openjpa.naming","naming");
if (policy != null) { if (policy != null) {
try { try {
factory = (MetaDataFactory)Class.forName(policy).newInstance(); factory = (MetaDataFactory)Class.forName(policy).newInstance();
@ -350,7 +361,7 @@ public class AnnotationProcessor6 extends AbstractProcessor {
} }
private void setHeader() { private void setHeader() {
String headerOption = processingEnv.getOptions().get("header"); String headerOption = getOptionValue("openjpa.header", "header");
if (headerOption == null) { if (headerOption == null) {
return; return;
} }
@ -369,7 +380,7 @@ public class AnnotationProcessor6 extends AbstractProcessor {
private void setFileManager() { private void setFileManager() {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
fileManager = compiler.getStandardFileManager(null, null, null); fileManager = compiler.getStandardFileManager(null, null, null);
String outDir = processingEnv.getOptions().get("out"); String outDir = getOptionValue("openjpa.out", "out");
if (outDir != null) if (outDir != null)
isUserSpecifiedOutputLocation = setSourceOutputDirectory(new File(outDir)); isUserSpecifiedOutputLocation = setSourceOutputDirectory(new File(outDir));
} }
@ -416,6 +427,18 @@ public class AnnotationProcessor6 extends AbstractProcessor {
} }
} }
/**
* Get the value for the given keys, whoever matches first, in the current available options.
*/
private String getOptionValue(String... keys) {
Map<String,String> options = processingEnv.getOptions();
for (String key : keys) {
if (options.containsKey(key))
return options.get(key);
}
return null;
}
/** /**
* An utility class to determine the source file corresponding to a {@link TypeElement}. * An utility class to determine the source file corresponding to a {@link TypeElement}.
* The utility uses Sun JDK internal API (com.sun.tools.*) and hence works reflectively * The utility uses Sun JDK internal API (com.sun.tools.*) and hence works reflectively

View File

@ -34,16 +34,14 @@ import org.apache.openjpa.lib.util.Localizer;
*/ */
public class CompileTimeLogger { public class CompileTimeLogger {
private static enum Level {TRACE, INFO, WARN, ERROR}; private static enum Level {TRACE, INFO, WARN, ERROR};
private static Localizer _loc = Localizer.forPackage( private static Localizer _loc = Localizer.forPackage(CompileTimeLogger.class);
CompileTimeLogger.class);
private static Level DEFAULT_LEVEL = Level.WARN; private static Level DEFAULT_LEVEL = Level.WARN;
private int logLevel; private int logLevel;
private Messager messager; private Messager messager;
public CompileTimeLogger(ProcessingEnvironment env) { public CompileTimeLogger(ProcessingEnvironment env, String level) {
messager = env.getMessager(); messager = env.getMessager();
String level = env.getOptions().get("log");
if (level == null) { if (level == null) {
logLevel = DEFAULT_LEVEL.ordinal(); logLevel = DEFAULT_LEVEL.ordinal();
return; return;

View File

@ -177,7 +177,8 @@ will generate source code for canonical meta-model class <code>mypackage.MyEntit
<para> <para>
The Annotation Processor recognizes the following options specified in the command-line with <code>-A</code> The Annotation Processor recognizes the following options specified in the command-line with <code>-A</code>
(none of them are mandatory): (none of them are mandatory). Each of the following option key can also be prefixed with <code>openjpa.</code>
to distinguish if multiple annotation processors are active during compilation.
<itemizedlist> <itemizedlist>
<listitem> <listitem>
<para> <para>