TestGetConstantExpression.java revision 698:d2aaaec153e8
1/* 2 * Copyright (c) 2009, 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 6471577 6517779 27 * @summary Test Elements.getConstantExpression 28 * @author Joseph D. Darcy 29 * @library ../../../../lib 30 * @build JavacTestingAbstractProcessor TestGetConstantExpression 31 * @compile -processor TestGetConstantExpression Foo.java 32 */ 33 34import java.util.Set; 35import javax.annotation.processing.*; 36import javax.lang.model.SourceVersion; 37import static javax.lang.model.SourceVersion.*; 38import javax.lang.model.element.*; 39import javax.lang.model.util.*; 40import static javax.lang.model.util.ElementFilter.*; 41import static javax.tools.Diagnostic.Kind.*; 42import static javax.tools.StandardLocation.*; 43import java.io.*; 44 45/** 46 * Test basic workings of Elements.getConstantExpression. 47 */ 48public class TestGetConstantExpression extends JavacTestingAbstractProcessor { 49 private int round = 1; 50 51 /** 52 * Check expected behavior on classes and packages. 53 */ 54 public boolean process(Set<? extends TypeElement> annotations, 55 RoundEnvironment roundEnv) { 56 int errors = 0; 57 boolean processingOver = roundEnv.processingOver(); 58 59 if (!processingOver && round == 1) { 60 errors += expectIllegalArgumentException(null); 61 errors += expectIllegalArgumentException(this); 62 63 // Generate source code with various constant values and 64 // make sure it compiles. 65 66 try { 67 PrintWriter pw = new PrintWriter(filer.createSourceFile("ConstantTest").openWriter()); 68 try { 69 Boolean[] booleans = {true, false}; 70 Byte[] bytes = {Byte.MIN_VALUE, -1, 0, 1, Byte.MAX_VALUE}; 71 Short[] shorts = {Short.MIN_VALUE, -1, 0, 1, Short.MAX_VALUE}; 72 Integer[] ints = {Integer.MIN_VALUE, -1, 0, 1, Integer.MAX_VALUE}; 73 Long[] longs = {Long.MIN_VALUE, -1L, 0L,1L, Long.MAX_VALUE}; 74 Character[] chars = {Character.MIN_VALUE, ' ', '\t', 'a', 'b', 'c', '~', Character.MAX_VALUE}; 75 Float[] floats = {Float.NaN, Float.NEGATIVE_INFINITY, -1.0f, -0.0f, 0.0f, 1.0f, Float.POSITIVE_INFINITY}; 76 Double[] doubles = {Double.NaN, Double.NEGATIVE_INFINITY, -1.0, -0.0, 0.0, 1.0, Double.POSITIVE_INFINITY}; 77 78 pw.println("class ConstantTest {"); 79 pw.println(String.format(" private static boolean[] booleans = {%s};", 80 printConstants(booleans))); 81 pw.println(String.format(" private static byte[] bytes = {%s};", 82 printConstants(bytes))); 83 pw.println(String.format(" private static short[] shorts = {%s};", 84 printConstants(shorts))); 85 pw.println(String.format(" private static int[] ints = {%s};", 86 printConstants(ints))); 87 pw.println(String.format(" private static long[] longs = {%s};", 88 printConstants(longs))); 89 pw.println(String.format(" private static char[] chars = {%s};", 90 printConstants(chars))); 91 pw.println(String.format(" private static float[] floats = {%s};", 92 printConstants(floats))); 93 pw.println(String.format(" private static double[] doubles = {%s};", 94 printConstants(doubles))); 95 pw.println("}"); 96 } finally { 97 pw.close(); 98 } 99 } catch(IOException io) { 100 throw new RuntimeException(io); 101 } 102 round++; 103 } else if (processingOver) { 104 if (errors > 0) { 105 throw new RuntimeException(); 106 } 107 } 108 return true; 109 } 110 111 String printConstants(Object[] constants) { 112 StringBuilder sb = new StringBuilder(); 113 114 for(Object o : constants) { 115 sb.append(eltUtils.getConstantExpression(o)); 116 sb.append(", "); 117 } 118 return sb.toString(); 119 } 120 121 int expectIllegalArgumentException(Object o) { 122 String s = ""; 123 try { 124 s = eltUtils.getConstantExpression(o); 125 System.err.println("Unexpected string returned: " + s); 126 return 1; 127 } catch (IllegalArgumentException iae) { 128 return 0; 129 } 130 } 131} 132