1/* 2 * Copyright (c) 2005, 2015, 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 * @modules jdk.compiler 31 * @build JavacTestingAbstractProcessor TestOperators 32 * @compile -processor TestOperators -proc:only TestOperators.java 33 */ 34 35import java.util.Set; 36import javax.annotation.processing.*; 37import javax.lang.model.element.*; 38import javax.lang.model.util.*; 39import static javax.tools.Diagnostic.Kind.*; 40 41import com.sun.source.tree.*; 42import com.sun.source.util.Trees; 43 44import static com.sun.source.tree.Tree.Kind.*; 45 46@interface TestMe { 47 Tree.Kind value(); 48} 49 50@SupportedAnnotationTypes("TestMe") 51public class TestOperators extends JavacTestingAbstractProcessor { 52 53 @TestMe(POSTFIX_INCREMENT) 54 public int test_POSTFIX_INCREMENT(int i) { 55 return i++; 56 } 57 58 @TestMe(POSTFIX_DECREMENT) 59 public int test_POSTFIX_DECREMENT(int i) { 60 return i--; 61 } 62 63 @TestMe(PREFIX_INCREMENT) 64 public int test_PREFIX_INCREMENT(int i) { 65 return ++i; 66 } 67 68 @TestMe(PREFIX_DECREMENT) 69 public int test_PREFIX_DECREMENT(int i) { 70 return --i; 71 } 72 73 @TestMe(UNARY_PLUS) 74 public int test_UNARY_PLUS(int i) { 75 return +i; 76 } 77 78 @TestMe(UNARY_MINUS) 79 public int test_UNARY_MINUS(int i) { 80 return -i; 81 } 82 83 @TestMe(BITWISE_COMPLEMENT) 84 public int test_BITWISE_COMPLEMENT(int i) { 85 return ~i; 86 } 87 88 @TestMe(LOGICAL_COMPLEMENT) 89 public boolean test_LOGICAL_COMPLEMENT(boolean b) { 90 return !b; 91 } 92 93 @TestMe(MULTIPLY) 94 public int test_MULTIPLY(int i, int j) { 95 return i * j; 96 } 97 98 @TestMe(DIVIDE) 99 public int test_DIVIDE(int i, int j) { 100 return i / j; 101 } 102 103 @TestMe(REMAINDER) 104 public int test_REMAINDER(int i, int j) { 105 return i % j; 106 } 107 108 @TestMe(PLUS) 109 public int test_PLUS(int i, int j) { 110 return i + j; 111 } 112 113 @TestMe(MINUS) 114 public int test_MINUS(int i, int j) { 115 return i - j; 116 } 117 118 @TestMe(LEFT_SHIFT) 119 public int test_LEFT_SHIFT(int i, int j) { 120 return i << j; 121 } 122 123 @TestMe(RIGHT_SHIFT) 124 public int test_RIGHT_SHIFT(int i, int j) { 125 return i >> j; 126 } 127 128 @TestMe(UNSIGNED_RIGHT_SHIFT) 129 public int test_UNSIGNED_RIGHT_SHIFT(int i, int j) { 130 return i >>> j; 131 } 132 133 @TestMe(LESS_THAN) 134 public boolean test_LESS_THAN(int i, int j) { 135 return i < j; 136 } 137 138 @TestMe(GREATER_THAN) 139 public boolean test_GREATER_THAN(int i, int j) { 140 return i > j; 141 } 142 143 @TestMe(LESS_THAN_EQUAL) 144 public boolean test_LESS_THAN_EQUAL(int i, int j) { 145 return i <= j; 146 } 147 148 @TestMe(GREATER_THAN_EQUAL) 149 public boolean test_GREATER_THAN_EQUAL(int i, int j) { 150 return i >= j; 151 } 152 153 @TestMe(EQUAL_TO) 154 public boolean test_EQUAL_TO(int i, int j) { 155 return i == j; 156 } 157 158 @TestMe(NOT_EQUAL_TO) 159 public boolean test_NOT_EQUAL_TO(int i, int j) { 160 return i != j; 161 } 162 163 @TestMe(AND) 164 public boolean test_AND(boolean a, boolean b) { 165 return a & b; 166 } 167 168 @TestMe(XOR) 169 public boolean test_XOR(boolean a, boolean b) { 170 return a ^ b; 171 } 172 173 @TestMe(OR) 174 public boolean test_OR(boolean a, boolean b) { 175 return a | b; 176 } 177 178 @TestMe(CONDITIONAL_AND) 179 public boolean test_CONDITIONAL_AND(boolean a, boolean b) { 180 return a && b; 181 } 182 183 @TestMe(CONDITIONAL_OR) 184 public boolean test_CONDITIONAL_OR(boolean a, boolean b) { 185 return a || b; 186 } 187 188 @TestMe(MULTIPLY_ASSIGNMENT) 189 public int test_MULTIPLY_ASSIGNMENT(int i, int j) { 190 return i *= j; 191 } 192 193 @TestMe(DIVIDE_ASSIGNMENT) 194 public int test_DIVIDE_ASSIGNMENT(int i, int j) { 195 return i /= j; 196 } 197 198 @TestMe(REMAINDER_ASSIGNMENT) 199 public int test_REMAINDER_ASSIGNMENT(int i, int j) { 200 return i %= j; 201 } 202 203 @TestMe(PLUS_ASSIGNMENT) 204 public int test_PLUS_ASSIGNMENT(int i, int j) { 205 return i += j; 206 } 207 208 @TestMe(MINUS_ASSIGNMENT) 209 public int test_MINUS_ASSIGNMENT(int i, int j) { 210 return i -= j; 211 } 212 213 @TestMe(LEFT_SHIFT_ASSIGNMENT) 214 public int test_LEFT_SHIFT_ASSIGNMENT(int i, int j) { 215 return i <<= j; 216 } 217 218 @TestMe(RIGHT_SHIFT_ASSIGNMENT) 219 public int test_RIGHT_SHIFT_ASSIGNMENT(int i, int j) { 220 return i >>= j; 221 } 222 223 @TestMe(UNSIGNED_RIGHT_SHIFT_ASSIGNMENT) 224 public int test_UNSIGNED_RIGHT_SHIFT_ASSIGNMENT(int i, int j) { 225 return i >>>= j; 226 } 227 228 @TestMe(AND_ASSIGNMENT) 229 public boolean test_AND_ASSIGNMENT(boolean a, boolean b) { 230 return a &= b; 231 } 232 233 @TestMe(XOR_ASSIGNMENT) 234 public boolean test_XOR_ASSIGNMENT(boolean a, boolean b) { 235 return a ^= b; 236 } 237 238 @TestMe(OR_ASSIGNMENT) 239 public boolean test_OR_ASSIGNMENT(boolean a, boolean b) { 240 return a |= b; 241 } 242 243 @TestMe(INT_LITERAL) 244 public Object test_INT_LITERAL() { 245 return 0; 246 } 247 248 @TestMe(LONG_LITERAL) 249 public Object test_LONG_LITERAL() { 250 return 0L; 251 } 252 253 @TestMe(FLOAT_LITERAL) 254 public Object test_FLOAT_LITERAL() { 255 return 0.0F; 256 } 257 258 @TestMe(DOUBLE_LITERAL) 259 public Object test_DOUBLE_LITERAL() { 260 return 0.0; 261 } 262 263 @TestMe(BOOLEAN_LITERAL) 264 public Object test_BOOLEAN_LITERAL() { 265 return true; 266 } 267 268 @TestMe(CHAR_LITERAL) 269 public Object test_CHAR_LITERAL() { 270 return 'a'; 271 } 272 273 @TestMe(STRING_LITERAL) 274 public Object test_STRING_LITERAL() { 275 return "a"; 276 } 277 278 @TestMe(NULL_LITERAL) 279 public Object test_NULL_LITERAL() { 280 return null; 281 } 282 283 @TestMe(UNBOUNDED_WILDCARD) 284 public Set<?> test_UNBOUNDED_WILDCARD() { 285 return null; 286 } 287 288 @TestMe(EXTENDS_WILDCARD) 289 public Set<? extends Number> test_EXTENDS_WILDCARD() { 290 return null; 291 } 292 293 @TestMe(SUPER_WILDCARD) 294 public Set<? super Number> test_SUPER_WILDCARD() { 295 return null; 296 } 297 298 public boolean process(Set<? extends TypeElement> annotations, 299 RoundEnvironment roundEnvironment) 300 { 301 final Trees trees = Trees.instance(processingEnv); 302 final Messager log = processingEnv.getMessager(); 303 final Elements elements = processingEnv.getElementUtils(); 304 class Scan extends ElementScanner<Void,Void> { 305 @Override 306 public Void visitExecutable(ExecutableElement e, Void p) { 307 Object debug = e; // info for exception handler 308 try { 309 TestMe info = e.getAnnotation(TestMe.class); 310 if (info == null) 311 return null; 312 313 Tree.Kind kind = info.value(); 314 MethodTree node = trees.getTree(e); 315 debug = node; 316 Tree testNode; 317 switch (kind) { 318 case UNBOUNDED_WILDCARD: 319 case EXTENDS_WILDCARD: 320 case SUPER_WILDCARD: 321 ParameterizedTypeTree typeTree; 322 typeTree = (ParameterizedTypeTree) node.getReturnType(); 323 testNode = typeTree.getTypeArguments().get(0); 324 break; 325 default: 326 ReturnTree returnNode; 327 returnNode = (ReturnTree) node.getBody().getStatements().get(0); 328 testNode = returnNode.getExpression(); 329 } 330 if (testNode.getKind() != kind) { 331 log.printMessage(ERROR, testNode.getKind() + " != " + kind, e); 332 throw new AssertionError(testNode); 333 } 334 System.err.format("OK: %32s %s%n", kind, testNode); 335 } catch (Error ex) { 336 System.err.println("Error while looking at " + debug); 337 throw ex; 338 } 339 return null; 340 } 341 } 342 Scan scan = new Scan(); 343 for (Element e : roundEnvironment.getRootElements()) { 344 scan.scan(e); 345 } 346 return true; 347 } 348} 349