test/tools/javac/api/TestOperators.java

Tue, 28 Dec 2010 15:54:52 -0800

author
ohair
date
Tue, 28 Dec 2010 15:54:52 -0800
changeset 798
4868a36f6fd8
parent 575
9a7c998bf2fc
child 1054
111bbf1ad913
permissions
-rw-r--r--

6962318: Update copyright year
Reviewed-by: xdono

     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  */
    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  */
    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.*;
    39 import com.sun.source.tree.*;
    40 import com.sun.source.util.Trees;
    42 import static com.sun.source.tree.Tree.Kind.*;
    44 @interface TestMe {
    45     Tree.Kind value();
    46 }
    48 @SupportedAnnotationTypes("TestMe")
    49 public class TestOperators extends AbstractProcessor {
    51     @TestMe(POSTFIX_INCREMENT)
    52     public int test_POSTFIX_INCREMENT(int i) {
    53         return i++;
    54     }
    56     @TestMe(POSTFIX_DECREMENT)
    57     public int test_POSTFIX_DECREMENT(int i) {
    58         return i--;
    59     }
    61     @TestMe(PREFIX_INCREMENT)
    62     public int test_PREFIX_INCREMENT(int i) {
    63         return ++i;
    64     }
    66     @TestMe(PREFIX_DECREMENT)
    67     public int test_PREFIX_DECREMENT(int i) {
    68         return --i;
    69     }
    71     @TestMe(UNARY_PLUS)
    72     public int test_UNARY_PLUS(int i) {
    73         return +i;
    74     }
    76     @TestMe(UNARY_MINUS)
    77     public int test_UNARY_MINUS(int i) {
    78         return -i;
    79     }
    81     @TestMe(BITWISE_COMPLEMENT)
    82     public int test_BITWISE_COMPLEMENT(int i) {
    83         return ~i;
    84     }
    86     @TestMe(LOGICAL_COMPLEMENT)
    87     public boolean test_LOGICAL_COMPLEMENT(boolean b) {
    88         return !b;
    89     }
    91     @TestMe(MULTIPLY)
    92     public int test_MULTIPLY(int i, int j) {
    93         return i * j;
    94     }
    96     @TestMe(DIVIDE)
    97     public int test_DIVIDE(int i, int j) {
    98         return i / j;
    99     }
   101     @TestMe(REMAINDER)
   102     public int test_REMAINDER(int i, int j) {
   103         return i % j;
   104     }
   106     @TestMe(PLUS)
   107     public int test_PLUS(int i, int j) {
   108         return i + j;
   109     }
   111     @TestMe(MINUS)
   112     public int test_MINUS(int i, int j) {
   113         return i - j;
   114     }
   116     @TestMe(LEFT_SHIFT)
   117     public int test_LEFT_SHIFT(int i, int j) {
   118         return i << j;
   119     }
   121     @TestMe(RIGHT_SHIFT)
   122     public int test_RIGHT_SHIFT(int i, int j) {
   123         return i >> j;
   124     }
   126     @TestMe(UNSIGNED_RIGHT_SHIFT)
   127     public int test_UNSIGNED_RIGHT_SHIFT(int i, int j) {
   128         return i >>> j;
   129     }
   131     @TestMe(LESS_THAN)
   132     public boolean test_LESS_THAN(int i, int j) {
   133         return i < j;
   134     }
   136     @TestMe(GREATER_THAN)
   137     public boolean test_GREATER_THAN(int i, int j) {
   138         return i > j;
   139     }
   141     @TestMe(LESS_THAN_EQUAL)
   142     public boolean test_LESS_THAN_EQUAL(int i, int j) {
   143         return i <= j;
   144     }
   146     @TestMe(GREATER_THAN_EQUAL)
   147     public boolean test_GREATER_THAN_EQUAL(int i, int j) {
   148         return i >= j;
   149     }
   151     @TestMe(EQUAL_TO)
   152     public boolean test_EQUAL_TO(int i, int j) {
   153         return i == j;
   154     }
   156     @TestMe(NOT_EQUAL_TO)
   157     public boolean test_NOT_EQUAL_TO(int i, int j) {
   158         return i != j;
   159     }
   161     @TestMe(AND)
   162     public boolean test_AND(boolean a, boolean b) {
   163         return a & b;
   164     }
   166     @TestMe(XOR)
   167     public boolean test_XOR(boolean a, boolean b) {
   168         return a ^ b;
   169     }
   171     @TestMe(OR)
   172     public boolean test_OR(boolean a, boolean b) {
   173         return a | b;
   174     }
   176     @TestMe(CONDITIONAL_AND)
   177     public boolean test_CONDITIONAL_AND(boolean a, boolean b) {
   178         return a && b;
   179     }
   181     @TestMe(CONDITIONAL_OR)
   182     public boolean test_CONDITIONAL_OR(boolean a, boolean b) {
   183         return a || b;
   184     }
   186     @TestMe(MULTIPLY_ASSIGNMENT)
   187     public int test_MULTIPLY_ASSIGNMENT(int i, int j) {
   188         return i *= j;
   189     }
   191     @TestMe(DIVIDE_ASSIGNMENT)
   192     public int test_DIVIDE_ASSIGNMENT(int i, int j) {
   193         return i /= j;
   194     }
   196     @TestMe(REMAINDER_ASSIGNMENT)
   197     public int test_REMAINDER_ASSIGNMENT(int i, int j) {
   198         return i %= j;
   199     }
   201     @TestMe(PLUS_ASSIGNMENT)
   202     public int test_PLUS_ASSIGNMENT(int i, int j) {
   203         return i += j;
   204     }
   206     @TestMe(MINUS_ASSIGNMENT)
   207     public int test_MINUS_ASSIGNMENT(int i, int j) {
   208         return i -= j;
   209     }
   211     @TestMe(LEFT_SHIFT_ASSIGNMENT)
   212     public int test_LEFT_SHIFT_ASSIGNMENT(int i, int j) {
   213         return i <<= j;
   214     }
   216     @TestMe(RIGHT_SHIFT_ASSIGNMENT)
   217     public int test_RIGHT_SHIFT_ASSIGNMENT(int i, int j) {
   218         return i >>= j;
   219     }
   221     @TestMe(UNSIGNED_RIGHT_SHIFT_ASSIGNMENT)
   222     public int test_UNSIGNED_RIGHT_SHIFT_ASSIGNMENT(int i, int j) {
   223         return i >>>= j;
   224     }
   226     @TestMe(AND_ASSIGNMENT)
   227     public boolean test_AND_ASSIGNMENT(boolean a, boolean b) {
   228         return a &= b;
   229     }
   231     @TestMe(XOR_ASSIGNMENT)
   232     public boolean test_XOR_ASSIGNMENT(boolean a, boolean b) {
   233         return a ^= b;
   234     }
   236     @TestMe(OR_ASSIGNMENT)
   237     public boolean test_OR_ASSIGNMENT(boolean a, boolean b) {
   238         return a |= b;
   239     }
   241     @TestMe(INT_LITERAL)
   242     public Object test_INT_LITERAL() {
   243         return 0;
   244     }
   246     @TestMe(LONG_LITERAL)
   247     public Object test_LONG_LITERAL() {
   248         return 0L;
   249     }
   251     @TestMe(FLOAT_LITERAL)
   252     public Object test_FLOAT_LITERAL() {
   253         return 0.0F;
   254     }
   256     @TestMe(DOUBLE_LITERAL)
   257     public Object test_DOUBLE_LITERAL() {
   258         return 0.0;
   259     }
   261     @TestMe(BOOLEAN_LITERAL)
   262     public Object test_BOOLEAN_LITERAL() {
   263         return true;
   264     }
   266     @TestMe(CHAR_LITERAL)
   267     public Object test_CHAR_LITERAL() {
   268         return 'a';
   269     }
   271     @TestMe(STRING_LITERAL)
   272     public Object test_STRING_LITERAL() {
   273         return "a";
   274     }
   276     @TestMe(NULL_LITERAL)
   277     public Object test_NULL_LITERAL() {
   278         return null;
   279     }
   281     @TestMe(UNBOUNDED_WILDCARD)
   282     public Set<?> test_UNBOUNDED_WILDCARD() {
   283         return null;
   284     }
   286     @TestMe(EXTENDS_WILDCARD)
   287     public Set<? extends Number> test_EXTENDS_WILDCARD() {
   288         return null;
   289     }
   291     @TestMe(SUPER_WILDCARD)
   292     public Set<? super Number> test_SUPER_WILDCARD() {
   293         return null;
   294     }
   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;
   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;
   323                     default:
   324                         ReturnTree returnNode;
   325                         returnNode = (ReturnTree) node.getBody().getStatements().get(0);
   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     }
   347 }

mercurial