< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/model/JavacElements.java

Print this page




  28 import java.util.Collections;
  29 import java.util.HashMap;
  30 import java.util.HashSet;
  31 import java.util.LinkedHashSet;
  32 import java.util.Map;
  33 import java.util.Optional;
  34 import java.util.Set;
  35 import java.util.stream.Collectors;
  36 
  37 import javax.lang.model.AnnotatedConstruct;
  38 import javax.lang.model.SourceVersion;
  39 import javax.lang.model.element.*;
  40 import javax.lang.model.type.DeclaredType;
  41 import javax.lang.model.util.Elements;
  42 import javax.tools.JavaFileObject;
  43 import static javax.lang.model.util.ElementFilter.methodsIn;
  44 
  45 import com.sun.source.util.JavacTask;
  46 import com.sun.tools.javac.api.JavacTaskImpl;
  47 import com.sun.tools.javac.code.*;

  48 import com.sun.tools.javac.code.Attribute.Compound;
  49 import com.sun.tools.javac.code.Directive.ExportsDirective;
  50 import com.sun.tools.javac.code.Directive.ExportsFlag;
  51 import com.sun.tools.javac.code.Directive.OpensDirective;
  52 import com.sun.tools.javac.code.Directive.OpensFlag;
  53 import com.sun.tools.javac.code.Directive.RequiresDirective;
  54 import com.sun.tools.javac.code.Directive.RequiresFlag;
  55 import com.sun.tools.javac.code.Scope.WriteableScope;
  56 import com.sun.tools.javac.code.Source.Feature;
  57 import com.sun.tools.javac.code.Symbol.*;
  58 import com.sun.tools.javac.comp.AttrContext;
  59 import com.sun.tools.javac.comp.Enter;
  60 import com.sun.tools.javac.comp.Env;
  61 import com.sun.tools.javac.main.JavaCompiler;
  62 import com.sun.tools.javac.processing.PrintingProcessor;
  63 import com.sun.tools.javac.tree.JCTree;
  64 import com.sun.tools.javac.tree.JCTree.*;
  65 import com.sun.tools.javac.tree.TreeInfo;
  66 import com.sun.tools.javac.tree.TreeScanner;
  67 import com.sun.tools.javac.util.*;


 445     @DefinedBy(Api.LANGUAGE_MODEL)
 446     public ModuleElement getModuleOf(Element e) {
 447         Symbol sym = cast(Symbol.class, e);
 448         if (modules.getDefaultModule() == syms.noModule)
 449             return null;
 450         return (sym.kind == MDL) ? ((ModuleElement) e) : sym.packge().modle;
 451     }
 452 
 453     @DefinedBy(Api.LANGUAGE_MODEL)
 454     public boolean isDeprecated(Element e) {
 455         Symbol sym = cast(Symbol.class, e);
 456         sym.apiComplete();
 457         return sym.isDeprecated();
 458     }
 459 
 460     @Override @DefinedBy(Api.LANGUAGE_MODEL)
 461     public Origin getOrigin(Element e) {
 462         Symbol sym = cast(Symbol.class, e);
 463         if ((sym.flags() & Flags.GENERATEDCONSTR) != 0)
 464             return Origin.MANDATED;


 465         //TypeElement.getEnclosedElements does not return synthetic elements,
 466         //and most synthetic elements are not read from the classfile anyway:
 467         return Origin.EXPLICIT;
 468     }
 469 
 470     @Override @DefinedBy(Api.LANGUAGE_MODEL)
 471     public Origin getOrigin(AnnotatedConstruct c, AnnotationMirror a) {
 472         Compound ac = cast(Compound.class, a);
 473         if (ac.isSynthesized())
 474             return Origin.MANDATED;
 475         return Origin.EXPLICIT;
 476     }
 477 
 478     @Override @DefinedBy(Api.LANGUAGE_MODEL)
 479     public Origin getOrigin(ModuleElement m, ModuleElement.Directive directive) {
 480         switch (directive.getKind()) {
 481             case REQUIRES:
 482                 RequiresDirective rd = cast(RequiresDirective.class, directive);
 483                 if (rd.flags.contains(RequiresFlag.MANDATED))
 484                     return Origin.MANDATED;


 782             javacTaskImpl.ensureEntered();
 783         }
 784         if (!javaCompiler.isEnterDone()) {
 785             throw new IllegalStateException("Cannot use Elements." + methodName + " before the TaskEvent.Kind.ENTER finished event.");
 786         }
 787     }
 788 
 789     /**
 790      * Returns an object cast to the specified type.
 791      * @throws NullPointerException if the object is {@code null}
 792      * @throws IllegalArgumentException if the object is of the wrong type
 793      */
 794     private static <T> T cast(Class<T> clazz, Object o) {
 795         if (! clazz.isInstance(o))
 796             throw new IllegalArgumentException(o.toString());
 797         return clazz.cast(o);
 798     }
 799 
 800     public void newRound() {
 801         resultCache.clear();



















 802     }
 803 }


  28 import java.util.Collections;
  29 import java.util.HashMap;
  30 import java.util.HashSet;
  31 import java.util.LinkedHashSet;
  32 import java.util.Map;
  33 import java.util.Optional;
  34 import java.util.Set;
  35 import java.util.stream.Collectors;
  36 
  37 import javax.lang.model.AnnotatedConstruct;
  38 import javax.lang.model.SourceVersion;
  39 import javax.lang.model.element.*;
  40 import javax.lang.model.type.DeclaredType;
  41 import javax.lang.model.util.Elements;
  42 import javax.tools.JavaFileObject;
  43 import static javax.lang.model.util.ElementFilter.methodsIn;
  44 
  45 import com.sun.source.util.JavacTask;
  46 import com.sun.tools.javac.api.JavacTaskImpl;
  47 import com.sun.tools.javac.code.*;
  48 import com.sun.tools.javac.code.Accessors.Kind;
  49 import com.sun.tools.javac.code.Attribute.Compound;
  50 import com.sun.tools.javac.code.Directive.ExportsDirective;
  51 import com.sun.tools.javac.code.Directive.ExportsFlag;
  52 import com.sun.tools.javac.code.Directive.OpensDirective;
  53 import com.sun.tools.javac.code.Directive.OpensFlag;
  54 import com.sun.tools.javac.code.Directive.RequiresDirective;
  55 import com.sun.tools.javac.code.Directive.RequiresFlag;
  56 import com.sun.tools.javac.code.Scope.WriteableScope;
  57 import com.sun.tools.javac.code.Source.Feature;
  58 import com.sun.tools.javac.code.Symbol.*;
  59 import com.sun.tools.javac.comp.AttrContext;
  60 import com.sun.tools.javac.comp.Enter;
  61 import com.sun.tools.javac.comp.Env;
  62 import com.sun.tools.javac.main.JavaCompiler;
  63 import com.sun.tools.javac.processing.PrintingProcessor;
  64 import com.sun.tools.javac.tree.JCTree;
  65 import com.sun.tools.javac.tree.JCTree.*;
  66 import com.sun.tools.javac.tree.TreeInfo;
  67 import com.sun.tools.javac.tree.TreeScanner;
  68 import com.sun.tools.javac.util.*;


 446     @DefinedBy(Api.LANGUAGE_MODEL)
 447     public ModuleElement getModuleOf(Element e) {
 448         Symbol sym = cast(Symbol.class, e);
 449         if (modules.getDefaultModule() == syms.noModule)
 450             return null;
 451         return (sym.kind == MDL) ? ((ModuleElement) e) : sym.packge().modle;
 452     }
 453 
 454     @DefinedBy(Api.LANGUAGE_MODEL)
 455     public boolean isDeprecated(Element e) {
 456         Symbol sym = cast(Symbol.class, e);
 457         sym.apiComplete();
 458         return sym.isDeprecated();
 459     }
 460 
 461     @Override @DefinedBy(Api.LANGUAGE_MODEL)
 462     public Origin getOrigin(Element e) {
 463         Symbol sym = cast(Symbol.class, e);
 464         if ((sym.flags() & Flags.GENERATEDCONSTR) != 0)
 465             return Origin.MANDATED;
 466         if ((sym.flags() & (Flags.RECORD | Flags.MANDATED)) != 0)
 467             return Origin.MANDATED;
 468         //TypeElement.getEnclosedElements does not return synthetic elements,
 469         //and most synthetic elements are not read from the classfile anyway:
 470         return Origin.EXPLICIT;
 471     }
 472 
 473     @Override @DefinedBy(Api.LANGUAGE_MODEL)
 474     public Origin getOrigin(AnnotatedConstruct c, AnnotationMirror a) {
 475         Compound ac = cast(Compound.class, a);
 476         if (ac.isSynthesized())
 477             return Origin.MANDATED;
 478         return Origin.EXPLICIT;
 479     }
 480 
 481     @Override @DefinedBy(Api.LANGUAGE_MODEL)
 482     public Origin getOrigin(ModuleElement m, ModuleElement.Directive directive) {
 483         switch (directive.getKind()) {
 484             case REQUIRES:
 485                 RequiresDirective rd = cast(RequiresDirective.class, directive);
 486                 if (rd.flags.contains(RequiresFlag.MANDATED))
 487                     return Origin.MANDATED;


 785             javacTaskImpl.ensureEntered();
 786         }
 787         if (!javaCompiler.isEnterDone()) {
 788             throw new IllegalStateException("Cannot use Elements." + methodName + " before the TaskEvent.Kind.ENTER finished event.");
 789         }
 790     }
 791 
 792     /**
 793      * Returns an object cast to the specified type.
 794      * @throws NullPointerException if the object is {@code null}
 795      * @throws IllegalArgumentException if the object is of the wrong type
 796      */
 797     private static <T> T cast(Class<T> clazz, Object o) {
 798         if (! clazz.isInstance(o))
 799             throw new IllegalArgumentException(o.toString());
 800         return clazz.cast(o);
 801     }
 802 
 803     public void newRound() {
 804         resultCache.clear();
 805     }
 806 
 807     @Override
 808     public ExecutableElement getterFor(VariableElement variableElement) {
 809         return accessorFor(Kind.GET, variableElement);
 810     }
 811 
 812     @Override
 813     public ExecutableElement setterFor(VariableElement variableElement) {
 814         return accessorFor(Kind.SET, variableElement);
 815     }
 816 
 817     private ExecutableElement accessorFor(Accessors.Kind kind, VariableElement variableElement) {
 818         for (Pair<Accessors.Kind, MethodSymbol> accessor : ((VarSymbol)variableElement).accessors) {
 819             if (accessor.fst == kind) {
 820                 return accessor.snd;
 821             }
 822         }
 823         return null;
 824     }
 825 }
< prev index next >