< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTrees.java

Print this page
rev 48842 : imported patch 8187950-ext


  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.javac.api;
  27 
  28 import java.io.FileNotFoundException;
  29 import java.io.IOException;
  30 import java.text.BreakIterator;
  31 import java.util.Collections;
  32 import java.util.HashSet;


  33 import java.util.Set;
  34 import java.util.regex.Matcher;
  35 import java.util.regex.Pattern;
  36 
  37 import javax.annotation.processing.ProcessingEnvironment;
  38 import javax.lang.model.element.AnnotationMirror;
  39 import javax.lang.model.element.AnnotationValue;
  40 import javax.lang.model.element.Element;
  41 import javax.lang.model.element.ElementKind;
  42 import javax.lang.model.element.ExecutableElement;
  43 import javax.lang.model.element.Modifier;
  44 import javax.lang.model.element.NestingKind;
  45 import javax.lang.model.element.PackageElement;
  46 import javax.lang.model.element.TypeElement;
  47 import javax.lang.model.type.DeclaredType;
  48 import javax.lang.model.type.TypeKind;
  49 import javax.lang.model.type.TypeMirror;
  50 import javax.tools.Diagnostic;
  51 import javax.tools.FileObject;
  52 import javax.tools.ForwardingFileObject;


 163 public class JavacTrees extends DocTrees {
 164 
 165     // in a world of a single context per compilation, these would all be final
 166     private Modules modules;
 167     private Resolve resolve;
 168     private Enter enter;
 169     private Log log;
 170     private MemberEnter memberEnter;
 171     private Attr attr;
 172     private TreeMaker treeMaker;
 173     private JavacElements elements;
 174     private JavacTaskImpl javacTaskImpl;
 175     private Names names;
 176     private Types types;
 177     private DocTreeMaker docTreeMaker;
 178     private BreakIterator breakIterator;
 179     private JavaFileManager fileManager;
 180     private ParserFactory parser;
 181     private Symtab syms;
 182 


 183     // called reflectively from Trees.instance(CompilationTask task)
 184     public static JavacTrees instance(JavaCompiler.CompilationTask task) {
 185         if (!(task instanceof BasicJavacTask))
 186             throw new IllegalArgumentException();
 187         return instance(((BasicJavacTask)task).getContext());
 188     }
 189 
 190     // called reflectively from Trees.instance(ProcessingEnvironment env)
 191     public static JavacTrees instance(ProcessingEnvironment env) {
 192         if (!(env instanceof JavacProcessingEnvironment))
 193             throw new IllegalArgumentException();
 194         return instance(((JavacProcessingEnvironment)env).getContext());
 195     }
 196 
 197     public static JavacTrees instance(Context context) {
 198         JavacTrees instance = context.get(JavacTrees.class);
 199         if (instance == null)
 200             instance = new JavacTrees(context);
 201         return instance;
 202     }


1065             if (t == leaf)
1066                 leafCopy = t2;
1067             return t2;
1068         }
1069     }
1070 
1071     protected Copier createCopier(TreeMaker maker) {
1072         return new Copier(maker);
1073     }
1074 
1075     /**
1076      * Returns the original type from the ErrorType object.
1077      * @param errorType The errorType for which we want to get the original type.
1078      * @return TypeMirror corresponding to the original type, replaced by the ErrorType.
1079      *         noType (type.tag == NONE) is returned if there is no original type.
1080      */
1081     @Override @DefinedBy(Api.COMPILER_TREE)
1082     public TypeMirror getOriginalType(javax.lang.model.type.ErrorType errorType) {
1083         if (errorType instanceof com.sun.tools.javac.code.Type.ErrorType) {
1084             return ((com.sun.tools.javac.code.Type.ErrorType)errorType).getOriginalType();












1085         }
1086 
1087         return com.sun.tools.javac.code.Type.noType;
1088     }
1089 
1090     /**
1091      * Prints a message of the specified kind at the location of the
1092      * tree within the provided compilation unit
1093      *
1094      * @param kind the kind of message
1095      * @param msg  the message, or an empty string if none
1096      * @param t    the tree to use as a position hint
1097      * @param root the compilation unit that contains tree
1098      */
1099     @Override @DefinedBy(Api.COMPILER_TREE)
1100     public void printMessage(Diagnostic.Kind kind, CharSequence msg,
1101             com.sun.source.tree.Tree t,
1102             com.sun.source.tree.CompilationUnitTree root) {
1103         printMessage(kind, msg, ((JCTree) t).pos(), root);
1104     }




  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.javac.api;
  27 
  28 import java.io.FileNotFoundException;
  29 import java.io.IOException;
  30 import java.text.BreakIterator;
  31 import java.util.Collections;
  32 import java.util.HashSet;
  33 import java.util.IdentityHashMap;
  34 import java.util.Map;
  35 import java.util.Set;
  36 import java.util.regex.Matcher;
  37 import java.util.regex.Pattern;
  38 
  39 import javax.annotation.processing.ProcessingEnvironment;
  40 import javax.lang.model.element.AnnotationMirror;
  41 import javax.lang.model.element.AnnotationValue;
  42 import javax.lang.model.element.Element;
  43 import javax.lang.model.element.ElementKind;
  44 import javax.lang.model.element.ExecutableElement;
  45 import javax.lang.model.element.Modifier;
  46 import javax.lang.model.element.NestingKind;
  47 import javax.lang.model.element.PackageElement;
  48 import javax.lang.model.element.TypeElement;
  49 import javax.lang.model.type.DeclaredType;
  50 import javax.lang.model.type.TypeKind;
  51 import javax.lang.model.type.TypeMirror;
  52 import javax.tools.Diagnostic;
  53 import javax.tools.FileObject;
  54 import javax.tools.ForwardingFileObject;


 165 public class JavacTrees extends DocTrees {
 166 
 167     // in a world of a single context per compilation, these would all be final
 168     private Modules modules;
 169     private Resolve resolve;
 170     private Enter enter;
 171     private Log log;
 172     private MemberEnter memberEnter;
 173     private Attr attr;
 174     private TreeMaker treeMaker;
 175     private JavacElements elements;
 176     private JavacTaskImpl javacTaskImpl;
 177     private Names names;
 178     private Types types;
 179     private DocTreeMaker docTreeMaker;
 180     private BreakIterator breakIterator;
 181     private JavaFileManager fileManager;
 182     private ParserFactory parser;
 183     private Symtab syms;
 184 
 185     private final Map<Type, Type> extraType2OriginalMap = new IdentityHashMap<>();
 186 
 187     // called reflectively from Trees.instance(CompilationTask task)
 188     public static JavacTrees instance(JavaCompiler.CompilationTask task) {
 189         if (!(task instanceof BasicJavacTask))
 190             throw new IllegalArgumentException();
 191         return instance(((BasicJavacTask)task).getContext());
 192     }
 193 
 194     // called reflectively from Trees.instance(ProcessingEnvironment env)
 195     public static JavacTrees instance(ProcessingEnvironment env) {
 196         if (!(env instanceof JavacProcessingEnvironment))
 197             throw new IllegalArgumentException();
 198         return instance(((JavacProcessingEnvironment)env).getContext());
 199     }
 200 
 201     public static JavacTrees instance(Context context) {
 202         JavacTrees instance = context.get(JavacTrees.class);
 203         if (instance == null)
 204             instance = new JavacTrees(context);
 205         return instance;
 206     }


1069             if (t == leaf)
1070                 leafCopy = t2;
1071             return t2;
1072         }
1073     }
1074 
1075     protected Copier createCopier(TreeMaker maker) {
1076         return new Copier(maker);
1077     }
1078 
1079     /**
1080      * Returns the original type from the ErrorType object.
1081      * @param errorType The errorType for which we want to get the original type.
1082      * @return TypeMirror corresponding to the original type, replaced by the ErrorType.
1083      *         noType (type.tag == NONE) is returned if there is no original type.
1084      */
1085     @Override @DefinedBy(Api.COMPILER_TREE)
1086     public TypeMirror getOriginalType(javax.lang.model.type.ErrorType errorType) {
1087         if (errorType instanceof com.sun.tools.javac.code.Type.ErrorType) {
1088             return ((com.sun.tools.javac.code.Type.ErrorType)errorType).getOriginalType();
1089         }
1090         if (errorType instanceof com.sun.tools.javac.code.Type.ClassType &&
1091             errorType.getKind() == TypeKind.ERROR) {
1092             ClassType ct = (ClassType) errorType;
1093             return extraType2OriginalMap.computeIfAbsent(ct, tt -> new ClassType(ct.getEnclosingType(), ct.typarams_field, ct.tsym, ct.getMetadata()) {
1094                 @Override
1095                 public Type baseType() { return ct; }
1096                 @Override
1097                 public TypeKind getKind() {
1098                     return TypeKind.DECLARED;
1099                 }
1100             });
1101         }
1102 
1103         return com.sun.tools.javac.code.Type.noType;
1104     }
1105 
1106     /**
1107      * Prints a message of the specified kind at the location of the
1108      * tree within the provided compilation unit
1109      *
1110      * @param kind the kind of message
1111      * @param msg  the message, or an empty string if none
1112      * @param t    the tree to use as a position hint
1113      * @param root the compilation unit that contains tree
1114      */
1115     @Override @DefinedBy(Api.COMPILER_TREE)
1116     public void printMessage(Diagnostic.Kind kind, CharSequence msg,
1117             com.sun.source.tree.Tree t,
1118             com.sun.source.tree.CompilationUnitTree root) {
1119         printMessage(kind, msg, ((JCTree) t).pos(), root);
1120     }


< prev index next >