test/tools/javac/T6423583.java

Wed, 14 Nov 2018 10:18:25 -0800

author
diazhou
date
Wed, 14 Nov 2018 10:18:25 -0800
changeset 3762
7909abb85562
parent 798
4868a36f6fd8
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8u201-b04 for changeset a7f48b9dfb82

     1 /*
     2  * Copyright (c) 2006, 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  */
    24 /*
    25  * @test
    26  * @bug 6423583
    27  * @summary LiteralTree.getValue() should return Boolean for Kind.BOOLEAN_LITERAL literals
    28  * @build T6423583
    29  * @compile -proc:only -processor T6423583 T6423583.java
    30  */
    32 import java.util.*;
    33 import javax.annotation.processing.*;
    34 import javax.lang.model.*;
    35 import javax.lang.model.element.*;
    36 import com.sun.source.tree.*;
    37 import com.sun.source.util.*;
    39 @SupportedAnnotationTypes("*")
    40 public class T6423583 extends AbstractProcessor {
    41     boolean b1 = true;
    42     boolean b2 = false;
    43     String s = "s";
    44     char c = 'c';
    45     int i = 0;
    46     long l = 0l;
    47     float f = 0f;
    48     double d = 0d;
    49     Void v = null;
    51     public boolean process(Set<? extends TypeElement> annos, RoundEnvironment rEnv) {
    52         Trees trees = Trees.instance(processingEnv);
    53         Test test = new Test();
    54         for (Element e: rEnv.getRootElements()) {
    55             Tree t = trees.getTree(e);
    56             test.scan(t, null);
    57         }
    58         return true;
    59     }
    61     @Override
    62     public SourceVersion getSupportedSourceVersion() {
    63         return SourceVersion.latest();
    64     }
    66     private static class Test extends TreeScanner<Void,Void> {
    68         private static Map<Tree.Kind, Class> map = new HashMap<Tree.Kind, Class>();
    69         static {
    70             map.put(Tree.Kind.BOOLEAN_LITERAL, Boolean.class);
    71             map.put(Tree.Kind.CHAR_LITERAL, Character.class);
    72             map.put(Tree.Kind.STRING_LITERAL, String.class);
    73             map.put(Tree.Kind.INT_LITERAL, Integer.class);
    74             map.put(Tree.Kind.LONG_LITERAL, Long.class);
    75             map.put(Tree.Kind.FLOAT_LITERAL, Float.class);
    76             map.put(Tree.Kind.DOUBLE_LITERAL, Double.class);
    77         }
    79         public Void visitLiteral(LiteralTree tree, Void ignore) {
    80             System.err.println(tree);
    81             Class expect = map.get(tree.getKind());
    82             if (!check(tree.getValue(), expect)) {
    83                 System.err.println("tree: " + tree);
    84                 System.err.println("expected class: " + expect);
    85                 if (tree.getValue() != null)
    86                     System.err.println("actual class: " + tree.getValue().getClass());
    87                 throw new AssertionError("unexpected value for literal");
    88             }
    89             return null;
    90         }
    92         private boolean check(Object value, Class<?> expectedClass) {
    93             return (value == null ? expectedClass == null : value.getClass().equals(expectedClass));
    94         }
    95     }
    96 }

mercurial