langtools/test/tools/javac/processing/model/trees/OnDemandAttribution.java
changeset 41160 61082a97bbf0
parent 30730 d3ce7619db2c
equal deleted inserted replaced
41159:06ac98da3f82 41160:61082a97bbf0
     1 /*
     1 /*
     2  * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 8038455
    26  * @bug 8038455 8047347
    27  * @summary Verify that in-method ClassSymbols from one round do not affect ClassSymbols in
    27  * @summary Verify that in-method ClassSymbols from one round do not affect ClassSymbols in
    28  *          following rounds.
    28  *          following rounds.
    29  * @library /tools/javac/lib
    29  * @library /tools/javac/lib
    30  * @modules jdk.compiler
    30  * @modules jdk.compiler
    31  * @build JavacTestingAbstractProcessor OnDemandAttribution
    31  * @build JavacTestingAbstractProcessor OnDemandAttribution
    32  * @compile/process -processor OnDemandAttribution OnDemandAttribution.java
    32  * @clean Gen
       
    33  * @compile/process -processor OnDemandAttribution OnDemandAttributionData.java
    33  */
    34  */
    34 
    35 
       
    36 import java.io.IOException;
       
    37 import java.io.Writer;
    35 import java.util.*;
    38 import java.util.*;
    36 import javax.annotation.processing.*;
    39 import javax.annotation.processing.*;
    37 import javax.lang.model.element.*;
    40 import javax.lang.model.element.*;
    38 import static javax.lang.model.util.ElementFilter.*;
    41 import static javax.lang.model.util.ElementFilter.constructorsIn;
    39 import com.sun.source.tree.*;
    42 import com.sun.source.tree.*;
    40 import com.sun.source.util.*;
    43 import com.sun.source.util.*;
    41 
    44 
    42 public class OnDemandAttribution extends JavacTestingAbstractProcessor {
    45 public class OnDemandAttribution extends JavacTestingAbstractProcessor {
    43 
    46 
    44     public OnDemandAttribution() {
    47     public OnDemandAttribution() {
    45         class Local { }
       
    46         new Object() { };
       
    47     }
    48     }
    48 
    49 
       
    50     int round;
       
    51     Set<String> roots = new HashSet<>();
       
    52 
    49     public boolean process(Set<? extends TypeElement> annos,RoundEnvironment rEnv) {
    53     public boolean process(Set<? extends TypeElement> annos,RoundEnvironment rEnv) {
    50         TypeElement currentClass = elements.getTypeElement("OnDemandAttribution");
    54         for (Element root : rEnv.getRootElements()) {
    51         ExecutableElement constr = constructorsIn(currentClass.getEnclosedElements()).get(0);
    55             while (root.getEnclosingElement().getKind() != ElementKind.PACKAGE) {
    52         Trees trees = Trees.instance(processingEnv);
    56                 root = root.getEnclosingElement();
    53         TreePath path = trees.getPath(constr);
    57             }
       
    58             roots.add(((TypeElement) root).getQualifiedName().toString());
       
    59         }
       
    60         for (String root : roots) {
       
    61             TypeElement currentClass = elements.getTypeElement(root);
       
    62             ExecutableElement constr = constructorsIn(currentClass.getEnclosedElements()).get(0);
       
    63             Trees trees = Trees.instance(processingEnv);
       
    64             TreePath path = trees.getPath(constr);
    54 
    65 
    55         new TreePathScanner<Void, Void>() {
    66             new TreePathScanner<Void, Void>() {
    56             @Override public Void visitClass(ClassTree node, Void p) {
    67                 @Override public Void visitClass(ClassTree node, Void p) {
    57                 if (node.getSimpleName().contentEquals("Local")) {
    68                     if (node.getSimpleName().contentEquals("Local")) {
    58                      //will also attribute the body on demand:
    69                          //will also attribute the body on demand:
       
    70                         Element el = trees.getElement(getCurrentPath());
       
    71                         Name binaryName = elements.getBinaryName((TypeElement) el);
       
    72                         if (!binaryName.contentEquals("OnDemandAttributionData$1Local")) {
       
    73                             throw new IllegalStateException("Incorrect binary name=" + binaryName);
       
    74                         }
       
    75                     }
       
    76                     return super.visitClass(node, p);
       
    77                 }
       
    78                 @Override public Void visitNewClass(NewClassTree node, Void p) {
    59                     Element el = trees.getElement(getCurrentPath());
    79                     Element el = trees.getElement(getCurrentPath());
    60                     Name binaryName = elements.getBinaryName((TypeElement) el);
    80                     Name binaryName = elements.getBinaryName((TypeElement) el.getEnclosingElement());
    61                     if (!binaryName.contentEquals("OnDemandAttribution$1Local")) {
    81                     if (!binaryName.contentEquals("OnDemandAttributionData$1")) {
    62                         throw new IllegalStateException("Incorrect binary name=" + binaryName);
    82                         throw new IllegalStateException("Incorrect binary name=" + binaryName);
    63                     }
    83                     }
       
    84                     return super.visitNewClass(node, p);
    64                 }
    85                 }
    65                 return super.visitClass(node, p);
    86                 @Override
       
    87                 public Void visitMethodInvocation(MethodInvocationTree node, Void p) {
       
    88                     if (trees.getElement(getCurrentPath()) == null)
       
    89                         throw new IllegalStateException("No element for: " + node);
       
    90                     return super.visitMethodInvocation(node, p);
       
    91                 }
       
    92                 @Override
       
    93                 public Void visitIdentifier(IdentifierTree node, Void p) {
       
    94                     if (trees.getElement(getCurrentPath()) == null)
       
    95                         throw new IllegalStateException("No element for: " + node);
       
    96                     return super.visitIdentifier(node, p);
       
    97                 }
       
    98             }.scan(path, null);
       
    99         }
       
   100 
       
   101         if (round++ == 0) {
       
   102             try (Writer out = filer.createSourceFile("Gen").openWriter()) {
       
   103                 out.write("class Gen { public static long C = 1; }");
       
   104             } catch (IOException ex) {
       
   105                 throw new IllegalStateException(ex);
    66             }
   106             }
    67             @Override public Void visitNewClass(NewClassTree node, Void p) {
   107         }
    68                 Element el = trees.getElement(getCurrentPath());
       
    69                 Name binaryName = elements.getBinaryName((TypeElement) el.getEnclosingElement());
       
    70                 if (!binaryName.contentEquals("OnDemandAttribution$1")) {
       
    71                     throw new IllegalStateException("Incorrect binary name=" + binaryName);
       
    72                 }
       
    73                 return super.visitNewClass(node, p);
       
    74             }
       
    75         }.scan(path, null);
       
    76 
   108 
    77         return true;
   109         return true;
    78     }
   110     }
    79 }
   111 }