started plugging codemodel inside the annotations processor method (it doesn't produce any result yet...)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/digester/trunk@1309905 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/annotations-processor/src/main/java/org/apache/commons/digester3/annotations/processor/DigesterAnnotationsProcessor.java b/annotations-processor/src/main/java/org/apache/commons/digester3/annotations/processor/DigesterAnnotationsProcessor.java
index 375c68f..e50c0ad 100644
--- a/annotations-processor/src/main/java/org/apache/commons/digester3/annotations/processor/DigesterAnnotationsProcessor.java
+++ b/annotations-processor/src/main/java/org/apache/commons/digester3/annotations/processor/DigesterAnnotationsProcessor.java
@@ -19,12 +19,18 @@
* under the License.
*/
+import static com.sun.codemodel.JMod.FINAL;
+import static com.sun.codemodel.JMod.PROTECTED;
+import static com.sun.codemodel.JMod.PUBLIC;
+
import static java.lang.String.format;
import static javax.tools.Diagnostic.Kind.*;
import static java.util.Arrays.asList;
+import java.io.IOException;
+import java.util.Date;
import java.util.HashSet;
import java.util.Set;
@@ -46,6 +52,12 @@
import org.apache.commons.digester3.annotations.rules.SetRoot;
import org.apache.commons.digester3.annotations.rules.SetTop;
+import com.sun.codemodel.JClassAlreadyExistsException;
+import com.sun.codemodel.JCodeModel;
+import com.sun.codemodel.JDefinedClass;
+import com.sun.codemodel.JMethod;
+import com.sun.codemodel.JPackage;
+
/**
* @since 3.3
*/
@@ -82,17 +94,49 @@
// Messager allows the processor to output messages to the environment
Messager messager = processingEnv.getMessager();
- // Loop through the annotations that we are going to process
- for (TypeElement annotation: annotations) {
- // Get the members
- for ( Element element : environment.getElementsAnnotatedWith( annotation ) )
+ String packageName = "com.acme";
+ String className = "MyRules";
+
+ JCodeModel codeModel = new JCodeModel();
+
+ JPackage modulePackage = codeModel._package( packageName );
+
+ boolean success = false;
+
+ try
+ {
+ JDefinedClass definedClass = modulePackage._class( FINAL | PUBLIC, className );
+ definedClass.javadoc().add( format( "Generated by Apache Commons Digester at %s", new Date() ) );
+
+ JMethod method = definedClass.method( PROTECTED, Void.class, "configure" );
+ method.annotate( Override.class );
+
+ // Loop through the annotations that we are going to process
+ for ( TypeElement annotation : annotations )
{
- System.out.println( format( "Processing @%s %s", annotation, element ) );
- messager.printMessage( OTHER, format( "Processing @%s %s", annotation, element ) );
+ // Get the members
+ for ( Element element : environment.getElementsAnnotatedWith( annotation ) )
+ {
+ System.out.println( format( "Processing @%s %s", annotation, element ) );
+ messager.printMessage( OTHER, format( "Processing @%s %s", annotation, element ) );
+ }
}
+
+ codeModel.build( new FilerCodeWriter( processingEnv.getFiler() ) );
+
+ success = true;
+ }
+ catch ( JClassAlreadyExistsException e )
+ {
+ messager.printMessage( ERROR, format( "Class %s.%s has been already defined", packageName, className ) );
+ }
+ catch ( IOException e )
+ {
+ messager.printMessage( ERROR, format( "Impossible to generate class %s.%s: %s",
+ packageName, className, e.getMessage() ) );
}
- return true;
+ return success;
}
}