test/tools/javac/api/TestOperators.java

Print this page


   1 /*
   2  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug     6338064 6346249 6340951 6392177
  27  * @summary Tree API: can't determine kind of operator
  28  * @author  Peter von der Ah\u00e9
  29  * @compile TestOperators.java

  30  * @compile -processor TestOperators -proc:only TestOperators.java
  31  */
  32 
  33 import java.util.Set;
  34 import javax.annotation.processing.*;
  35 import javax.lang.model.element.*;
  36 import javax.lang.model.util.*;
  37 import static javax.tools.Diagnostic.Kind.*;
  38 
  39 import com.sun.source.tree.*;
  40 import com.sun.source.util.Trees;
  41 
  42 import static com.sun.source.tree.Tree.Kind.*;
  43 
  44 @interface TestMe {
  45     Tree.Kind value();
  46 }
  47 
  48 @SupportedAnnotationTypes("TestMe")
  49 public class TestOperators extends AbstractProcessor {
  50 
  51     @TestMe(POSTFIX_INCREMENT)
  52     public int test_POSTFIX_INCREMENT(int i) {
  53         return i++;
  54     }
  55 
  56     @TestMe(POSTFIX_DECREMENT)
  57     public int test_POSTFIX_DECREMENT(int i) {
  58         return i--;
  59     }
  60 
  61     @TestMe(PREFIX_INCREMENT)
  62     public int test_PREFIX_INCREMENT(int i) {
  63         return ++i;
  64     }
  65 
  66     @TestMe(PREFIX_DECREMENT)
  67     public int test_PREFIX_DECREMENT(int i) {
  68         return --i;
  69     }


 282     public Set<?> test_UNBOUNDED_WILDCARD() {
 283         return null;
 284     }
 285 
 286     @TestMe(EXTENDS_WILDCARD)
 287     public Set<? extends Number> test_EXTENDS_WILDCARD() {
 288         return null;
 289     }
 290 
 291     @TestMe(SUPER_WILDCARD)
 292     public Set<? super Number> test_SUPER_WILDCARD() {
 293         return null;
 294     }
 295 
 296     public boolean process(Set<? extends TypeElement> annotations,
 297                            RoundEnvironment roundEnvironment)
 298     {
 299         final Trees trees = Trees.instance(processingEnv);
 300         final Messager log = processingEnv.getMessager();
 301         final Elements elements = processingEnv.getElementUtils();
 302         class Scan extends ElementScanner7<Void,Void> {
 303             @Override
 304             public Void visitExecutable(ExecutableElement e, Void p) {
 305                 Object debug = e; // info for exception handler
 306                 try {
 307                     TestMe info = e.getAnnotation(TestMe.class);
 308                     if (info == null)
 309                         return null;
 310 
 311                     Tree.Kind kind = info.value();
 312                     MethodTree node = trees.getTree(e);
 313                     debug = node;
 314                     Tree testNode;
 315                     switch (kind) {
 316                     case UNBOUNDED_WILDCARD:
 317                     case EXTENDS_WILDCARD:
 318                     case SUPER_WILDCARD:
 319                         ParameterizedTypeTree typeTree;
 320                         typeTree = (ParameterizedTypeTree) node.getReturnType();
 321                         testNode = typeTree.getTypeArguments().get(0);
 322                         break;


 326                         testNode = returnNode.getExpression();
 327                     }
 328                     if (testNode.getKind() != kind) {
 329                         log.printMessage(ERROR, testNode.getKind() + " != " + kind, e);
 330                         throw new AssertionError(testNode);
 331                     }
 332                     System.err.format("OK: %32s %s%n", kind, testNode);
 333                 } catch (Error ex) {
 334                     System.err.println("Error while looking at " + debug);
 335                     throw ex;
 336                 }
 337                 return null;
 338             }
 339         }
 340         Scan scan = new Scan();
 341         for (Element e : roundEnvironment.getRootElements()) {
 342             scan.scan(e);
 343         }
 344         return true;
 345     }
 346 
 347 }
   1 /*
   2  * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug     6338064 6346249 6340951 6392177
  27  * @summary Tree API: can't determine kind of operator
  28  * @author  Peter von der Ah\u00e9
  29  * @library ../lib
  30  * @build JavacTestingAbstractProcessor TestOperators
  31  * @compile -processor TestOperators -proc:only TestOperators.java
  32  */
  33 
  34 import java.util.Set;
  35 import javax.annotation.processing.*;
  36 import javax.lang.model.element.*;
  37 import javax.lang.model.util.*;
  38 import static javax.tools.Diagnostic.Kind.*;
  39 
  40 import com.sun.source.tree.*;
  41 import com.sun.source.util.Trees;
  42 
  43 import static com.sun.source.tree.Tree.Kind.*;
  44 
  45 @interface TestMe {
  46     Tree.Kind value();
  47 }
  48 
  49 @SupportedAnnotationTypes("TestMe")
  50 public class TestOperators extends JavacTestingAbstractProcessor {
  51 
  52     @TestMe(POSTFIX_INCREMENT)
  53     public int test_POSTFIX_INCREMENT(int i) {
  54         return i++;
  55     }
  56 
  57     @TestMe(POSTFIX_DECREMENT)
  58     public int test_POSTFIX_DECREMENT(int i) {
  59         return i--;
  60     }
  61 
  62     @TestMe(PREFIX_INCREMENT)
  63     public int test_PREFIX_INCREMENT(int i) {
  64         return ++i;
  65     }
  66 
  67     @TestMe(PREFIX_DECREMENT)
  68     public int test_PREFIX_DECREMENT(int i) {
  69         return --i;
  70     }


 283     public Set<?> test_UNBOUNDED_WILDCARD() {
 284         return null;
 285     }
 286 
 287     @TestMe(EXTENDS_WILDCARD)
 288     public Set<? extends Number> test_EXTENDS_WILDCARD() {
 289         return null;
 290     }
 291 
 292     @TestMe(SUPER_WILDCARD)
 293     public Set<? super Number> test_SUPER_WILDCARD() {
 294         return null;
 295     }
 296 
 297     public boolean process(Set<? extends TypeElement> annotations,
 298                            RoundEnvironment roundEnvironment)
 299     {
 300         final Trees trees = Trees.instance(processingEnv);
 301         final Messager log = processingEnv.getMessager();
 302         final Elements elements = processingEnv.getElementUtils();
 303         class Scan extends LatestElementScanner<Void,Void> {
 304             @Override
 305             public Void visitExecutable(ExecutableElement e, Void p) {
 306                 Object debug = e; // info for exception handler
 307                 try {
 308                     TestMe info = e.getAnnotation(TestMe.class);
 309                     if (info == null)
 310                         return null;
 311 
 312                     Tree.Kind kind = info.value();
 313                     MethodTree node = trees.getTree(e);
 314                     debug = node;
 315                     Tree testNode;
 316                     switch (kind) {
 317                     case UNBOUNDED_WILDCARD:
 318                     case EXTENDS_WILDCARD:
 319                     case SUPER_WILDCARD:
 320                         ParameterizedTypeTree typeTree;
 321                         typeTree = (ParameterizedTypeTree) node.getReturnType();
 322                         testNode = typeTree.getTypeArguments().get(0);
 323                         break;


 327                         testNode = returnNode.getExpression();
 328                     }
 329                     if (testNode.getKind() != kind) {
 330                         log.printMessage(ERROR, testNode.getKind() + " != " + kind, e);
 331                         throw new AssertionError(testNode);
 332                     }
 333                     System.err.format("OK: %32s %s%n", kind, testNode);
 334                 } catch (Error ex) {
 335                     System.err.println("Error while looking at " + debug);
 336                     throw ex;
 337                 }
 338                 return null;
 339             }
 340         }
 341         Scan scan = new Scan();
 342         for (Element e : roundEnvironment.getRootElements()) {
 343             scan.scan(e);
 344         }
 345         return true;
 346     }

 347 }