langtools/test/tools/javac/tree/TypeAnnotationsPretty.java
changeset 19668 6b6009d00484
parent 19651 b1aa46cc2198
child 27388 d694da45bd7a
equal deleted inserted replaced
19667:fdfce85627a9 19668:6b6009d00484
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 1234567
    26  * @bug 8023522
    27  * @summary test Pretty print of type annotations
    27  * @summary test Pretty print of type annotations
    28  * @author wmdietl
    28  * @author wmdietl
    29  */
    29  */
    30 
    30 
    31 import com.sun.source.tree.ClassTree;
    31 import com.sun.source.tree.ClassTree;
    85             for (String m : tap.matches)
    85             for (String m : tap.matches)
    86                 System.out.println(m);
    86                 System.out.println(m);
    87         }
    87         }
    88         if (!tap.mismatches.isEmpty()) {
    88         if (!tap.mismatches.isEmpty()) {
    89             for (String mm : tap.mismatches)
    89             for (String mm : tap.mismatches)
    90                 System.err.println(mm + "\n");
    90                 System.err.println(mm + NL);
    91             throw new RuntimeException("Tests failed!");
    91             throw new RuntimeException("Tests failed!");
    92         }
    92         }
    93     }
    93     }
    94 
    94 
    95     private static final String prefix =
    95     private static final String prefix =
   105             "@Target(ElementType.TYPE_USE)" +
   105             "@Target(ElementType.TYPE_USE)" +
   106             "@interface TC {}" +
   106             "@interface TC {}" +
   107             "@Target(ElementType.TYPE_USE)" +
   107             "@Target(ElementType.TYPE_USE)" +
   108             "@interface TD {}";
   108             "@interface TD {}";
   109 
   109 
       
   110     private static final String NL = System.getProperty("line.separator");
   110 
   111 
   111     private void runField(String code) throws IOException {
   112     private void runField(String code) throws IOException {
   112         String src = prefix +
   113         String src = prefix +
   113                 code + "; }" +
   114                 code + "; }" +
   114                 postfix;
   115                 postfix;
   115 
   116 
   116         JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
   117         JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
   117                 null, Arrays.asList(new MyFileObject(src)));
   118                 null, Arrays.asList(new MyFileObject(src)));
   118 
   119 
   119 
       
   120         for (CompilationUnitTree cut : ct.parse()) {
   120         for (CompilationUnitTree cut : ct.parse()) {
   121             JCTree.JCVariableDecl var =
   121             JCTree.JCVariableDecl var =
   122                     (JCTree.JCVariableDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
   122                     (JCTree.JCVariableDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
   123 
   123             checkMatch(code, var);
   124             if (!code.equals(var.toString())) {
       
   125                 mismatches.add("Expected: " + code +
       
   126                         "\nObtained: " + var.toString());
       
   127             } else {
       
   128                 matches.add("Passed: " + code);
       
   129             }
       
   130         }
   124         }
   131     }
   125     }
   132 
   126 
   133     private void runMethod(String code) throws IOException {
   127     private void runMethod(String code) throws IOException {
   134         String src = prefix +
   128         String src = prefix +
   138         JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
   132         JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
   139                 null, Arrays.asList(new MyFileObject(src)));
   133                 null, Arrays.asList(new MyFileObject(src)));
   140 
   134 
   141 
   135 
   142         for (CompilationUnitTree cut : ct.parse()) {
   136         for (CompilationUnitTree cut : ct.parse()) {
   143             JCTree.JCMethodDecl var =
   137             JCTree.JCMethodDecl meth =
   144                     (JCTree.JCMethodDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
   138                     (JCTree.JCMethodDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
       
   139             checkMatch(code, meth);
       
   140         }
       
   141     }
   145 
   142 
   146             if (!code.equals(var.toString())) {
   143     void checkMatch(String code, JCTree tree) {
   147                 mismatches.add("Expected: " + code +
   144         String expect = code.replace("\n", NL);
   148                         "\nObtained: " + var.toString());
   145         String found = tree.toString();
   149             } else {
   146         if (!expect.equals(found)) {
   150                 matches.add("Passed: " + code);
   147             mismatches.add("Expected: " + expect + NL +
   151             }
   148                     "Obtained: " + found);
       
   149         } else {
       
   150             matches.add("Passed: " + expect);
   152         }
   151         }
   153     }
   152     }
   154 }
   153 }
   155 
   154 
   156 
   155