413N/A/*
698N/A * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
413N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
413N/A *
413N/A * This code is free software; you can redistribute it and/or modify it
413N/A * under the terms of the GNU General Public License version 2 only, as
413N/A * published by the Free Software Foundation.
413N/A *
413N/A * This code is distributed in the hope that it will be useful, but WITHOUT
413N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
413N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
413N/A * version 2 for more details (a copy is included in the LICENSE file that
413N/A * accompanied this code).
413N/A *
413N/A * You should have received a copy of the GNU General Public License version
413N/A * 2 along with this work; if not, write to the Free Software Foundation,
413N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
413N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
413N/A */
413N/A
413N/A/*
413N/A * @test
413N/A * @bug 6471577 6517779
413N/A * @summary Test Elements.getConstantExpression
413N/A * @author Joseph D. Darcy
698N/A * @library ../../../../lib
698N/A * @build JavacTestingAbstractProcessor TestGetConstantExpression
413N/A * @compile -processor TestGetConstantExpression Foo.java
413N/A */
413N/A
413N/Aimport java.util.Set;
413N/Aimport javax.annotation.processing.*;
413N/Aimport javax.lang.model.SourceVersion;
413N/Aimport static javax.lang.model.SourceVersion.*;
413N/Aimport javax.lang.model.element.*;
413N/Aimport javax.lang.model.util.*;
413N/Aimport static javax.lang.model.util.ElementFilter.*;
413N/Aimport static javax.tools.Diagnostic.Kind.*;
413N/Aimport static javax.tools.StandardLocation.*;
413N/Aimport java.io.*;
413N/A
413N/A/**
413N/A * Test basic workings of Elements.getConstantExpression.
413N/A */
698N/Apublic class TestGetConstantExpression extends JavacTestingAbstractProcessor {
413N/A private int round = 1;
413N/A
413N/A /**
413N/A * Check expected behavior on classes and packages.
413N/A */
413N/A public boolean process(Set<? extends TypeElement> annotations,
413N/A RoundEnvironment roundEnv) {
413N/A int errors = 0;
413N/A boolean processingOver = roundEnv.processingOver();
413N/A
413N/A if (!processingOver && round == 1) {
413N/A errors += expectIllegalArgumentException(null);
413N/A errors += expectIllegalArgumentException(this);
413N/A
413N/A // Generate source code with various constant values and
413N/A // make sure it compiles.
413N/A
413N/A try {
413N/A PrintWriter pw = new PrintWriter(filer.createSourceFile("ConstantTest").openWriter());
413N/A try {
413N/A Boolean[] booleans = {true, false};
413N/A Byte[] bytes = {Byte.MIN_VALUE, -1, 0, 1, Byte.MAX_VALUE};
413N/A Short[] shorts = {Short.MIN_VALUE, -1, 0, 1, Short.MAX_VALUE};
413N/A Integer[] ints = {Integer.MIN_VALUE, -1, 0, 1, Integer.MAX_VALUE};
413N/A Long[] longs = {Long.MIN_VALUE, -1L, 0L,1L, Long.MAX_VALUE};
413N/A Character[] chars = {Character.MIN_VALUE, ' ', '\t', 'a', 'b', 'c', '~', Character.MAX_VALUE};
413N/A Float[] floats = {Float.NaN, Float.NEGATIVE_INFINITY, -1.0f, -0.0f, 0.0f, 1.0f, Float.POSITIVE_INFINITY};
413N/A Double[] doubles = {Double.NaN, Double.NEGATIVE_INFINITY, -1.0, -0.0, 0.0, 1.0, Double.POSITIVE_INFINITY};
413N/A
413N/A pw.println("class ConstantTest {");
413N/A pw.println(String.format(" private static boolean[] booleans = {%s};",
413N/A printConstants(booleans)));
413N/A pw.println(String.format(" private static byte[] bytes = {%s};",
413N/A printConstants(bytes)));
413N/A pw.println(String.format(" private static short[] shorts = {%s};",
413N/A printConstants(shorts)));
413N/A pw.println(String.format(" private static int[] ints = {%s};",
413N/A printConstants(ints)));
413N/A pw.println(String.format(" private static long[] longs = {%s};",
413N/A printConstants(longs)));
413N/A pw.println(String.format(" private static char[] chars = {%s};",
413N/A printConstants(chars)));
413N/A pw.println(String.format(" private static float[] floats = {%s};",
413N/A printConstants(floats)));
413N/A pw.println(String.format(" private static double[] doubles = {%s};",
413N/A printConstants(doubles)));
413N/A pw.println("}");
413N/A } finally {
413N/A pw.close();
413N/A }
413N/A } catch(IOException io) {
413N/A throw new RuntimeException(io);
413N/A }
413N/A round++;
413N/A } else if (processingOver) {
413N/A if (errors > 0) {
413N/A throw new RuntimeException();
413N/A }
413N/A }
413N/A return true;
413N/A }
413N/A
413N/A String printConstants(Object[] constants) {
413N/A StringBuilder sb = new StringBuilder();
413N/A
413N/A for(Object o : constants) {
413N/A sb.append(eltUtils.getConstantExpression(o));
413N/A sb.append(", ");
413N/A }
413N/A return sb.toString();
413N/A }
413N/A
413N/A int expectIllegalArgumentException(Object o) {
413N/A String s = "";
413N/A try {
413N/A s = eltUtils.getConstantExpression(o);
413N/A System.err.println("Unexpected string returned: " + s);
413N/A return 1;
413N/A } catch (IllegalArgumentException iae) {
413N/A return 0;
413N/A }
413N/A }
413N/A}