test/tools/javac/processing/model/util/elements/TestGetConstantExpression.java

Print this page


   1 /*
   2  * Copyright (c) 2009, 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  * @build TestGetConstantExpression

  30  * @compile -processor TestGetConstantExpression Foo.java
  31  */
  32 
  33 import java.util.Set;
  34 import javax.annotation.processing.*;
  35 import javax.lang.model.SourceVersion;
  36 import static javax.lang.model.SourceVersion.*;
  37 import javax.lang.model.element.*;
  38 import javax.lang.model.util.*;
  39 import static javax.lang.model.util.ElementFilter.*;
  40 import static javax.tools.Diagnostic.Kind.*;
  41 import static javax.tools.StandardLocation.*;
  42 import java.io.*;
  43 
  44 /**
  45  * Test basic workings of Elements.getConstantExpression.
  46  */
  47 @SupportedAnnotationTypes("*")
  48 public class TestGetConstantExpression extends AbstractProcessor {
  49     private Elements eltUtils;
  50     private Filer filer;
  51     private int round = 1;
  52 
  53     /**
  54      * Check expected behavior on classes and packages.
  55      */
  56     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 {


 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 }
   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 TestGetConstantExpression
  31  * @compile -processor TestGetConstantExpression Foo.java
  32  */
  33 
  34 import java.util.Set;
  35 import javax.annotation.processing.*;
  36 import javax.lang.model.SourceVersion;
  37 import static javax.lang.model.SourceVersion.*;
  38 import javax.lang.model.element.*;
  39 import javax.lang.model.util.*;
  40 import static javax.lang.model.util.ElementFilter.*;
  41 import static javax.tools.Diagnostic.Kind.*;
  42 import static javax.tools.StandardLocation.*;
  43 import java.io.*;
  44 
  45 /**
  46  * Test basic workings of Elements.getConstantExpression.
  47  */
  48 public class TestGetConstantExpression extends JavacTestingAbstractProcessor {



  49     private int round = 1;
  50 
  51     /**
  52      * Check expected behavior on classes and packages.
  53      */
  54     public boolean process(Set<? extends TypeElement> annotations,
  55                            RoundEnvironment roundEnv) {
  56         int errors = 0;
  57         boolean processingOver = roundEnv.processingOver();
  58 
  59         if (!processingOver && round == 1) {
  60             errors += expectIllegalArgumentException(null);
  61             errors += expectIllegalArgumentException(this);
  62 
  63             // Generate source code with various constant values and
  64             // make sure it compiles.
  65 
  66             try {
  67                 PrintWriter pw = new PrintWriter(filer.createSourceFile("ConstantTest").openWriter());
  68                 try {


 111     String printConstants(Object[] constants) {
 112         StringBuilder sb = new StringBuilder();
 113 
 114         for(Object o : constants) {
 115             sb.append(eltUtils.getConstantExpression(o));
 116             sb.append(", ");
 117         }
 118         return sb.toString();
 119     }
 120 
 121     int expectIllegalArgumentException(Object o) {
 122         String s = "";
 123         try {
 124             s = eltUtils.getConstantExpression(o);
 125             System.err.println("Unexpected string returned: " + s);
 126             return 1;
 127         } catch (IllegalArgumentException iae) {
 128             return 0;
 129         }
 130     }










 131 }