test/tools/javac/api/TestOperators.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/api/TestOperators.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,347 @@
     1.4 +/*
     1.5 + * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug     6338064 6346249 6340951 6392177
    1.30 + * @summary Tree API: can't determine kind of operator
    1.31 + * @author  Peter von der Ah\u00e9
    1.32 + * @library ../lib
    1.33 + * @build JavacTestingAbstractProcessor TestOperators
    1.34 + * @compile -processor TestOperators -proc:only TestOperators.java
    1.35 + */
    1.36 +
    1.37 +import java.util.Set;
    1.38 +import javax.annotation.processing.*;
    1.39 +import javax.lang.model.element.*;
    1.40 +import javax.lang.model.util.*;
    1.41 +import static javax.tools.Diagnostic.Kind.*;
    1.42 +
    1.43 +import com.sun.source.tree.*;
    1.44 +import com.sun.source.util.Trees;
    1.45 +
    1.46 +import static com.sun.source.tree.Tree.Kind.*;
    1.47 +
    1.48 +@interface TestMe {
    1.49 +    Tree.Kind value();
    1.50 +}
    1.51 +
    1.52 +@SupportedAnnotationTypes("TestMe")
    1.53 +public class TestOperators extends JavacTestingAbstractProcessor {
    1.54 +
    1.55 +    @TestMe(POSTFIX_INCREMENT)
    1.56 +    public int test_POSTFIX_INCREMENT(int i) {
    1.57 +        return i++;
    1.58 +    }
    1.59 +
    1.60 +    @TestMe(POSTFIX_DECREMENT)
    1.61 +    public int test_POSTFIX_DECREMENT(int i) {
    1.62 +        return i--;
    1.63 +    }
    1.64 +
    1.65 +    @TestMe(PREFIX_INCREMENT)
    1.66 +    public int test_PREFIX_INCREMENT(int i) {
    1.67 +        return ++i;
    1.68 +    }
    1.69 +
    1.70 +    @TestMe(PREFIX_DECREMENT)
    1.71 +    public int test_PREFIX_DECREMENT(int i) {
    1.72 +        return --i;
    1.73 +    }
    1.74 +
    1.75 +    @TestMe(UNARY_PLUS)
    1.76 +    public int test_UNARY_PLUS(int i) {
    1.77 +        return +i;
    1.78 +    }
    1.79 +
    1.80 +    @TestMe(UNARY_MINUS)
    1.81 +    public int test_UNARY_MINUS(int i) {
    1.82 +        return -i;
    1.83 +    }
    1.84 +
    1.85 +    @TestMe(BITWISE_COMPLEMENT)
    1.86 +    public int test_BITWISE_COMPLEMENT(int i) {
    1.87 +        return ~i;
    1.88 +    }
    1.89 +
    1.90 +    @TestMe(LOGICAL_COMPLEMENT)
    1.91 +    public boolean test_LOGICAL_COMPLEMENT(boolean b) {
    1.92 +        return !b;
    1.93 +    }
    1.94 +
    1.95 +    @TestMe(MULTIPLY)
    1.96 +    public int test_MULTIPLY(int i, int j) {
    1.97 +        return i * j;
    1.98 +    }
    1.99 +
   1.100 +    @TestMe(DIVIDE)
   1.101 +    public int test_DIVIDE(int i, int j) {
   1.102 +        return i / j;
   1.103 +    }
   1.104 +
   1.105 +    @TestMe(REMAINDER)
   1.106 +    public int test_REMAINDER(int i, int j) {
   1.107 +        return i % j;
   1.108 +    }
   1.109 +
   1.110 +    @TestMe(PLUS)
   1.111 +    public int test_PLUS(int i, int j) {
   1.112 +        return i + j;
   1.113 +    }
   1.114 +
   1.115 +    @TestMe(MINUS)
   1.116 +    public int test_MINUS(int i, int j) {
   1.117 +        return i - j;
   1.118 +    }
   1.119 +
   1.120 +    @TestMe(LEFT_SHIFT)
   1.121 +    public int test_LEFT_SHIFT(int i, int j) {
   1.122 +        return i << j;
   1.123 +    }
   1.124 +
   1.125 +    @TestMe(RIGHT_SHIFT)
   1.126 +    public int test_RIGHT_SHIFT(int i, int j) {
   1.127 +        return i >> j;
   1.128 +    }
   1.129 +
   1.130 +    @TestMe(UNSIGNED_RIGHT_SHIFT)
   1.131 +    public int test_UNSIGNED_RIGHT_SHIFT(int i, int j) {
   1.132 +        return i >>> j;
   1.133 +    }
   1.134 +
   1.135 +    @TestMe(LESS_THAN)
   1.136 +    public boolean test_LESS_THAN(int i, int j) {
   1.137 +        return i < j;
   1.138 +    }
   1.139 +
   1.140 +    @TestMe(GREATER_THAN)
   1.141 +    public boolean test_GREATER_THAN(int i, int j) {
   1.142 +        return i > j;
   1.143 +    }
   1.144 +
   1.145 +    @TestMe(LESS_THAN_EQUAL)
   1.146 +    public boolean test_LESS_THAN_EQUAL(int i, int j) {
   1.147 +        return i <= j;
   1.148 +    }
   1.149 +
   1.150 +    @TestMe(GREATER_THAN_EQUAL)
   1.151 +    public boolean test_GREATER_THAN_EQUAL(int i, int j) {
   1.152 +        return i >= j;
   1.153 +    }
   1.154 +
   1.155 +    @TestMe(EQUAL_TO)
   1.156 +    public boolean test_EQUAL_TO(int i, int j) {
   1.157 +        return i == j;
   1.158 +    }
   1.159 +
   1.160 +    @TestMe(NOT_EQUAL_TO)
   1.161 +    public boolean test_NOT_EQUAL_TO(int i, int j) {
   1.162 +        return i != j;
   1.163 +    }
   1.164 +
   1.165 +    @TestMe(AND)
   1.166 +    public boolean test_AND(boolean a, boolean b) {
   1.167 +        return a & b;
   1.168 +    }
   1.169 +
   1.170 +    @TestMe(XOR)
   1.171 +    public boolean test_XOR(boolean a, boolean b) {
   1.172 +        return a ^ b;
   1.173 +    }
   1.174 +
   1.175 +    @TestMe(OR)
   1.176 +    public boolean test_OR(boolean a, boolean b) {
   1.177 +        return a | b;
   1.178 +    }
   1.179 +
   1.180 +    @TestMe(CONDITIONAL_AND)
   1.181 +    public boolean test_CONDITIONAL_AND(boolean a, boolean b) {
   1.182 +        return a && b;
   1.183 +    }
   1.184 +
   1.185 +    @TestMe(CONDITIONAL_OR)
   1.186 +    public boolean test_CONDITIONAL_OR(boolean a, boolean b) {
   1.187 +        return a || b;
   1.188 +    }
   1.189 +
   1.190 +    @TestMe(MULTIPLY_ASSIGNMENT)
   1.191 +    public int test_MULTIPLY_ASSIGNMENT(int i, int j) {
   1.192 +        return i *= j;
   1.193 +    }
   1.194 +
   1.195 +    @TestMe(DIVIDE_ASSIGNMENT)
   1.196 +    public int test_DIVIDE_ASSIGNMENT(int i, int j) {
   1.197 +        return i /= j;
   1.198 +    }
   1.199 +
   1.200 +    @TestMe(REMAINDER_ASSIGNMENT)
   1.201 +    public int test_REMAINDER_ASSIGNMENT(int i, int j) {
   1.202 +        return i %= j;
   1.203 +    }
   1.204 +
   1.205 +    @TestMe(PLUS_ASSIGNMENT)
   1.206 +    public int test_PLUS_ASSIGNMENT(int i, int j) {
   1.207 +        return i += j;
   1.208 +    }
   1.209 +
   1.210 +    @TestMe(MINUS_ASSIGNMENT)
   1.211 +    public int test_MINUS_ASSIGNMENT(int i, int j) {
   1.212 +        return i -= j;
   1.213 +    }
   1.214 +
   1.215 +    @TestMe(LEFT_SHIFT_ASSIGNMENT)
   1.216 +    public int test_LEFT_SHIFT_ASSIGNMENT(int i, int j) {
   1.217 +        return i <<= j;
   1.218 +    }
   1.219 +
   1.220 +    @TestMe(RIGHT_SHIFT_ASSIGNMENT)
   1.221 +    public int test_RIGHT_SHIFT_ASSIGNMENT(int i, int j) {
   1.222 +        return i >>= j;
   1.223 +    }
   1.224 +
   1.225 +    @TestMe(UNSIGNED_RIGHT_SHIFT_ASSIGNMENT)
   1.226 +    public int test_UNSIGNED_RIGHT_SHIFT_ASSIGNMENT(int i, int j) {
   1.227 +        return i >>>= j;
   1.228 +    }
   1.229 +
   1.230 +    @TestMe(AND_ASSIGNMENT)
   1.231 +    public boolean test_AND_ASSIGNMENT(boolean a, boolean b) {
   1.232 +        return a &= b;
   1.233 +    }
   1.234 +
   1.235 +    @TestMe(XOR_ASSIGNMENT)
   1.236 +    public boolean test_XOR_ASSIGNMENT(boolean a, boolean b) {
   1.237 +        return a ^= b;
   1.238 +    }
   1.239 +
   1.240 +    @TestMe(OR_ASSIGNMENT)
   1.241 +    public boolean test_OR_ASSIGNMENT(boolean a, boolean b) {
   1.242 +        return a |= b;
   1.243 +    }
   1.244 +
   1.245 +    @TestMe(INT_LITERAL)
   1.246 +    public Object test_INT_LITERAL() {
   1.247 +        return 0;
   1.248 +    }
   1.249 +
   1.250 +    @TestMe(LONG_LITERAL)
   1.251 +    public Object test_LONG_LITERAL() {
   1.252 +        return 0L;
   1.253 +    }
   1.254 +
   1.255 +    @TestMe(FLOAT_LITERAL)
   1.256 +    public Object test_FLOAT_LITERAL() {
   1.257 +        return 0.0F;
   1.258 +    }
   1.259 +
   1.260 +    @TestMe(DOUBLE_LITERAL)
   1.261 +    public Object test_DOUBLE_LITERAL() {
   1.262 +        return 0.0;
   1.263 +    }
   1.264 +
   1.265 +    @TestMe(BOOLEAN_LITERAL)
   1.266 +    public Object test_BOOLEAN_LITERAL() {
   1.267 +        return true;
   1.268 +    }
   1.269 +
   1.270 +    @TestMe(CHAR_LITERAL)
   1.271 +    public Object test_CHAR_LITERAL() {
   1.272 +        return 'a';
   1.273 +    }
   1.274 +
   1.275 +    @TestMe(STRING_LITERAL)
   1.276 +    public Object test_STRING_LITERAL() {
   1.277 +        return "a";
   1.278 +    }
   1.279 +
   1.280 +    @TestMe(NULL_LITERAL)
   1.281 +    public Object test_NULL_LITERAL() {
   1.282 +        return null;
   1.283 +    }
   1.284 +
   1.285 +    @TestMe(UNBOUNDED_WILDCARD)
   1.286 +    public Set<?> test_UNBOUNDED_WILDCARD() {
   1.287 +        return null;
   1.288 +    }
   1.289 +
   1.290 +    @TestMe(EXTENDS_WILDCARD)
   1.291 +    public Set<? extends Number> test_EXTENDS_WILDCARD() {
   1.292 +        return null;
   1.293 +    }
   1.294 +
   1.295 +    @TestMe(SUPER_WILDCARD)
   1.296 +    public Set<? super Number> test_SUPER_WILDCARD() {
   1.297 +        return null;
   1.298 +    }
   1.299 +
   1.300 +    public boolean process(Set<? extends TypeElement> annotations,
   1.301 +                           RoundEnvironment roundEnvironment)
   1.302 +    {
   1.303 +        final Trees trees = Trees.instance(processingEnv);
   1.304 +        final Messager log = processingEnv.getMessager();
   1.305 +        final Elements elements = processingEnv.getElementUtils();
   1.306 +        class Scan extends ElementScanner<Void,Void> {
   1.307 +            @Override
   1.308 +            public Void visitExecutable(ExecutableElement e, Void p) {
   1.309 +                Object debug = e; // info for exception handler
   1.310 +                try {
   1.311 +                    TestMe info = e.getAnnotation(TestMe.class);
   1.312 +                    if (info == null)
   1.313 +                        return null;
   1.314 +
   1.315 +                    Tree.Kind kind = info.value();
   1.316 +                    MethodTree node = trees.getTree(e);
   1.317 +                    debug = node;
   1.318 +                    Tree testNode;
   1.319 +                    switch (kind) {
   1.320 +                    case UNBOUNDED_WILDCARD:
   1.321 +                    case EXTENDS_WILDCARD:
   1.322 +                    case SUPER_WILDCARD:
   1.323 +                        ParameterizedTypeTree typeTree;
   1.324 +                        typeTree = (ParameterizedTypeTree) node.getReturnType();
   1.325 +                        testNode = typeTree.getTypeArguments().get(0);
   1.326 +                        break;
   1.327 +                    default:
   1.328 +                        ReturnTree returnNode;
   1.329 +                        returnNode = (ReturnTree) node.getBody().getStatements().get(0);
   1.330 +                        testNode = returnNode.getExpression();
   1.331 +                    }
   1.332 +                    if (testNode.getKind() != kind) {
   1.333 +                        log.printMessage(ERROR, testNode.getKind() + " != " + kind, e);
   1.334 +                        throw new AssertionError(testNode);
   1.335 +                    }
   1.336 +                    System.err.format("OK: %32s %s%n", kind, testNode);
   1.337 +                } catch (Error ex) {
   1.338 +                    System.err.println("Error while looking at " + debug);
   1.339 +                    throw ex;
   1.340 +                }
   1.341 +                return null;
   1.342 +            }
   1.343 +        }
   1.344 +        Scan scan = new Scan();
   1.345 +        for (Element e : roundEnvironment.getRootElements()) {
   1.346 +            scan.scan(e);
   1.347 +        }
   1.348 +        return true;
   1.349 +    }
   1.350 +}

mercurial