1 /* 2 * Copyright (c) 2004, 2008, 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 /* 26 * @test 27 * @bug 5027675 5048535 28 * @summary Tests FieldDeclaration.getConstantExpression method 29 * @library ../../lib 30 * @compile -source 1.5 ConstExpr.java 31 * @run main/othervm ConstExpr 32 */ 33 34 35 import java.math.RoundingMode; 36 import java.util.*; 37 38 import com.sun.mirror.declaration.*; 39 import com.sun.mirror.type.*; 40 import com.sun.mirror.util.*; 41 42 import static java.math.RoundingMode.UP; 43 44 import static com.sun.mirror.util.DeclarationVisitors.*; 45 46 47 public class ConstExpr extends Tester { 48 49 public static void main(String[] args) { 50 (new ConstExpr()).run(); 51 } 52 53 54 // Declarations used by tests 55 56 public static final byte B = (byte) 0xBE; 57 public static final short S = (short) 32767; 58 public static final int I = -4; 59 public static final long l = 4294967296L; 60 public static final float f = 3.5f; 61 public static final double PI = Math.PI; 62 public static final char C = 'C'; 63 public static final String STR = "cheese"; 64 65 public static final char SMILEY = '\u263A'; 66 public static final String TWOLINES = "ab\ncd"; 67 68 public static final double D1 = Double.POSITIVE_INFINITY; 69 public static final double D2 = Double.NEGATIVE_INFINITY; 70 public static final double D3 = Double.NaN; 71 public static final float F1 = Float.POSITIVE_INFINITY; 72 public static final float F2 = Float.NEGATIVE_INFINITY; 73 public static final float F3 = Float.NaN; 74 75 public static final String NOSTR = null; // not a compile-time constant 76 public static final RoundingMode R = UP; // not a compile-time constant 77 78 79 @Test(result={ 80 "0xbe", 81 "32767", 82 "-4", 83 "4294967296L", 84 "3.5f", 85 "3.141592653589793", 86 "'C'", 87 "\"cheese\"", 88 89 "'\\u263a'", 90 "\"ab\\ncd\"", 91 92 "1.0/0.0", 93 "-1.0/0.0", 94 "0.0/0.0", 95 "1.0f/0.0f", 96 "-1.0f/0.0f", 97 "0.0f/0.0f", 98 99 "null", 100 "null" 101 }, 102 ordered=true) 103 Collection<String> getConstantExpression() { 104 final Collection<String> res = new ArrayList<String>(); 105 106 thisClassDecl.accept( 107 DeclarationVisitors.getSourceOrderDeclarationScanner( 108 NO_OP, 109 new SimpleDeclarationVisitor() { 110 public void visitFieldDeclaration(FieldDeclaration f) { 111 res.add(f.getConstantExpression()); 112 } 113 })); 114 return res; 115 } 116 }