You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by ch...@apache.org on 2010/10/07 23:09:53 UTC

svn commit: r1005639 - in /harmony/enhanced/java/branches/java6/classlib/modules/annotation/src: main/java/javax/annotation/processing/ test/java/org/apache/harmony/annotation/tests/javax/annotation/processing/

Author: chope
Date: Thu Oct  7 21:09:53 2010
New Revision: 1005639

URL: http://svn.apache.org/viewvc?rev=1005639&view=rev
Log:
Fix to the javax.annotation.processing.AbtractProcessor.getSupportedX methods to return the information from the annotations of the processing class if given.  Also added a null check to the init method and a testcase.

Added:
    harmony/enhanced/java/branches/java6/classlib/modules/annotation/src/test/java/org/apache/harmony/annotation/tests/javax/annotation/processing/
    harmony/enhanced/java/branches/java6/classlib/modules/annotation/src/test/java/org/apache/harmony/annotation/tests/javax/annotation/processing/AbstractProcessorTest.java   (with props)
Modified:
    harmony/enhanced/java/branches/java6/classlib/modules/annotation/src/main/java/javax/annotation/processing/AbstractProcessor.java

Modified: harmony/enhanced/java/branches/java6/classlib/modules/annotation/src/main/java/javax/annotation/processing/AbstractProcessor.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/java6/classlib/modules/annotation/src/main/java/javax/annotation/processing/AbstractProcessor.java?rev=1005639&r1=1005638&r2=1005639&view=diff
==============================================================================
--- harmony/enhanced/java/branches/java6/classlib/modules/annotation/src/main/java/javax/annotation/processing/AbstractProcessor.java (original)
+++ harmony/enhanced/java/branches/java6/classlib/modules/annotation/src/main/java/javax/annotation/processing/AbstractProcessor.java Thu Oct  7 21:09:53 2010
@@ -17,8 +17,10 @@
 
 package javax.annotation.processing;
 
+import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashSet;
-import java.util.Iterator;
+import java.util.List;
 import java.util.Set;
 
 import javax.lang.model.SourceVersion;
@@ -31,8 +33,15 @@ public abstract class AbstractProcessor 
 
     private boolean isInitialized;
 
+    private static final Set<String> emptySet;
+
     protected ProcessingEnvironment processingEnv;
 
+    static {
+        Set<String> tempSet = Collections.emptySet();
+        emptySet = Collections.unmodifiableSet(tempSet);
+    }
+    
     protected AbstractProcessor() {
 
     }
@@ -40,41 +49,47 @@ public abstract class AbstractProcessor 
     public Iterable<? extends Completion> getCompletions(Element element,
             AnnotationMirror annotation, ExecutableElement member,
             String userText) {
-        // return a empty iterable
-        return new Iterable<Completion>() {
-            public Iterator<Completion> iterator() {
-                return new Iterator<Completion>() {
-
-                    public boolean hasNext() {
-                        return false;
-                    }
-
-                    public Completion next() {
-                        return null;
-                    }
-
-                    public void remove() {
-                        // do nothing
-                    }
-                };
-            }
-        };
+        // return an empty iterable
+        List<Completion> emptyList = Collections.emptyList();
+        return emptyList;
     }
 
+    @SuppressWarnings("unchecked")
     public Set<String> getSupportedAnnotationTypes() {
-        return new HashSet<String>();
+        // check if the processing class has the types set
+        SupportedAnnotationTypes types = this.getClass().getAnnotation(SupportedAnnotationTypes.class);
+        if (types != null) {
+            return Collections.unmodifiableSet(new HashSet(Arrays.asList(types.value())));
+        }
+        // otherwise return an empty set
+        return emptySet;
     }
 
+    @SuppressWarnings("unchecked")
     public Set<String> getSupportedOptions() {
-        return new HashSet<String>();
+        // check if the processing class has the options set
+        SupportedOptions options = this.getClass().getAnnotation(SupportedOptions.class);
+        if (options != null) {
+            return Collections.unmodifiableSet(new HashSet(Arrays.asList(options.value())));
+        }
+        // otherwise return an empty set
+        return emptySet;
     }
 
     public SourceVersion getSupportedSourceVersion() {
+        // check if the processing class has the source version set
+        SupportedSourceVersion sourceVersion = this.getClass().getAnnotation(SupportedSourceVersion.class);
+        if (sourceVersion != null) {
+            return sourceVersion.value();
+        }
         return SourceVersion.RELEASE_6;
     }
 
     public void init(ProcessingEnvironment processingEnv) {
-        if (this.processingEnv != null && this.processingEnv == processingEnv) {
+        if (processingEnv == null) {
+            throw new NullPointerException();
+        }
+        if (this.processingEnv != null) {
             throw new IllegalStateException();
         }
         this.processingEnv = processingEnv;

Added: harmony/enhanced/java/branches/java6/classlib/modules/annotation/src/test/java/org/apache/harmony/annotation/tests/javax/annotation/processing/AbstractProcessorTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/java6/classlib/modules/annotation/src/test/java/org/apache/harmony/annotation/tests/javax/annotation/processing/AbstractProcessorTest.java?rev=1005639&view=auto
==============================================================================
--- harmony/enhanced/java/branches/java6/classlib/modules/annotation/src/test/java/org/apache/harmony/annotation/tests/javax/annotation/processing/AbstractProcessorTest.java (added)
+++ harmony/enhanced/java/branches/java6/classlib/modules/annotation/src/test/java/org/apache/harmony/annotation/tests/javax/annotation/processing/AbstractProcessorTest.java Thu Oct  7 21:09:53 2010
@@ -0,0 +1,239 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You 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.apache.harmony.annotation.tests.javax.annotation.processing;
+
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+
+import javax.annotation.processing.AbstractProcessor;
+import javax.annotation.processing.Completion;
+import javax.annotation.processing.Filer;
+import javax.annotation.processing.Messager;
+import javax.annotation.processing.ProcessingEnvironment;
+import javax.annotation.processing.RoundEnvironment;
+import javax.annotation.processing.SupportedAnnotationTypes;
+import javax.annotation.processing.SupportedOptions;
+import javax.annotation.processing.SupportedSourceVersion;
+import javax.lang.model.SourceVersion;
+import javax.lang.model.element.TypeElement;
+import javax.lang.model.util.Elements;
+import javax.lang.model.util.Types;
+
+import junit.framework.TestCase;
+
+public class AbstractProcessorTest extends TestCase {
+
+    AbstractProcessor processor;
+    AbstractProcessor annotatedProcessor;
+
+    public void setUp() {
+        processor = new MockAbstractProcessor();
+        annotatedProcessor = new MockAbstractProcessorAnnotated();
+    }
+
+    /**
+     * @tests javax.annotation.processing.AbstractProcessor.getSupportedOptions()
+     */
+    public void test_getSupportedOptions() {
+        // check default options
+        Set<String> supportedOptions = processor.getSupportedOptions();
+        assertEquals(0, supportedOptions.size());
+        // set should be unmodifiable
+        try {
+            supportedOptions.add("test");
+            fail("Returned supported options set is not unmodifiable");
+        } catch (UnsupportedOperationException e) {
+            // expected
+        }
+
+        // check annotated options
+        supportedOptions = annotatedProcessor.getSupportedOptions();
+        assertEquals(2, supportedOptions.size());
+        assertTrue(supportedOptions.contains("option.one"));
+        assertTrue(supportedOptions.contains("option.two"));
+        // set should be unmodifiable
+        try {
+            supportedOptions.add("test");
+            fail("Returned supported options set is not unmodifiable");
+        } catch (UnsupportedOperationException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests javax.annotation.processing.AbstractProcessor.getSupportedAnnotationTypes()
+     */
+    public void test_getSupportedAnnotationTypes() {
+        // check default types
+        Set<String> supportedTypes = processor.getSupportedAnnotationTypes();
+        assertEquals(0, supportedTypes.size());
+        // set should be unmodifiable
+        try {
+            supportedTypes.add("test");
+            fail("Returned supported annotation types set is not unmodifiable");
+        } catch (UnsupportedOperationException e) {
+            // expected
+        }
+
+        // check annotated types
+        supportedTypes = annotatedProcessor.getSupportedAnnotationTypes();
+        assertEquals(3, supportedTypes.size());
+        assertTrue(supportedTypes.contains("type.one"));
+        assertTrue(supportedTypes.contains("type.two"));
+        assertTrue(supportedTypes.contains("type2.*"));
+        // set should be unmodifiable
+        try {
+            supportedTypes.add("test");
+            fail("Returned supported annotation types set is not unmodifiable");
+        } catch (UnsupportedOperationException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests javax.annotation.processing.AbstractProcessor.getSupportedSourceVersion()
+     */
+    public void test_getSupportedSourceVersion() {
+        // check default source version
+        assertEquals(SourceVersion.RELEASE_6, processor
+                .getSupportedSourceVersion());
+
+        // check annotated version
+        assertEquals(SourceVersion.RELEASE_5, annotatedProcessor
+                .getSupportedSourceVersion());
+    }
+
+    /**
+     * @tests javax.annotation.processing.AbstractProcessor.init(
+     *        javax.annotation.processing.ProcessingEnvironment)
+     */
+    public void test_init() {
+        try {
+            processor.init(null);
+            fail("Calling init(null) should throw NPE");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        ProcessingEnvironment processingEnv = new MockProcessingEnvironment();
+        processor.init(processingEnv);
+
+        assertEquals(processingEnv, ((MockAbstractProcessor) processor)
+                .getEnvironment());
+
+        try {
+            processor.init(processingEnv);
+            fail("Calling init twice should throw IllegalStateException");
+        } catch (IllegalStateException e) {
+            // expected
+        }
+
+        try {
+            processor.init(new MockProcessingEnvironment());
+            fail("Calling init twice should throw IllegalStateException");
+        } catch (IllegalStateException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests javax.annotation.processing.AbstractProcessor.isInitialized()
+     */
+    public void test_isInitialized() {
+        assertFalse(((MockAbstractProcessor) processor).isInitialized());
+        processor.init(new MockProcessingEnvironment());
+        assertTrue(((MockAbstractProcessor) processor).isInitialized());
+    }
+
+    /**
+     * @tests javax.annotation.processing.AbstractProcessor.getCompletions()
+     */
+    public void test_getCompletions() {
+        // check that the default implementation returns an empty iterator
+        Iterable<? extends Completion> completions = processor.getCompletions(
+                null, null, null, null);
+        assertFalse(completions.iterator().hasNext());
+    }
+
+    class MockAbstractProcessor extends AbstractProcessor {
+
+        @Override
+        public boolean process(Set<? extends TypeElement> annotations,
+                RoundEnvironment roundEnv) {
+            return false;
+        }
+
+        public ProcessingEnvironment getEnvironment() {
+            return processingEnv;
+        }
+
+        public boolean isInitialized() {
+            return super.isInitialized();
+        }
+    }
+
+    @SupportedSourceVersion(SourceVersion.RELEASE_5)
+    @SupportedAnnotationTypes( { "type.one", "type.two", "type2.*" })
+    @SupportedOptions( { "option.one", "option.two", "option.one" })
+    class MockAbstractProcessorAnnotated extends AbstractProcessor {
+
+        @Override
+        public boolean process(Set<? extends TypeElement> annotations,
+                RoundEnvironment roundEnv) {
+            return false;
+        }
+
+        public ProcessingEnvironment getEnvironment() {
+            return processingEnv;
+        }
+
+        public boolean isInitialized() {
+            return super.isInitialized();
+        }
+    }
+
+    class MockProcessingEnvironment implements ProcessingEnvironment {
+
+        public Elements getElementUtils() {
+            return null;
+        }
+
+        public Filer getFiler() {
+            return null;
+        }
+
+        public Locale getLocale() {
+            return null;
+        }
+
+        public Messager getMessager() {
+            return null;
+        }
+
+        public Map<String, String> getOptions() {
+            return null;
+        }
+
+        public SourceVersion getSourceVersion() {
+            return null;
+        }
+
+        public Types getTypeUtils() {
+            return null;
+        }
+    }
+}

Propchange: harmony/enhanced/java/branches/java6/classlib/modules/annotation/src/test/java/org/apache/harmony/annotation/tests/javax/annotation/processing/AbstractProcessorTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain