TestGetConstantExpression.java revision 413:e992e602788e
1115170Speter/*
299127Sobrien * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
399127Sobrien * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
499127Sobrien *
599127Sobrien * This code is free software; you can redistribute it and/or modify it
699127Sobrien * under the terms of the GNU General Public License version 2 only, as
799127Sobrien * published by the Free Software Foundation.
899127Sobrien *
999127Sobrien * This code is distributed in the hope that it will be useful, but WITHOUT
1099127Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1199127Sobrien * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12115170Speter * version 2 for more details (a copy is included in the LICENSE file that
1399127Sobrien * accompanied this code).
1499127Sobrien *
1599127Sobrien * You should have received a copy of the GNU General Public License version
16115170Speter * 2 along with this work; if not, write to the Free Software Foundation,
1799127Sobrien * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1899127Sobrien *
1999127Sobrien * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
2099127Sobrien * CA 95054 USA or visit www.sun.com if you need additional information or
21169612Swkoszek * have any questions.
2299127Sobrien */
2399127Sobrien
2499127Sobrien/*
2599127Sobrien * @test
2699127Sobrien * @bug 6471577 6517779
2799127Sobrien * @summary Test Elements.getConstantExpression
2899127Sobrien * @author  Joseph D. Darcy
2999127Sobrien * @build TestGetConstantExpression
3099127Sobrien * @compile -processor TestGetConstantExpression Foo.java
3199127Sobrien */
3299127Sobrien
3399127Sobrienimport java.util.Set;
34120589Speterimport javax.annotation.processing.*;
35174395Sjkoshyimport javax.lang.model.SourceVersion;
36174395Sjkoshyimport static javax.lang.model.SourceVersion.*;
37115405Speterimport javax.lang.model.element.*;
38115405Speterimport javax.lang.model.util.*;
39115405Speterimport static javax.lang.model.util.ElementFilter.*;
40114370Speterimport static javax.tools.Diagnostic.Kind.*;
4199127Sobrienimport static javax.tools.StandardLocation.*;
4299127Sobrienimport java.io.*;
4399127Sobrien
4499127Sobrien/**
4599127Sobrien * Test basic workings of Elements.getConstantExpression.
4699127Sobrien */
4799127Sobrien@SupportedAnnotationTypes("*")
4899127Sobrienpublic class TestGetConstantExpression extends AbstractProcessor {
4999127Sobrien    private Elements eltUtils;
5099127Sobrien    private Filer filer;
5199127Sobrien    private int round = 1;
5299127Sobrien
5399127Sobrien    /**
5499127Sobrien     * Check expected behavior on classes and packages.
5599127Sobrien     */
5699127Sobrien    public boolean process(Set<? extends TypeElement> annotations,
57                           RoundEnvironment roundEnv) {
58        int errors = 0;
59        boolean processingOver = roundEnv.processingOver();
60
61        if (!processingOver && round == 1) {
62            errors += expectIllegalArgumentException(null);
63            errors += expectIllegalArgumentException(this);
64
65            // Generate source code with various constant values and
66            // make sure it compiles.
67
68            try {
69                PrintWriter pw = new PrintWriter(filer.createSourceFile("ConstantTest").openWriter());
70                try {
71                    Boolean[]   booleans = {true, false};
72                    Byte[]      bytes    = {Byte.MIN_VALUE,    -1,  0, 1,  Byte.MAX_VALUE};
73                    Short[]     shorts   = {Short.MIN_VALUE,   -1,  0, 1,  Short.MAX_VALUE};
74                    Integer[]   ints     = {Integer.MIN_VALUE, -1,  0, 1,  Integer.MAX_VALUE};
75                    Long[]      longs    = {Long.MIN_VALUE,    -1L, 0L,1L, Long.MAX_VALUE};
76                    Character[] chars    = {Character.MIN_VALUE, ' ', '\t', 'a', 'b', 'c', '~', Character.MAX_VALUE};
77                    Float[]     floats   = {Float.NaN,  Float.NEGATIVE_INFINITY,  -1.0f, -0.0f, 0.0f, 1.0f, Float.POSITIVE_INFINITY};
78                    Double[]    doubles  = {Double.NaN, Double.NEGATIVE_INFINITY, -1.0,  -0.0,  0.0,  1.0,  Double.POSITIVE_INFINITY};
79
80                    pw.println("class ConstantTest {");
81                    pw.println(String.format("  private static boolean[] booleans = {%s};",
82                                             printConstants(booleans)));
83                    pw.println(String.format("  private static byte[] bytes = {%s};",
84                                             printConstants(bytes)));
85                    pw.println(String.format("  private static short[] shorts = {%s};",
86                                             printConstants(shorts)));
87                    pw.println(String.format("  private static int[] ints = {%s};",
88                                             printConstants(ints)));
89                    pw.println(String.format("  private static long[] longs = {%s};",
90                                             printConstants(longs)));
91                    pw.println(String.format("  private static char[] chars = {%s};",
92                                             printConstants(chars)));
93                    pw.println(String.format("  private static float[] floats = {%s};",
94                                             printConstants(floats)));
95                    pw.println(String.format("  private static double[] doubles = {%s};",
96                                             printConstants(doubles)));
97                    pw.println("}");
98                } finally {
99                    pw.close();
100                }
101            } catch(IOException io) {
102                throw new RuntimeException(io);
103            }
104            round++;
105        } else if (processingOver) {
106            if (errors > 0) {
107                throw new RuntimeException();
108            }
109        }
110        return true;
111    }
112
113    String printConstants(Object[] constants) {
114        StringBuilder sb = new StringBuilder();
115
116        for(Object o : constants) {
117            sb.append(eltUtils.getConstantExpression(o));
118            sb.append(", ");
119        }
120        return sb.toString();
121    }
122
123    int expectIllegalArgumentException(Object o) {
124        String s = "";
125        try {
126            s = eltUtils.getConstantExpression(o);
127            System.err.println("Unexpected string returned: " + s);
128            return 1;
129        } catch (IllegalArgumentException iae) {
130            return 0;
131        }
132    }
133
134    public SourceVersion getSupportedSourceVersion() {
135        return SourceVersion.latest();
136    }
137
138    public void init(ProcessingEnvironment processingEnv) {
139        super.init(processingEnv);
140        eltUtils = processingEnv.getElementUtils();
141        filer    = processingEnv.getFiler();
142    }
143}
144