use of com.sun.tools.javac.code.Symbol.PackageSymbol in project checker-framework by typetools.
the class FlowExpressionParseUtil method matchPackageNameWithinExpression.
/**
* Greedily matches the longest substring of {@code expression} to a package (starting from the
* beginning of the string).
*
* @param expression the expression string that may start with a package name
* @param resolver the {@code Resolver} for the current processing environment
* @param path the tree path to the local scope
* @return {@code null} if the expression string did not start with a package name; otherwise a
* {@code Pair} containing the {@code PackageSymbol} for the matched package, and the
* remaining substring of the expression (always non-null) after the package name
* @throws FlowExpressionParseException if the entire expression string matches a package name
*/
private static Pair<PackageSymbol, String> matchPackageNameWithinExpression(String expression, Resolver resolver, TreePath path) throws FlowExpressionParseException {
Pair<String, String> select = parseMemberSelect(expression);
// groups will not be filled in.
if (select == null) {
return null;
}
String packageName = select.first;
String remainingString = select.second, remainingStringIfPackageMatched = remainingString;
// the result of this method call
PackageSymbol result = null;
while (true) {
// At this point, packageName is one component longer than result, and that extra
// component appears in remainingString but not in remainingStringIfPackageMatched. In
// other words, result and remainingStringIfPackageMatched are consistent, and
// packageName and remainingString are consistent. Try to set result to account for the
// extra component in packageName.
PackageSymbol longerResult;
try {
longerResult = resolver.findPackage(packageName, path);
} catch (Throwable t) {
throw constructParserException(expression, "findPackage threw an exception when looking up package " + packageName, t);
}
if (longerResult == null) {
break;
}
result = longerResult;
remainingString = remainingStringIfPackageMatched;
select = parseMemberSelect(remainingString);
if (select != null) {
packageName += "." + select.first;
remainingStringIfPackageMatched = select.second;
} else {
// There are no dots in remainingString, so we are done.
// Fail if the whole string represents a package, otherwise return.
PackageSymbol wholeExpressionAsPackage;
try {
wholeExpressionAsPackage = resolver.findPackage(expression, path);
} catch (Throwable t) {
throw constructParserException(expression, "findPackage threw an exception when looking up package " + expression, t);
}
if (wholeExpressionAsPackage != null) {
// The entire expression matches a package name.
throw constructParserException(expression, "a flow expression string cannot be just a package name");
}
break;
}
}
if (result == null) {
return null;
}
// an exception would have been thrown above if the entire expression is a package name
assert remainingString != null;
return Pair.of(result, remainingString);
}
use of com.sun.tools.javac.code.Symbol.PackageSymbol in project error-prone by google.
the class FindIdentifiers method isVisible.
private static boolean isVisible(VarSymbol var, final TreePath path) {
switch(var.getKind()) {
case ENUM_CONSTANT:
case FIELD:
// TODO(eaftan): Switch collector to ImmutableList.toImmutableList() when released
List<ClassSymbol> enclosingClasses = StreamSupport.stream(path.spliterator(), false).filter(tree -> tree instanceof ClassTree).map(ClassTree.class::cast).map(ASTHelpers::getSymbol).collect(Collectors.toCollection(ArrayList::new));
if (!var.isStatic()) {
// Instance fields are not visible if we are in a static context...
if (inStaticContext(path)) {
return false;
}
// the enclosing static nested class (JLS 8.5.1).
if (lowerThan(path, (curr, unused) -> curr instanceof ClassTree && ASTHelpers.getSymbol((ClassTree) curr).isStatic(), (curr, unused) -> curr instanceof ClassTree && ASTHelpers.getSymbol((ClassTree) curr).equals(var.owner))) {
return false;
}
}
// fields (JLS 6.6.1).
if (enclosingClasses.contains(ASTHelpers.enclosingClass(var))) {
return true;
}
PackageSymbol enclosingPackage = ((JCCompilationUnit) path.getCompilationUnit()).packge;
Set<Modifier> modifiers = var.getModifiers();
// (JLS 6.6.1).
if (Objects.equals(enclosingPackage, ASTHelpers.enclosingPackage(var))) {
return !modifiers.contains(Modifier.PRIVATE);
}
// in the enclosing class or a superclass).
return modifiers.contains(Modifier.PUBLIC) || modifiers.contains(Modifier.PROTECTED);
case PARAMETER:
case LOCAL_VARIABLE:
// final or effectively final (JLS 8.1.3).
if (lowerThan(path, (curr, parent) -> curr.getKind() == Kind.LAMBDA_EXPRESSION || (curr.getKind() == Kind.NEW_CLASS && ((NewClassTree) curr).getClassBody() != null) || (curr.getKind() == Kind.CLASS && parent.getKind() == Kind.BLOCK), (curr, unused) -> Objects.equals(var.owner, ASTHelpers.getSymbol(curr)))) {
if ((var.flags() & (Flags.FINAL | Flags.EFFECTIVELY_FINAL)) == 0) {
return false;
}
}
return true;
case EXCEPTION_PARAMETER:
case RESOURCE_VARIABLE:
return true;
default:
throw new IllegalArgumentException("Unexpected variable type: " + var.getKind());
}
}
use of com.sun.tools.javac.code.Symbol.PackageSymbol in project j2objc by google.
the class TreeConverter method convertPackage.
private PackageDeclaration convertPackage(PackageElement pkg, Trees trees) {
JCTree node = (JCTree) trees.getTree(pkg);
PackageDeclaration newNode = new PackageDeclaration().setPackageElement(pkg);
for (JCTree.JCAnnotation pkgAnnotation : unit.getPackageAnnotations()) {
newNode.addAnnotation((Annotation) convert(pkgAnnotation));
}
if (unit.sourcefile.toUri().getPath().endsWith("package-info.java")) {
newNode.setJavadoc((Javadoc) getAssociatedJavaDoc(node, pkg));
}
return (PackageDeclaration) newNode.setName(convertName((PackageSymbol) pkg, getPosition(node))).setPosition(SourcePosition.NO_POSITION);
}
use of com.sun.tools.javac.code.Symbol.PackageSymbol in project checker-framework by typetools.
the class FlowExpressionParseUtil method matchPackageAndClassNameWithinExpression.
/**
* Matches a substring of {@code expression} to a package and class name (starting from the
* beginning of the string).
*
* @param expression the expression string that may start with a package and class name
* @param resolver the {@code Resolver} for the current processing environment
* @param path the tree path to the local scope
* @return {@code null} if the expression string did not start with a package name; otherwise a
* {@code Pair} containing the {@code ClassName} for the matched class, and the remaining
* substring of the expression (possibly null) after the package and class name.
* @throws FlowExpressionParseException if the entire expression string matches a package name
* (but no class name), or if a package name was matched but the class could not be found
* within the package (e.g., {@code "myExistingPackage.myNonExistentClass"}).
*/
private static Pair<ClassName, String> matchPackageAndClassNameWithinExpression(String expression, Resolver resolver, TreePath path) throws FlowExpressionParseException {
Pair<PackageSymbol, String> packageSymbolAndRemainingString = matchPackageNameWithinExpression(expression, resolver, path);
if (packageSymbolAndRemainingString == null) {
return null;
}
PackageSymbol packageSymbol = packageSymbolAndRemainingString.first;
String packageRemainingString = packageSymbolAndRemainingString.second;
Pair<String, String> select = parseMemberSelect(packageRemainingString);
String classNameString;
String remainingString;
if (select != null) {
classNameString = select.first;
remainingString = select.second;
} else {
classNameString = packageRemainingString;
remainingString = null;
}
ClassSymbol classSymbol;
try {
classSymbol = resolver.findClassInPackage(classNameString, packageSymbol, path);
} catch (Throwable t) {
throw constructParserException(expression, " findClassInPackage threw an exception when looking up class " + classNameString + " in package " + packageSymbol.toString(), t);
}
if (classSymbol == null) {
throw constructParserException(expression, "classSymbol==null when looking up class " + classNameString + " in package " + packageSymbol.toString());
}
TypeMirror classType = ElementUtils.getType(classSymbol);
if (classType == null) {
throw constructParserException(expression, "classType==null when looking for class symbol " + classSymbol);
}
return Pair.of(new ClassName(classType), remainingString);
}
use of com.sun.tools.javac.code.Symbol.PackageSymbol in project checker-framework by typetools.
the class Resolver method findPackage.
/**
* Finds the package with name {@code name}.
*
* @param name the name of the package
* @param path the tree path to the local scope
* @return the {@code PackageSymbol} for the package if it is found, {@code null} otherwise
*/
@Nullable
public PackageSymbol findPackage(String name, TreePath path) {
Log.DiagnosticHandler discardDiagnosticHandler = new Log.DiscardDiagnosticHandler(log);
try {
Env<AttrContext> env = getEnvForPath(path);
Element res = wrapInvocationOnResolveInstance(FIND_IDENT, env, names.fromString(name), Kinds.KindSelector.PCK);
// a.b.c.MyClass.myStaticField. "exists()" must be called on it to ensure that it exists.
if (res.getKind() == ElementKind.PACKAGE) {
PackageSymbol ps = (PackageSymbol) res;
return ps.exists() ? ps : null;
} else {
return null;
}
} finally {
log.popDiagnosticHandler(discardDiagnosticHandler);
}
}
Aggregations