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