test/tools/javac/processing/model/type/TestUnionType.java

Print this page




   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     7029150
  27  * @summary Test support for union types
  28  * @library ../../../lib
  29  */
  30 
  31 import java.net.URI;
  32 import java.util.*;
  33 import javax.annotation.processing.*;
  34 import javax.lang.model.element.*;
  35 import javax.lang.model.type.*;
  36 import javax.lang.model.util.*;
  37 import javax.tools.*;
  38 
  39 import com.sun.source.tree.*;
  40 import com.sun.source.util.*;
  41 
  42 
  43 public class TestUnionType extends JavacTestingAbstractProcessor {
  44     enum TestKind {
  45         SingleType("E1", "E1",
  46                 "VariableTree: E1 e",
  47                 "VariableTree: elem EXCEPTION_PARAMETER e",
  48                 "VariableTree: elem.type DECLARED",
  49                 "VariableTree: elem.type.elem CLASS E1",
  50                 "VariableTree: type DECLARED",
  51                 "VariableTree: type.elem CLASS E1",
  52                 "VariableTree: type.elem.type DECLARED"),
  53 
  54         ValidTypes("E1, E2", "E1 | E2",
  55                 "VariableTree: E1 | E2 e",
  56                 "VariableTree: elem EXCEPTION_PARAMETER e",
  57                 "VariableTree: elem.type UNION Test.E1,Test.E2",
  58                 "VariableTree: elem.type.elem null",
  59                 "VariableTree: type UNION Test.E1,Test.E2",
  60                 "VariableTree: type.elem null"),
  61 
  62         InvalidTypes("E1, E2", "E1 | EMissing",


 177                 if (types.asElement(tm) != null)
 178                     log("VariableTree: type.elem.type " + print(types.asElement(tm).asType()));
 179             }
 180             return super.visitVariable(tree, null);
 181         }
 182 
 183         String print(TypeMirror tm) {
 184             return (tm == null) ? null : new TypePrinter().visit(tm);
 185         }
 186 
 187         String print(Element e) {
 188             return (e == null) ? null : (e.getKind() + " " + e.getSimpleName());
 189         }
 190 
 191         void log(String msg) {
 192             System.err.println(msg);
 193             log.add(msg);
 194         }
 195     }
 196 
 197     class TypePrinter extends SimpleTypeVisitor7<String, Void> {
 198         @Override
 199         protected String defaultAction(TypeMirror tm, Void ignore) {
 200             return String.valueOf(tm.getKind());
 201         }
 202 
 203         @Override
 204         public String visitUnion(UnionType t, Void ignore) {
 205             return (t.getKind() + " " + t.getAlternatives());
 206         }
 207     }
 208 }


   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     7029150 7025809
  27  * @summary Test support for union types
  28  * @library ../../../lib
  29  */
  30 
  31 import java.net.URI;
  32 import java.util.*;
  33 import javax.annotation.processing.*;
  34 import javax.lang.model.element.*;
  35 import javax.lang.model.type.*;
  36 import javax.lang.model.util.*;
  37 import javax.tools.*;
  38 
  39 import com.sun.source.tree.*;
  40 import com.sun.source.util.*;
  41 

  42 public class TestUnionType extends JavacTestingAbstractProcessor {
  43     enum TestKind {
  44         SingleType("E1", "E1",
  45                 "VariableTree: E1 e",
  46                 "VariableTree: elem EXCEPTION_PARAMETER e",
  47                 "VariableTree: elem.type DECLARED",
  48                 "VariableTree: elem.type.elem CLASS E1",
  49                 "VariableTree: type DECLARED",
  50                 "VariableTree: type.elem CLASS E1",
  51                 "VariableTree: type.elem.type DECLARED"),
  52 
  53         ValidTypes("E1, E2", "E1 | E2",
  54                 "VariableTree: E1 | E2 e",
  55                 "VariableTree: elem EXCEPTION_PARAMETER e",
  56                 "VariableTree: elem.type UNION Test.E1,Test.E2",
  57                 "VariableTree: elem.type.elem null",
  58                 "VariableTree: type UNION Test.E1,Test.E2",
  59                 "VariableTree: type.elem null"),
  60 
  61         InvalidTypes("E1, E2", "E1 | EMissing",


 176                 if (types.asElement(tm) != null)
 177                     log("VariableTree: type.elem.type " + print(types.asElement(tm).asType()));
 178             }
 179             return super.visitVariable(tree, null);
 180         }
 181 
 182         String print(TypeMirror tm) {
 183             return (tm == null) ? null : new TypePrinter().visit(tm);
 184         }
 185 
 186         String print(Element e) {
 187             return (e == null) ? null : (e.getKind() + " " + e.getSimpleName());
 188         }
 189 
 190         void log(String msg) {
 191             System.err.println(msg);
 192             log.add(msg);
 193         }
 194     }
 195 
 196     class TypePrinter extends SimpleTypeVisitor<String, Void> {
 197         @Override
 198         protected String defaultAction(TypeMirror tm, Void ignore) {
 199             return String.valueOf(tm.getKind());
 200         }
 201 
 202         @Override
 203         public String visitUnion(UnionType t, Void ignore) {
 204             return (t.getKind() + " " + t.getAlternatives());
 205         }
 206     }
 207 }