本文整理汇总了Java中com.sun.tools.javac.api.JavacScope类的典型用法代码示例。如果您正苦于以下问题:Java JavacScope类的具体用法?Java JavacScope怎么用?Java JavacScope使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JavacScope类属于com.sun.tools.javac.api包,在下文中一共展示了JavacScope类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: findField
import com.sun.tools.javac.api.JavacScope; //导入依赖的package包/类
/**
* Finds the field with name {@code name} in a given type.
*
* <p>
* The method adheres to all the rules of Java's scoping (while also
* considering the imports) for name resolution.
*
* @param name
* The name of the field.
* @param type
* The type of the receiver (i.e., the type in which to look for
* the field).
* @param path
* The tree path to the local scope.
* @return The element for the field.
*/
public VariableElement findField(String name, TypeMirror type, TreePath path) {
Log.DiagnosticHandler discardDiagnosticHandler =
new Log.DiscardDiagnosticHandler(log);
try {
JavacScope scope = (JavacScope) trees.getScope(path);
Env<AttrContext> env = scope.getEnv();
Element res = wrapInvocation(FIND_IDENT_IN_TYPE, env, type,
names.fromString(name), VAR);
if (res.getKind() == ElementKind.FIELD) {
return (VariableElement) res;
} else {
// Most likely didn't find the field and the Element is a SymbolNotFoundError
return null;
}
} finally {
log.popDiagnosticHandler(discardDiagnosticHandler);
}
}
示例2: findVariable
import com.sun.tools.javac.api.JavacScope; //导入依赖的package包/类
/**
* Finds the variable referenced in the passed {@code String}.
*
* This method may only operate on variable references, e.g. local
* variables, parameters, fields.
*
* The reference string may be either an single Java identifier (e.g. "field")
* or dot-separated identifiers (e.g. "Collections.EMPTY_LIST").
*
* The method adheres to all the rules of Java's scoping (while also
* considering the imports) for name resolution.
*
* @param reference the variable reference string
* @param path the tree path to the local scope
* @return the variable reference
*/
public Element findVariable(String reference, TreePath path) {
JavacScope scope = (JavacScope) trees.getScope(path);
Env<AttrContext> env = scope.getEnv();
if (!reference.contains(".")) {
// Simple variable
return wrapInvocation(
FIND_IDENT,
env, names.fromString(reference), Kinds.VAR);
} else {
int lastDot = reference.lastIndexOf('.');
String expr = reference.substring(0, lastDot);
String name = reference.substring(lastDot + 1);
Element site = findType(expr, env);
Name ident = names.fromString(name);
return wrapInvocation(
FIND_IDENT_IN_TYPE,
env, site.asType(), ident, VAR);
}
}
示例3: getInvocationTargetType
import com.sun.tools.javac.api.JavacScope; //导入依赖的package包/类
@Override
public IsType getInvocationTargetType(CompilationUnitTree cup, MethodInvocationTree node) {
if (node instanceof JCMethodInvocation) {
final TreePath path = trees.getPath(cup, node);
final TreePath parent = path.getParentPath();
final Tree target = parent.getLeaf();
switch (target.getKind()) {
case VARIABLE:
JCVariableDecl var = (JCVariableDecl) target;
final JCTree type = var.getType();
final JavacScope scope = trees.getScope(path);
final IsType cls = getTypeOf(cup, scope, type);
return cls;
default:
X_Log.error(getClass(), "Unhandled invocation target type: ", target.getKind(), target);
}
} else {
X_Log.warn(getClass(), "Does not support MethodInvocationTree ", node);
}
return null;
}
示例4: getEnv
import com.sun.tools.javac.api.JavacScope; //导入依赖的package包/类
private static Env<AttrContext> getEnv(Scope scope) {
if (scope instanceof NBScope) {
scope = ((NBScope) scope).delegate;
}
return ((JavacScope) scope).getEnv();
}
示例5: attributeTreeTo
import com.sun.tools.javac.api.JavacScope; //导入依赖的package包/类
private static Scope attributeTreeTo(JavacTaskImpl jti, Tree tree, Scope scope, Tree to, final List<Diagnostic<? extends JavaFileObject>> errors) {
Log log = Log.instance(jti.getContext());
JavaFileObject prev = log.useSource(new DummyJFO());
Log.DiagnosticHandler discardHandler = new Log.DiscardDiagnosticHandler(log) {
@Override
public void report(JCDiagnostic diag) {
errors.add(diag);
}
};
NBResolve resolve = NBResolve.instance(jti.getContext());
resolve.disableAccessibilityChecks();
// Enter enter = Enter.instance(jti.getContext());
// enter.shadowTypeEnvs(true);
// ArgumentAttr argumentAttr = ArgumentAttr.instance(jti.getContext());
// ArgumentAttr.LocalCacheContext cacheContext = argumentAttr.withLocalCacheContext();
try {
Attr attr = Attr.instance(jti.getContext());
Env<AttrContext> env = getEnv(scope);
Env<AttrContext> result = tree instanceof JCExpression ?
attr.attribExprToTree((JCExpression) tree, env, (JCTree) to) :
attr.attribStatToTree((JCTree) tree, env, (JCTree) to);
try {
Constructor<JavacScope> c = JavacScope.class.getDeclaredConstructor(Env.class);
c.setAccessible(true);
return c.newInstance(result);
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
throw new IllegalStateException(ex);
}
} finally {
// cacheContext.leave();
log.useSource(prev);
log.popDiagnosticHandler(discardHandler);
resolve.restoreAccessbilityChecks();
// enter.shadowTypeEnvs(false);
}
}
示例6: attributeTree
import com.sun.tools.javac.api.JavacScope; //导入依赖的package包/类
private static TypeMirror attributeTree(JavacTaskImpl jti, Tree tree, Scope scope, final List<Diagnostic<? extends JavaFileObject>> errors) {
Log log = Log.instance(jti.getContext());
JavaFileObject prev = log.useSource(new DummyJFO());
Log.DiagnosticHandler discardHandler = new Log.DiscardDiagnosticHandler(log) {
@Override
public void report(JCDiagnostic diag) {
errors.add(diag);
}
};
NBResolve resolve = NBResolve.instance(jti.getContext());
resolve.disableAccessibilityChecks();
// Enter enter = Enter.instance(jti.getContext());
// enter.shadowTypeEnvs(true);
// ArgumentAttr argumentAttr = ArgumentAttr.instance(jti.getContext());
// ArgumentAttr.LocalCacheContext cacheContext = argumentAttr.withLocalCacheContext();
try {
Attr attr = Attr.instance(jti.getContext());
Env<AttrContext> env = ((JavacScope) scope).getEnv();
if (tree instanceof JCExpression)
return attr.attribExpr((JCTree) tree,env, Type.noType);
return attr.attribStat((JCTree) tree,env);
} finally {
// cacheContext.leave();
log.useSource(prev);
log.popDiagnosticHandler(discardHandler);
resolve.restoreAccessbilityChecks();
// enter.shadowTypeEnvs(false);
}
}
示例7: attributeTree
import com.sun.tools.javac.api.JavacScope; //导入依赖的package包/类
private static TypeMirror attributeTree(JavacTaskImpl jti, Tree tree, Scope scope,
final List<Diagnostic<? extends JavaFileObject>> errors, @NullAllowed final Diagnostic.Kind filter) {
Log log = Log.instance(jti.getContext());
JavaFileObject prev = log.useSource(new DummyJFO());
Enter enter = Enter.instance(jti.getContext());
Log.DiagnosticHandler discardHandler = new Log.DiscardDiagnosticHandler(log) {
private Diagnostic.Kind f = filter == null ? Diagnostic.Kind.ERROR : filter;
@Override
public void report(JCDiagnostic diag) {
if (diag.getKind().compareTo(f) >= 0) {
errors.add(diag);
}
}
};
// ArgumentAttr argumentAttr = ArgumentAttr.instance(jti.getContext());
// ArgumentAttr.LocalCacheContext cacheContext = argumentAttr.withLocalCacheContext();
try {
// enter.shadowTypeEnvs(true);
Attr attr = Attr.instance(jti.getContext());
Env<AttrContext> env = ((JavacScope) scope).getEnv();
if (tree instanceof JCTree.JCExpression) {
return attr.attribExpr((JCTree) tree,env, Type.noType);
}
return attr.attribStat((JCTree) tree,env);
} finally {
// cacheContext.leave();
log.useSource(prev);
log.popDiagnosticHandler(discardHandler);
// enter.shadowTypeEnvs(false);
}
}
示例8: findMethod
import com.sun.tools.javac.api.JavacScope; //导入依赖的package包/类
/**
* Finds the method element for a given name and list of expected parameter
* types.
*
* <p>
* The method adheres to all the rules of Java's scoping (while also
* considering the imports) for name resolution.
*
* @param methodName
* Name of the method to find.
* @param receiverType
* Type of the receiver of the method
* @param path
* Tree path.
* @return The method element (if found).
*/
public Element findMethod(String methodName, TypeMirror receiverType,
TreePath path, java.util.List<TypeMirror> argumentTypes) {
Log.DiagnosticHandler discardDiagnosticHandler =
new Log.DiscardDiagnosticHandler(log);
try {
JavacScope scope = (JavacScope) trees.getScope(path);
Env<AttrContext> env = scope.getEnv();
Type site = (Type) receiverType;
Name name = names.fromString(methodName);
List<Type> argtypes = List.nil();
for (TypeMirror a : argumentTypes) {
argtypes = argtypes.append((Type) a);
}
List<Type> typeargtypes = List.nil();
boolean allowBoxing = true;
boolean useVarargs = false;
boolean operator = true;
try {
// For some reason we have to set our own method context, which is rather ugly.
// TODO: find a nicer way to do this.
Object methodContext = buildMethodContext();
Object oldContext = getField(resolve, "currentResolutionContext");
setField(resolve, "currentResolutionContext", methodContext);
Element result = wrapInvocation(FIND_METHOD, env, site, name, argtypes,
typeargtypes, allowBoxing, useVarargs, operator);
setField(resolve, "currentResolutionContext", oldContext);
return result;
} catch (Throwable t) {
Error err = new AssertionError("Unexpected Reflection error");
err.initCause(t);
throw err;
}
} finally {
log.popDiagnosticHandler(discardDiagnosticHandler);
}
}
示例9: getName
import com.sun.tools.javac.api.JavacScope; //导入依赖的package包/类
@Override
public IsNamedType getName(CompilationUnitTree cup, MethodInvocationTree node) {
if (node instanceof JCMethodInvocation) {
final String simpleName = TreeInfo.name(((JCMethodInvocation) node).meth).toString();
String fullName = TreeInfo.fullName(((JCMethodInvocation) node).meth).toString();
String className = fullName.indexOf('.') == -1 ? fullName : fullName.replaceFirst("[.](?:[^.]+)$", "");
if (fullName.equals(className)) {
// This is a local method reference. Find the enclosing class type
ClassTree cls = getEnclosingClass(cup, node);
className = getNameOf(cls);
return IsNamedType.namedType(className, simpleName);
}
final TreePath path = trees.getPath(cup, node);
final JavacScope scope = trees.getScope(path);
if (scope.isStarImportScope()) {
// need to do lookup on the simple name of the method
} else {
// need to do lookup on the class of the imported method
}
final Optional<? extends ImportTree> match = cup.getImports().stream()
.filter(importName -> {
final Name n = TreeInfo.name((JCTree) importName.getQualifiedIdentifier());
return n.contentEquals(simpleName);
})
.findFirst();
return IsNamedType.namedType(className, simpleName);
} else {
X_Log.warn(getClass(), "Does not support MethodInvocationTree ", node);
}
return null;
}
示例10: attributeTree
import com.sun.tools.javac.api.JavacScope; //导入依赖的package包/类
public TypeMirror attributeTree(Tree tree, Scope scope) {
// if (scope instanceof NBScope && ((NBScope)scope).areAccessibilityChecksDisabled()) {
// NBResolve.instance(info.impl.getJavacTask().getContext()).disableAccessibilityChecks();
// }
// try {
return info.getJavacTask().attributeTree((JCTree) tree, ((JavacScope) scope).getEnv());
// } finally {
// NBResolve.instance(info.impl.getJavacTask().getContext()).restoreAccessbilityChecks();
// }
}
示例11: attributeTreeTo
import com.sun.tools.javac.api.JavacScope; //导入依赖的package包/类
/**Attribute the given tree until the given <code>to</code> tree is reached.
* Returns scope valid at point when <code>to</code> is reached.
*/
public Scope attributeTreeTo(Tree tree, Scope scope, Tree to) {
// if (scope instanceof NBScope && ((NBScope)scope).areAccessibilityChecksDisabled()) {
// NBResolve.instance(info.impl.getJavacTask().getContext()).disableAccessibilityChecks();
// }
// try {
return info.getJavacTask().attributeTreeTo((JCTree)tree, ((JavacScope)scope).getEnv(), (JCTree)to);
// } finally {
// NBResolve.instance(info.impl.getJavacTask().getContext()).restoreAccessbilityChecks();
// }
}
示例12: reattributeTree
import com.sun.tools.javac.api.JavacScope; //导入依赖的package包/类
public TypeMirror reattributeTree(Tree tree, Scope scope) {
Env<AttrContext> env = ((JavacScope)scope).getEnv();
copyInnerClassIndexes(env.tree, tree);
// if (scope instanceof NBScope && ((NBScope)scope).areAccessibilityChecksDisabled()) {
// NBResolve.instance(info.impl.getJavacTask().getContext()).disableAccessibilityChecks();
// }
// try {
return info.getJavacTask().attributeTree((JCTree)tree, env);
// } finally {
// NBResolve.instance(info.impl.getJavacTask().getContext()).restoreAccessbilityChecks();
// }
}
示例13: reattributeTreeTo
import com.sun.tools.javac.api.JavacScope; //导入依赖的package包/类
public Scope reattributeTreeTo(Tree tree, Scope scope, Tree to) {
Env<AttrContext> env = ((JavacScope)scope).getEnv();
copyInnerClassIndexes(env.tree, tree);
// if (scope instanceof NBScope && ((NBScope)scope).areAccessibilityChecksDisabled()) {
// NBResolve.instance(info.impl.getJavacTask().getContext()).disableAccessibilityChecks();
// }
// try {
return info.getJavacTask().attributeTreeTo((JCTree)tree, env, (JCTree)to);
// } finally {
// NBResolve.instance(info.impl.getJavacTask().getContext()).restoreAccessbilityChecks();
// }
}
示例14: NBScope
import com.sun.tools.javac.api.JavacScope; //导入依赖的package包/类
private NBScope(JavacScope delegate) {
this.delegate = delegate;
}
示例15: isStaticContext
import com.sun.tools.javac.api.JavacScope; //导入依赖的package包/类
/**Checks whether the given scope is in "static" context.
*/
public boolean isStaticContext(Scope scope) {
return Resolve.instance(info.getJavacTask().getContext()).isStatic(((JavacScope)scope).getEnv());
}