beehive-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mm...@apache.org
Subject svn commit: rev 43491 - in incubator/beehive/trunk/wsm: drt drt/tests/org/apache/beehive/wsm/jsr181/processor/apt src/runtime/org/apache/beehive/wsm/jsr181/processor/apt
Date Wed, 08 Sep 2004 03:17:28 GMT
Author: mmerz
Date: Tue Sep  7 20:17:27 2004
New Revision: 43491

Added:
   incubator/beehive/trunk/wsm/drt/tests/org/apache/beehive/wsm/jsr181/processor/apt/WsmAnnotationProcessorTest.java
Modified:
   incubator/beehive/trunk/wsm/drt/build.xml
   incubator/beehive/trunk/wsm/src/runtime/org/apache/beehive/wsm/jsr181/processor/apt/WsmAnnotationProcessor.java
   incubator/beehive/trunk/wsm/src/runtime/org/apache/beehive/wsm/jsr181/processor/apt/WsmAnnotationProcessorFactory.java
Log:
Bug fixes and rudimentary test case for source file annotation processing into object model.



Modified: incubator/beehive/trunk/wsm/drt/build.xml
==============================================================================
--- incubator/beehive/trunk/wsm/drt/build.xml	(original)
+++ incubator/beehive/trunk/wsm/drt/build.xml	Tue Sep  7 20:17:27 2004
@@ -77,8 +77,9 @@
 
     <target name="run-drt">
         <echo message="** junit logfiles written to ${drt.logs} **" />
-        <junit haltonerror="off" printsummary="on" tempdir="${build.dir}" >
+        <junit haltonerror="off" printsummary="on" tempdir="${build.dir}" fork="yes">
             <classpath>
+                <pathelement location="${tools.jar}" />
                 <pathelement location="${build.tests}" />
                 <pathelement path="${xbean.jar}" />
                 <pathelement path="${jsr173.jar}" />
@@ -86,7 +87,7 @@
                 <pathelement path="../build/jars/wsm.jar"/>
             </classpath>
             <formatter type="plain" />
-            <batchtest fork="off" filtertrace="off" todir="${drt.logs}" >
+            <batchtest filtertrace="off" todir="${drt.logs}" >
                 <fileset dir="tests">
                     <include name="**/*Test.java" />
                     <!-- exclude name="**/util/**.class" / -->

Added: incubator/beehive/trunk/wsm/drt/tests/org/apache/beehive/wsm/jsr181/processor/apt/WsmAnnotationProcessorTest.java
==============================================================================
--- (empty file)
+++ incubator/beehive/trunk/wsm/drt/tests/org/apache/beehive/wsm/jsr181/processor/apt/WsmAnnotationProcessorTest.java
Tue Sep  7 20:17:27 2004
@@ -0,0 +1,78 @@
+package org.apache.beehive.wsm.jsr181.processor.apt;
+
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * 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.
+ *
+ * $Header:$
+ */
+
+import com.sun.tools.apt.Main;
+
+import junit.framework.TestCase;
+
+import org.apache.beehive.wsm.jsr181.model.AnnotationModel;
+
+import org.apache.beehive.wsm.jsr181.processor.apt.WsmAnnotationProcessor;
+
+
+/**
+ * Test case verifies that a source file can be read and interpreted by the
+ * annotation processor.
+ * Data validation is not implemented here, but will be part of other test
+ * cases.
+ */
+public class WsmAnnotationProcessorTest extends TestCase {
+
+    private static final String CLASSNAME = "Foo";
+    private static final String SRCFILENAME = "Foo.java";
+    private static final String BEEHIVE_HOME = System.getenv( "BEEHIVE_HOME" );
+    private static final String TESTDIR = BEEHIVE_HOME + "/wsm/drt/tests/";
+    private static final String CLASSTESTDIR = BEEHIVE_HOME + "/wsm/drt/build/classes/tests/";
+    private static final String WSMJAR = BEEHIVE_HOME + "/wsm/build/jars/wsm.jar";
+    private static final String TOOLSJAR = "D:\\tmp\\tools.jar";
+    
+    private Main apt;
+
+    public void setUp()
+    {
+        apt = new Main();
+    }
+    
+    public void tearDown()
+    {
+        // todo
+    }
+    
+    public void testAnnotationProcessor() throws Exception
+    {
+        String[] _args = {
+            "-classpath", TOOLSJAR + ";" + WSMJAR,
+            "-factory", "org.apache.beehive.wsm.jsr181.processor.apt.WsmAnnotationProcessorFactory",
+            new java.io.File(TESTDIR, SRCFILENAME).getCanonicalPath()
+        };
+
+        // compile source file
+        int status = apt.compile(_args);
+        if (0 != status)
+        {
+            throw new Exception("** Error compiling: " + SRCFILENAME);
+        }
+
+        // get object model
+        AnnotationModel objectModel =
+            WsmAnnotationProcessor.getObjectModel(CLASSNAME);
+        assertNotNull(objectModel);
+    }
+}

Modified: incubator/beehive/trunk/wsm/src/runtime/org/apache/beehive/wsm/jsr181/processor/apt/WsmAnnotationProcessor.java
==============================================================================
--- incubator/beehive/trunk/wsm/src/runtime/org/apache/beehive/wsm/jsr181/processor/apt/WsmAnnotationProcessor.java
(original)
+++ incubator/beehive/trunk/wsm/src/runtime/org/apache/beehive/wsm/jsr181/processor/apt/WsmAnnotationProcessor.java
Tue Sep  7 20:17:27 2004
@@ -18,6 +18,8 @@
  * $Header:$
  */
 
+import java.lang.annotation.Annotation;
+
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
@@ -27,14 +29,20 @@
 
 import com.sun.mirror.apt.AnnotationProcessorEnvironment;
 
+import com.sun.mirror.declaration.AnnotationMirror;
 import com.sun.mirror.declaration.AnnotationTypeDeclaration;
 import com.sun.mirror.declaration.ClassDeclaration;
 import com.sun.mirror.declaration.Declaration;
 import com.sun.mirror.declaration.MethodDeclaration;
 import com.sun.mirror.declaration.ParameterDeclaration;
 
+import com.sun.mirror.type.AnnotationType;
+
 import org.apache.beehive.wsm.jsr181.model.*;
 
+
+import java.io.*;
+
 public class WsmAnnotationProcessor extends TwoPhaseAnnotationProcessor
 {
     /**
@@ -63,11 +71,12 @@
             return;
             // todo: log warning/error "@WebService" not defined (should never happen)
         }
-
+        
         // create & store object model
+        AnnotationModel objectModel =
+            getWebServiceTYPEMetadata((ClassDeclaration) decl);
         objectModels.put(
-            ((ClassDeclaration) decl).getQualifiedName(),
-            getWebServiceTYPEMetadata((ClassDeclaration) decl));
+            ((ClassDeclaration) decl).getQualifiedName(), objectModel);
     }
     
     @Override
@@ -78,19 +87,17 @@
         // todo: do something useful with the object model
     }
 
-    public static AnnotationModel getObjectModel(String fqClassName)
+    public static AnnotationModel getObjectModel(String name)
     {
-        return objectModels.get(fqClassName);
+        return objectModels.get(name);
     }
 
-
     public static Set<String> getObjectModelNames()
     {
         return objectModels.keySet();
     }
 
-    
-    private WebServiceTYPEMetadata getWebServiceTYPEMetadata(
+    protected WebServiceTYPEMetadata getWebServiceTYPEMetadata(
         ClassDeclaration decl)
     {
         // get webService's webMethods
@@ -105,14 +112,22 @@
         }
 
         // create & return webService
-        return new WebServiceTYPEMetadata(
-            java.util.Arrays.asList(decl.getClass().getAnnotations()),
-            ((ClassDeclaration)decl).getClass().getName(),
-            webMethods
-        );
+        WebServiceTYPEMetadata wstm = null;
+        try {
+            wstm = new WebServiceTYPEMetadata(
+                getAnnotations(decl),
+                decl.getQualifiedName(),
+                webMethods
+            );
+        }
+        catch (Throwable t)
+        {
+            t.printStackTrace(); // todo: proper error handling
+        }
+        return wstm;
     }
     
-    private WebServiceMETHODMetadata getWebServiceMETHODMetadata(
+    protected WebServiceMETHODMetadata getWebServiceMETHODMetadata(
         MethodDeclaration decl)
     {
         // get webMethod's webParams
@@ -127,23 +142,102 @@
         }
 
         // create & return webMethod
-        return new WebServiceMETHODMetadata(
-            java.util.Arrays.asList(decl.getClass().getAnnotations()),
-            decl.getSimpleName(),
-            ((MethodDeclaration)decl).getReturnType().getClass(),
-            webParams
-        );
+        WebServiceMETHODMetadata wsmm = null;
+        try {
+            wsmm = new WebServiceMETHODMetadata(
+                getAnnotations(decl),
+                decl.getSimpleName(),
+                classForName(decl.getReturnType().toString()),
+                webParams
+            );
+        }
+        catch (Throwable t)
+        {
+            t.printStackTrace(); // todo: proper error handling
+        }
+        return wsmm;
     }
     
-    private WebServicePARAMETERMetadata getWebServicePARAMETERMetadata(
+    protected WebServicePARAMETERMetadata getWebServicePARAMETERMetadata(
         ParameterDeclaration decl)
     {
         // create & return webParam
-        return new WebServicePARAMETERMetadata(
-            java.util.Arrays.asList(decl.getClass().getAnnotations()),
-            decl.getClass(),
-            decl.getSimpleName()
-        );
+        WebServicePARAMETERMetadata wspm = null;
+        try {
+            wspm = new WebServicePARAMETERMetadata(
+                getAnnotations(decl),
+                classForName(decl.getType().toString()),
+                decl.getSimpleName()
+            );
+        }
+        catch (Throwable t)
+        {
+            t.printStackTrace(); // todo: proper error handling
+        }
+        return wspm;
+    }
+
+    private Collection<Annotation> getAnnotations(Declaration decl)
+    {
+        Collection<Annotation> annotations = new ArrayList<Annotation>();
+        for ( AnnotationMirror am : decl.getAnnotationMirrors() )
+        {
+            AnnotationType at = am.getAnnotationType(); 
+            try {
+                Class clazz = Class.forName(at.getDeclaration().getQualifiedName());
+                Annotation a = decl.getAnnotation(clazz);
+                if (null != a)
+                {
+                    annotations.add(a);
+                }
+            }
+            catch (ClassNotFoundException e) { }
+        }
+        return annotations;
+    }
+    
+    private Class classForName(String name) throws ClassNotFoundException
+    {
+        if ((null == name) || (0 == name.length()))
+        {
+            throw new IllegalArgumentException("class name must not be null");
+        }
+        else if (name.equals("boolean"))
+        {
+            return boolean.class;
+        }
+        else if (name.equals("byte"))
+        {
+            return byte.class;
+        }
+        else if (name.equals("char"))
+        {
+            return char.class;
+        }
+        else if (name.equals("double"))
+        {
+            return double.class;
+        }
+        else if (name.equals("float"))
+        {
+            return float.class;
+        }
+        else if (name.equals("int"))
+        {
+            return int.class;
+        }
+        else if (name.equals("long"))
+        {
+            return long.class;
+        }
+        else if (name.equals("short"))
+        {
+            return short.class;
+        }
+        else
+        {
+            return Class.forName(name);
+        }
     }
     
     private static Map<String, AnnotationModel> objectModels =

Modified: incubator/beehive/trunk/wsm/src/runtime/org/apache/beehive/wsm/jsr181/processor/apt/WsmAnnotationProcessorFactory.java
==============================================================================
--- incubator/beehive/trunk/wsm/src/runtime/org/apache/beehive/wsm/jsr181/processor/apt/WsmAnnotationProcessorFactory.java
(original)
+++ incubator/beehive/trunk/wsm/src/runtime/org/apache/beehive/wsm/jsr181/processor/apt/WsmAnnotationProcessorFactory.java
Tue Sep  7 20:17:27 2004
@@ -19,7 +19,6 @@
  */
 
 import static java.util.Arrays.*;
-import java.util.ArrayList;
 import java.util.Collection;
 import static java.util.Collections.*;
 import java.util.Set;
@@ -27,13 +26,14 @@
 import com.sun.mirror.apt.AnnotationProcessor;
 import com.sun.mirror.apt.AnnotationProcessorFactory;
 import com.sun.mirror.apt.AnnotationProcessorEnvironment;
+
 import com.sun.mirror.declaration.AnnotationTypeDeclaration;
 
 public class WsmAnnotationProcessorFactory implements AnnotationProcessorFactory
 {
     private static final Collection<String> supportedAnnotations =
-            unmodifiableCollection(
-                asList(new String[] { "javax.jws.WebService" }));
+        unmodifiableCollection(
+            asList(new String[] { "javax.jws.WebService" }));
 
     private static final Collection<String> supportedOptions = emptySet();
 
@@ -47,8 +47,8 @@
         return supportedOptions;
     }
 
-    public AnnotationProcessor getProcessorFor(Set<AnnotationTypeDeclaration> atds,
-                                               AnnotationProcessorEnvironment env)
+    public AnnotationProcessor getProcessorFor(
+        Set<AnnotationTypeDeclaration> atds, AnnotationProcessorEnvironment env)
     {
         return new WsmAnnotationProcessor(atds, env);   
     }

Mime
View raw message