Wed, 23 Sep 2009 18:29:41 -0700
6517779: javax.lang.model.util.Elements.getConstantExpression() doesn't throw any exception
6517907: javax.lang.model.util.Elements.getConstantExpression() with negative byte value fails
Summary: Fix various problems with Elements.getConstantExpression()
Reviewed-by: jjg
1.1 --- a/src/share/classes/com/sun/tools/javac/util/Constants.java Mon Sep 21 21:08:11 2009 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/util/Constants.java Wed Sep 23 18:29:41 2009 -0700 1.3 @@ -83,16 +83,28 @@ 1.4 */ 1.5 public static String format(Object value) { 1.6 if (value instanceof Byte) return formatByte((Byte) value); 1.7 + if (value instanceof Short) return formatShort((Short) value); 1.8 if (value instanceof Long) return formatLong((Long) value); 1.9 if (value instanceof Float) return formatFloat((Float) value); 1.10 if (value instanceof Double) return formatDouble((Double) value); 1.11 if (value instanceof Character) return formatChar((Character) value); 1.12 if (value instanceof String) return formatString((String) value); 1.13 - return value + ""; 1.14 + if (value instanceof Integer || 1.15 + value instanceof Boolean) return value.toString(); 1.16 + else 1.17 + throw new IllegalArgumentException("Argument is not a primitive type or a string; it " + 1.18 + ((value == null) ? 1.19 + "is a null value." : 1.20 + "has class " + 1.21 + value.getClass().getName()) + "." ); 1.22 } 1.23 1.24 private static String formatByte(byte b) { 1.25 - return String.format("0x%02x", b); 1.26 + return String.format("(byte)0x%02x", b); 1.27 + } 1.28 + 1.29 + private static String formatShort(short s) { 1.30 + return String.format("(short)%d", s); 1.31 } 1.32 1.33 private static String formatLong(long lng) {
2.1 --- a/src/share/classes/com/sun/tools/javac/util/Convert.java Mon Sep 21 21:08:11 2009 -0700 2.2 +++ b/src/share/classes/com/sun/tools/javac/util/Convert.java Wed Sep 23 18:29:41 2009 -0700 2.3 @@ -239,9 +239,9 @@ 2.4 case '\"': return "\\\""; 2.5 case '\\': return "\\\\"; 2.6 default: 2.7 - return (ch > 127 || isPrintableAscii(ch)) 2.8 + return (isPrintableAscii(ch)) 2.9 ? String.valueOf(ch) 2.10 - : String.format("\\%03o", (int) ch); 2.11 + : String.format("\\u%04x", (int) ch); 2.12 } 2.13 } 2.14
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/processing/model/util/elements/Foo.java Wed Sep 23 18:29:41 2009 -0700 3.3 @@ -0,0 +1,29 @@ 3.4 + 3.5 +/* 3.6 + * Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. 3.7 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.8 + * 3.9 + * This code is free software; you can redistribute it and/or modify it 3.10 + * under the terms of the GNU General Public License version 2 only, as 3.11 + * published by the Free Software Foundation. 3.12 + * 3.13 + * This code is distributed in the hope that it will be useful, but WITHOUT 3.14 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.15 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.16 + * version 2 for more details (a copy is included in the LICENSE file that 3.17 + * accompanied this code). 3.18 + * 3.19 + * You should have received a copy of the GNU General Public License version 3.20 + * 2 along with this work; if not, write to the Free Software Foundation, 3.21 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.22 + * 3.23 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 3.24 + * CA 95054 USA or visit www.sun.com if you need additional information or 3.25 + * have any questions. 3.26 + */ 3.27 + 3.28 +/** 3.29 + * Dummy type to compile. 3.30 + */ 3.31 +public class Foo { 3.32 +} 3.33 \ No newline at end of file
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/javac/processing/model/util/elements/TestGetConstantExpression.java Wed Sep 23 18:29:41 2009 -0700 4.3 @@ -0,0 +1,143 @@ 4.4 +/* 4.5 + * Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. 4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4.7 + * 4.8 + * This code is free software; you can redistribute it and/or modify it 4.9 + * under the terms of the GNU General Public License version 2 only, as 4.10 + * published by the Free Software Foundation. 4.11 + * 4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 4.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 4.15 + * version 2 for more details (a copy is included in the LICENSE file that 4.16 + * accompanied this code). 4.17 + * 4.18 + * You should have received a copy of the GNU General Public License version 4.19 + * 2 along with this work; if not, write to the Free Software Foundation, 4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 4.21 + * 4.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 4.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 4.24 + * have any questions. 4.25 + */ 4.26 + 4.27 +/* 4.28 + * @test 4.29 + * @bug 6471577 6517779 4.30 + * @summary Test Elements.getConstantExpression 4.31 + * @author Joseph D. Darcy 4.32 + * @build TestGetConstantExpression 4.33 + * @compile -processor TestGetConstantExpression Foo.java 4.34 + */ 4.35 + 4.36 +import java.util.Set; 4.37 +import javax.annotation.processing.*; 4.38 +import javax.lang.model.SourceVersion; 4.39 +import static javax.lang.model.SourceVersion.*; 4.40 +import javax.lang.model.element.*; 4.41 +import javax.lang.model.util.*; 4.42 +import static javax.lang.model.util.ElementFilter.*; 4.43 +import static javax.tools.Diagnostic.Kind.*; 4.44 +import static javax.tools.StandardLocation.*; 4.45 +import java.io.*; 4.46 + 4.47 +/** 4.48 + * Test basic workings of Elements.getConstantExpression. 4.49 + */ 4.50 +@SupportedAnnotationTypes("*") 4.51 +public class TestGetConstantExpression extends AbstractProcessor { 4.52 + private Elements eltUtils; 4.53 + private Filer filer; 4.54 + private int round = 1; 4.55 + 4.56 + /** 4.57 + * Check expected behavior on classes and packages. 4.58 + */ 4.59 + public boolean process(Set<? extends TypeElement> annotations, 4.60 + RoundEnvironment roundEnv) { 4.61 + int errors = 0; 4.62 + boolean processingOver = roundEnv.processingOver(); 4.63 + 4.64 + if (!processingOver && round == 1) { 4.65 + errors += expectIllegalArgumentException(null); 4.66 + errors += expectIllegalArgumentException(this); 4.67 + 4.68 + // Generate source code with various constant values and 4.69 + // make sure it compiles. 4.70 + 4.71 + try { 4.72 + PrintWriter pw = new PrintWriter(filer.createSourceFile("ConstantTest").openWriter()); 4.73 + try { 4.74 + Boolean[] booleans = {true, false}; 4.75 + Byte[] bytes = {Byte.MIN_VALUE, -1, 0, 1, Byte.MAX_VALUE}; 4.76 + Short[] shorts = {Short.MIN_VALUE, -1, 0, 1, Short.MAX_VALUE}; 4.77 + Integer[] ints = {Integer.MIN_VALUE, -1, 0, 1, Integer.MAX_VALUE}; 4.78 + Long[] longs = {Long.MIN_VALUE, -1L, 0L,1L, Long.MAX_VALUE}; 4.79 + Character[] chars = {Character.MIN_VALUE, ' ', '\t', 'a', 'b', 'c', '~', Character.MAX_VALUE}; 4.80 + Float[] floats = {Float.NaN, Float.NEGATIVE_INFINITY, -1.0f, -0.0f, 0.0f, 1.0f, Float.POSITIVE_INFINITY}; 4.81 + Double[] doubles = {Double.NaN, Double.NEGATIVE_INFINITY, -1.0, -0.0, 0.0, 1.0, Double.POSITIVE_INFINITY}; 4.82 + 4.83 + pw.println("class ConstantTest {"); 4.84 + pw.println(String.format(" private static boolean[] booleans = {%s};", 4.85 + printConstants(booleans))); 4.86 + pw.println(String.format(" private static byte[] bytes = {%s};", 4.87 + printConstants(bytes))); 4.88 + pw.println(String.format(" private static short[] shorts = {%s};", 4.89 + printConstants(shorts))); 4.90 + pw.println(String.format(" private static int[] ints = {%s};", 4.91 + printConstants(ints))); 4.92 + pw.println(String.format(" private static long[] longs = {%s};", 4.93 + printConstants(longs))); 4.94 + pw.println(String.format(" private static char[] chars = {%s};", 4.95 + printConstants(chars))); 4.96 + pw.println(String.format(" private static float[] floats = {%s};", 4.97 + printConstants(floats))); 4.98 + pw.println(String.format(" private static double[] doubles = {%s};", 4.99 + printConstants(doubles))); 4.100 + pw.println("}"); 4.101 + } finally { 4.102 + pw.close(); 4.103 + } 4.104 + } catch(IOException io) { 4.105 + throw new RuntimeException(io); 4.106 + } 4.107 + round++; 4.108 + } else if (processingOver) { 4.109 + if (errors > 0) { 4.110 + throw new RuntimeException(); 4.111 + } 4.112 + } 4.113 + return true; 4.114 + } 4.115 + 4.116 + String printConstants(Object[] constants) { 4.117 + StringBuilder sb = new StringBuilder(); 4.118 + 4.119 + for(Object o : constants) { 4.120 + sb.append(eltUtils.getConstantExpression(o)); 4.121 + sb.append(", "); 4.122 + } 4.123 + return sb.toString(); 4.124 + } 4.125 + 4.126 + int expectIllegalArgumentException(Object o) { 4.127 + String s = ""; 4.128 + try { 4.129 + s = eltUtils.getConstantExpression(o); 4.130 + System.err.println("Unexpected string returned: " + s); 4.131 + return 1; 4.132 + } catch (IllegalArgumentException iae) { 4.133 + return 0; 4.134 + } 4.135 + } 4.136 + 4.137 + public SourceVersion getSupportedSourceVersion() { 4.138 + return SourceVersion.latest(); 4.139 + } 4.140 + 4.141 + public void init(ProcessingEnvironment processingEnv) { 4.142 + super.init(processingEnv); 4.143 + eltUtils = processingEnv.getElementUtils(); 4.144 + filer = processingEnv.getFiler(); 4.145 + } 4.146 +}