You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by si...@apache.org on 2012/04/05 17:45:02 UTC

svn commit: r1309905 - /commons/proper/digester/trunk/annotations-processor/src/main/java/org/apache/commons/digester3/annotations/processor/DigesterAnnotationsProcessor.java

Author: simonetripodi
Date: Thu Apr  5 15:45:01 2012
New Revision: 1309905

URL: http://svn.apache.org/viewvc?rev=1309905&view=rev
Log:
started plugging codemodel inside the annotations processor method (it doesn't produce any result yet...)

Modified:
    commons/proper/digester/trunk/annotations-processor/src/main/java/org/apache/commons/digester3/annotations/processor/DigesterAnnotationsProcessor.java

Modified: commons/proper/digester/trunk/annotations-processor/src/main/java/org/apache/commons/digester3/annotations/processor/DigesterAnnotationsProcessor.java
URL: http://svn.apache.org/viewvc/commons/proper/digester/trunk/annotations-processor/src/main/java/org/apache/commons/digester3/annotations/processor/DigesterAnnotationsProcessor.java?rev=1309905&r1=1309904&r2=1309905&view=diff
==============================================================================
--- commons/proper/digester/trunk/annotations-processor/src/main/java/org/apache/commons/digester3/annotations/processor/DigesterAnnotationsProcessor.java (original)
+++ commons/proper/digester/trunk/annotations-processor/src/main/java/org/apache/commons/digester3/annotations/processor/DigesterAnnotationsProcessor.java Thu Apr  5 15:45:01 2012
@@ -19,12 +19,18 @@ package org.apache.commons.digester3.ann
  * 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.anno
 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 @@ public class DigesterAnnotationsProcesso
         // 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;
     }
 
 }