30 import java.util.Collections;
31 import java.util.EnumMap;
32 import java.util.EnumSet;
33 import java.util.HashMap;
34 import java.util.HashSet;
35 import java.util.LinkedHashMap;
36 import java.util.LinkedHashSet;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Set;
40
41 import javax.lang.model.element.Element;
42 import javax.lang.model.element.ElementKind;
43 import javax.lang.model.element.Modifier;
44 import javax.lang.model.element.ModuleElement;
45 import javax.lang.model.element.ModuleElement.ExportsDirective;
46 import javax.lang.model.element.ModuleElement.RequiresDirective;
47 import javax.lang.model.element.PackageElement;
48 import javax.lang.model.element.TypeElement;
49 import javax.lang.model.util.ElementFilter;
50 import javax.lang.model.util.SimpleElementVisitor9;
51 import javax.tools.JavaFileManager;
52 import javax.tools.JavaFileManager.Location;
53 import javax.tools.JavaFileObject;
54 import javax.tools.StandardLocation;
55
56 import com.sun.tools.javac.code.Kinds.Kind;
57 import com.sun.tools.javac.code.Source;
58 import com.sun.tools.javac.code.Source.Feature;
59 import com.sun.tools.javac.code.Symbol;
60 import com.sun.tools.javac.code.Symbol.ClassSymbol;
61 import com.sun.tools.javac.code.Symbol.CompletionFailure;
62 import com.sun.tools.javac.code.Symbol.ModuleSymbol;
63 import com.sun.tools.javac.code.Symbol.PackageSymbol;
64 import com.sun.tools.javac.code.Symtab;
65 import com.sun.tools.javac.comp.Modules;
66 import com.sun.tools.javac.main.JavaCompiler;
67 import com.sun.tools.javac.tree.JCTree.JCClassDecl;
68 import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
69 import com.sun.tools.javac.tree.JCTree.JCModuleDecl;
70 import com.sun.tools.javac.tree.TreeInfo;
968 else
969 messager.printWarningUsingKey("main.unexpected.exception", e);
970 }
971 }
972
973 /**
974 * Returns a list of all classes contained in this package, including
975 * member classes of those classes, and their member classes, etc.
976 */
977 private void addAllClasses(Collection<TypeElement> list, PackageElement pkg) {
978 boolean filtered = true;
979 for (Element isym : pkg.getEnclosedElements()) {
980 addAllClasses(list, (TypeElement)isym, filtered);
981 }
982 }
983
984 private boolean isTypeElementSelected(TypeElement te) {
985 return (xclasses || toolEnv.getFileKind(te) == SOURCE) && isSelected(te);
986 }
987
988 SimpleElementVisitor9<Boolean, Void> visibleElementVisitor = null;
989 /**
990 * Returns true if the element is selected, by applying
991 * the access filter checks. Special treatment is applied to
992 * types, for a top level type the access filter applies completely,
993 * however if is a nested type then it is allowed either if
994 * the enclosing is a static or the enclosing is also selected.
995 *
996 * @param e the element to be checked
997 * @return true if the element is visible
998 */
999 public boolean isSelected(Element e) {
1000 if (toolEnv.isSynthetic((Symbol) e)) {
1001 return false;
1002 }
1003 if (visibleElementVisitor == null) {
1004 visibleElementVisitor = new SimpleElementVisitor9<Boolean, Void>() {
1005 @Override
1006 public Boolean visitType(TypeElement e, Void p) {
1007 if (!accessFilter.checkModifier(e)) {
1008 return false; // it is not allowed
1009 }
1010 Element encl = e.getEnclosingElement();
1011
1012 // check if nested
1013 if (encl.getKind() == ElementKind.PACKAGE)
1014 return true; // top-level class, allow it
1015
1016 // is enclosed static
1017 if (encl.getModifiers().contains(Modifier.STATIC))
1018 return true; // allowed
1019
1020 // check the enclosing
1021 return visit(encl);
1022 }
1023
1024 @Override
1025 protected Boolean defaultAction(Element e, Void p) {
1026 return accessFilter.checkModifier(e);
1027 }
1028
1029 @Override
1030 public Boolean visitUnknown(Element e, Void p) {
1031 throw new AssertionError("unkown element: " + p);
1032 }
1033 };
1034 }
1035 return visibleElementVisitor.visit(e);
1036 }
1037
1038 private class IncludedVisitor extends SimpleElementVisitor9<Boolean, Void> {
1039 final private Set<Element> includedCache;
1040
1041 public IncludedVisitor() {
1042 includedCache = new LinkedHashSet<>();
1043 }
1044
1045 @Override
1046 public Boolean visitModule(ModuleElement e, Void p) {
1047 // deduced by specified and/or requires expansion
1048 return includedModuleElements.contains(e);
1049 }
1050
1051 @Override
1052 public Boolean visitPackage(PackageElement e, Void p) {
1053 // deduced by specified or downward expansions
1054 return includedPackageElements.contains(e);
1055 }
1056
1057 @Override
1058 public Boolean visitType(TypeElement e, Void p) {
1183 public String toString() {
1184 return moduleName == null ? packageName : moduleName + "/" + packageName;
1185 }
1186 }
1187
1188 /**
1189 * A class which filters the access flags on classes, fields, methods, etc.
1190 *
1191 * @see javax.lang.model.element.Modifier
1192 */
1193
1194 static class ModifierFilter {
1195 /**
1196 * The allowed ElementKind that can be stored.
1197 */
1198 static final EnumSet<ElementKind> ALLOWED_KINDS = EnumSet.of(ElementKind.METHOD,
1199 ElementKind.CLASS,
1200 ElementKind.PACKAGE,
1201 ElementKind.MODULE);
1202
1203 // all possible accesss levels allowed for each element
1204 private final EnumMap<ElementKind, EnumSet<AccessKind>> filterMap =
1205 new EnumMap<>(ElementKind.class);
1206
1207 // the specified access level for each element
1208 private final EnumMap<ElementKind, AccessKind> accessMap =
1209 new EnumMap<>(ElementKind.class);
1210
1211 /**
1212 * Constructor - Specify a filter.
1213 *
1214 * @param accessSet an Access filter.
1215 */
1216 ModifierFilter(Map<ToolOption, Object> opts) {
1217
1218 AccessKind accessValue = null;
1219 for (ElementKind kind : ALLOWED_KINDS) {
1220 switch (kind) {
1221 case METHOD:
1222 accessValue = (AccessKind)opts.get(ToolOption.SHOW_MEMBERS);
1223 break;
1268 */
1269 public boolean checkModifier(Element e) {
1270 Set<Modifier> modifiers = e.getModifiers();
1271 AccessKind fflag = AccessKind.PACKAGE;
1272 if (modifiers.contains(Modifier.PUBLIC)) {
1273 fflag = AccessKind.PUBLIC;
1274 } else if (modifiers.contains(Modifier.PROTECTED)) {
1275 fflag = AccessKind.PROTECTED;
1276 } else if (modifiers.contains(Modifier.PRIVATE)) {
1277 fflag = AccessKind.PRIVATE;
1278 }
1279 EnumSet<AccessKind> filterSet = filterMap.get(getAllowedKind(e.getKind()));
1280 return filterSet.contains(fflag);
1281 }
1282
1283 // convert a requested element kind to an allowed access kind
1284 private ElementKind getAllowedKind(ElementKind kind) {
1285 switch (kind) {
1286 case CLASS: case METHOD: case MODULE: case PACKAGE:
1287 return kind;
1288 case ANNOTATION_TYPE: case ENUM: case INTERFACE:
1289 return ElementKind.CLASS;
1290 case CONSTRUCTOR: case ENUM_CONSTANT: case EXCEPTION_PARAMETER:
1291 case FIELD: case INSTANCE_INIT: case LOCAL_VARIABLE: case PARAMETER:
1292 case RESOURCE_VARIABLE: case STATIC_INIT: case TYPE_PARAMETER:
1293 return ElementKind.METHOD;
1294 default:
1295 throw new AssertionError("unsupported kind: " + kind);
1296 }
1297 }
1298 } // end ModifierFilter
1299 }
|
30 import java.util.Collections;
31 import java.util.EnumMap;
32 import java.util.EnumSet;
33 import java.util.HashMap;
34 import java.util.HashSet;
35 import java.util.LinkedHashMap;
36 import java.util.LinkedHashSet;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Set;
40
41 import javax.lang.model.element.Element;
42 import javax.lang.model.element.ElementKind;
43 import javax.lang.model.element.Modifier;
44 import javax.lang.model.element.ModuleElement;
45 import javax.lang.model.element.ModuleElement.ExportsDirective;
46 import javax.lang.model.element.ModuleElement.RequiresDirective;
47 import javax.lang.model.element.PackageElement;
48 import javax.lang.model.element.TypeElement;
49 import javax.lang.model.util.ElementFilter;
50 import javax.lang.model.util.SimpleElementVisitor14;
51 import javax.tools.JavaFileManager;
52 import javax.tools.JavaFileManager.Location;
53 import javax.tools.JavaFileObject;
54 import javax.tools.StandardLocation;
55
56 import com.sun.tools.javac.code.Kinds.Kind;
57 import com.sun.tools.javac.code.Source;
58 import com.sun.tools.javac.code.Source.Feature;
59 import com.sun.tools.javac.code.Symbol;
60 import com.sun.tools.javac.code.Symbol.ClassSymbol;
61 import com.sun.tools.javac.code.Symbol.CompletionFailure;
62 import com.sun.tools.javac.code.Symbol.ModuleSymbol;
63 import com.sun.tools.javac.code.Symbol.PackageSymbol;
64 import com.sun.tools.javac.code.Symtab;
65 import com.sun.tools.javac.comp.Modules;
66 import com.sun.tools.javac.main.JavaCompiler;
67 import com.sun.tools.javac.tree.JCTree.JCClassDecl;
68 import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
69 import com.sun.tools.javac.tree.JCTree.JCModuleDecl;
70 import com.sun.tools.javac.tree.TreeInfo;
968 else
969 messager.printWarningUsingKey("main.unexpected.exception", e);
970 }
971 }
972
973 /**
974 * Returns a list of all classes contained in this package, including
975 * member classes of those classes, and their member classes, etc.
976 */
977 private void addAllClasses(Collection<TypeElement> list, PackageElement pkg) {
978 boolean filtered = true;
979 for (Element isym : pkg.getEnclosedElements()) {
980 addAllClasses(list, (TypeElement)isym, filtered);
981 }
982 }
983
984 private boolean isTypeElementSelected(TypeElement te) {
985 return (xclasses || toolEnv.getFileKind(te) == SOURCE) && isSelected(te);
986 }
987
988 @SuppressWarnings("preview")
989 SimpleElementVisitor14<Boolean, Void> visibleElementVisitor = null;
990 /**
991 * Returns true if the element is selected, by applying
992 * the access filter checks. Special treatment is applied to
993 * types, for a top level type the access filter applies completely,
994 * however if is a nested type then it is allowed either if
995 * the enclosing is a static or the enclosing is also selected.
996 *
997 * @param e the element to be checked
998 * @return true if the element is visible
999 */
1000 @SuppressWarnings("preview")
1001 public boolean isSelected(Element e) {
1002 if (toolEnv.isSynthetic((Symbol) e)) {
1003 return false;
1004 }
1005 if (visibleElementVisitor == null) {
1006 visibleElementVisitor = new SimpleElementVisitor14<Boolean, Void>() {
1007 @Override
1008 public Boolean visitType(TypeElement e, Void p) {
1009 if (!accessFilter.checkModifier(e)) {
1010 return false; // it is not allowed
1011 }
1012 Element encl = e.getEnclosingElement();
1013
1014 // check if nested
1015 if (encl.getKind() == ElementKind.PACKAGE)
1016 return true; // top-level class, allow it
1017
1018 // is enclosed static
1019 if (encl.getModifiers().contains(Modifier.STATIC))
1020 return true; // allowed
1021
1022 // check the enclosing
1023 return visit(encl);
1024 }
1025
1026 @Override
1027 protected Boolean defaultAction(Element e, Void p) {
1028 return accessFilter.checkModifier(e);
1029 }
1030
1031 @Override
1032 public Boolean visitUnknown(Element e, Void p) {
1033 throw new AssertionError("unkown element: " + p);
1034 }
1035 };
1036 }
1037 return visibleElementVisitor.visit(e);
1038 }
1039
1040 @SuppressWarnings("preview")
1041 private class IncludedVisitor extends SimpleElementVisitor14<Boolean, Void> {
1042 final private Set<Element> includedCache;
1043
1044 public IncludedVisitor() {
1045 includedCache = new LinkedHashSet<>();
1046 }
1047
1048 @Override
1049 public Boolean visitModule(ModuleElement e, Void p) {
1050 // deduced by specified and/or requires expansion
1051 return includedModuleElements.contains(e);
1052 }
1053
1054 @Override
1055 public Boolean visitPackage(PackageElement e, Void p) {
1056 // deduced by specified or downward expansions
1057 return includedPackageElements.contains(e);
1058 }
1059
1060 @Override
1061 public Boolean visitType(TypeElement e, Void p) {
1186 public String toString() {
1187 return moduleName == null ? packageName : moduleName + "/" + packageName;
1188 }
1189 }
1190
1191 /**
1192 * A class which filters the access flags on classes, fields, methods, etc.
1193 *
1194 * @see javax.lang.model.element.Modifier
1195 */
1196
1197 static class ModifierFilter {
1198 /**
1199 * The allowed ElementKind that can be stored.
1200 */
1201 static final EnumSet<ElementKind> ALLOWED_KINDS = EnumSet.of(ElementKind.METHOD,
1202 ElementKind.CLASS,
1203 ElementKind.PACKAGE,
1204 ElementKind.MODULE);
1205
1206 // all possible access levels allowed for each element
1207 private final EnumMap<ElementKind, EnumSet<AccessKind>> filterMap =
1208 new EnumMap<>(ElementKind.class);
1209
1210 // the specified access level for each element
1211 private final EnumMap<ElementKind, AccessKind> accessMap =
1212 new EnumMap<>(ElementKind.class);
1213
1214 /**
1215 * Constructor - Specify a filter.
1216 *
1217 * @param accessSet an Access filter.
1218 */
1219 ModifierFilter(Map<ToolOption, Object> opts) {
1220
1221 AccessKind accessValue = null;
1222 for (ElementKind kind : ALLOWED_KINDS) {
1223 switch (kind) {
1224 case METHOD:
1225 accessValue = (AccessKind)opts.get(ToolOption.SHOW_MEMBERS);
1226 break;
1271 */
1272 public boolean checkModifier(Element e) {
1273 Set<Modifier> modifiers = e.getModifiers();
1274 AccessKind fflag = AccessKind.PACKAGE;
1275 if (modifiers.contains(Modifier.PUBLIC)) {
1276 fflag = AccessKind.PUBLIC;
1277 } else if (modifiers.contains(Modifier.PROTECTED)) {
1278 fflag = AccessKind.PROTECTED;
1279 } else if (modifiers.contains(Modifier.PRIVATE)) {
1280 fflag = AccessKind.PRIVATE;
1281 }
1282 EnumSet<AccessKind> filterSet = filterMap.get(getAllowedKind(e.getKind()));
1283 return filterSet.contains(fflag);
1284 }
1285
1286 // convert a requested element kind to an allowed access kind
1287 private ElementKind getAllowedKind(ElementKind kind) {
1288 switch (kind) {
1289 case CLASS: case METHOD: case MODULE: case PACKAGE:
1290 return kind;
1291 case RECORD: case ANNOTATION_TYPE: case ENUM: case INTERFACE:
1292 return ElementKind.CLASS;
1293 case CONSTRUCTOR: case ENUM_CONSTANT: case EXCEPTION_PARAMETER:
1294 case FIELD: case INSTANCE_INIT: case LOCAL_VARIABLE: case PARAMETER:
1295 case RESOURCE_VARIABLE: case STATIC_INIT: case TYPE_PARAMETER:
1296 return ElementKind.METHOD;
1297 default:
1298 throw new AssertionError("unsupported kind: " + kind);
1299 }
1300 }
1301 } // end ModifierFilter
1302 }
|