本文整理了Java中com.sun.tools.javac.code.Types.findDescriptorSymbol()
方法的一些代码示例,展示了Types.findDescriptorSymbol()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Types.findDescriptorSymbol()
方法的具体详情如下:
包路径:com.sun.tools.javac.code.Types
类名称:Types
方法名:findDescriptorSymbol
[英]Find the method descriptor associated to this class symbol - if the symbol 'origin' is not a functional interface, an exception is thrown.
[中]查找与此类符号关联的方法描述符-如果符号“origin”不是函数接口,则会引发异常。
代码示例来源:origin: uber/NullAway
/**
* finds the corresponding functional interface method for a lambda expression or method reference
*
* @param tree the lambda expression or method reference
* @return the functional interface method
*/
public static Symbol.MethodSymbol getFunctionalInterfaceMethod(ExpressionTree tree, Types types) {
Preconditions.checkArgument(
(tree instanceof LambdaExpressionTree) || (tree instanceof MemberReferenceTree));
Type funcInterfaceType = ((JCTree.JCFunctionalExpression) tree).type;
return (Symbol.MethodSymbol) types.findDescriptorSymbol(funcInterfaceType.tsym);
}
代码示例来源:origin: google/error-prone
@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
ClassTree enclosingClazz = ASTHelpers.findEnclosingNode(state.getPath(), ClassTree.class);
if (tree.getModifiers().getFlags().contains(Modifier.DEFAULT)
&& IS_FUNCTIONAL_INTERFACE.matches(enclosingClazz, state)) {
Types types = Types.instance(state.context);
Set<Symbol> functionalSuperInterfaceSams =
enclosingClazz.getImplementsClause().stream()
.filter(t -> IS_FUNCTIONAL_INTERFACE.matches(t, state))
.map(ASTHelpers::getSymbol)
.map(TypeSymbol.class::cast)
.map(types::findDescriptorSymbol) // TypeSymbol to single abstract method of the type
.collect(toImmutableSet());
// We designate an override of a superinterface SAM "behavior preserving" if it just
// calls the SAM of this interface.
Symbol thisInterfaceSam = types.findDescriptorSymbol(ASTHelpers.getSymbol(enclosingClazz));
// relatively crude: doesn't verify that the same args are passed in the same order
// so it can get false positives for behavior-preservingness (false negatives for the check)
TreeVisitor<Boolean, VisitorState> behaviorPreserving =
new BehaviorPreservingChecker(thisInterfaceSam);
if (!Collections.disjoint(
ASTHelpers.findSuperMethods(ASTHelpers.getSymbol(tree), types),
functionalSuperInterfaceSams)
&& !tree.accept(behaviorPreserving, state)) {
return describeMatch(tree);
}
}
return Description.NO_MATCH;
}
代码示例来源:origin: org.kohsuke.sorcerer/sorcerer-javac
/**
* Is given type a functional interface?
*/
public boolean isFunctionalInterface(TypeSymbol tsym) {
try {
findDescriptorSymbol(tsym);
return true;
} catch (FunctionDescriptorLookupError ex) {
return false;
}
}
代码示例来源:origin: konsoletyper/teavm-javac
/**
* Is given type a functional interface?
*/
public boolean isFunctionalInterface(TypeSymbol tsym) {
try {
findDescriptorSymbol(tsym);
return true;
} catch (FunctionDescriptorLookupError ex) {
return false;
}
}
代码示例来源:origin: konsoletyper/teavm-javac
Type bridgedRefSig() {
return types.erasure(types.findDescriptorSymbol(tree.targets.head.tsym).type);
}
}
代码示例来源:origin: org.kohsuke.sorcerer/sorcerer-javac
Type bridgedRefSig() {
return types.erasure(types.findDescriptorSymbol(tree.targets.head.tsym).type);
}
}
代码示例来源:origin: org.kohsuke.sorcerer/sorcerer-javac
buf.append(types.findDescriptorSymbol(tree.type.tsym).owner.flatName());
buf.append(" ");
代码示例来源:origin: konsoletyper/teavm-javac
buf.append(types.findDescriptorSymbol(tree.type.tsym).owner.flatName());
buf.append(" ");
代码示例来源:origin: com.google.errorprone/error_prone_core
@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
ClassTree enclosingClazz = ASTHelpers.findEnclosingNode(state.getPath(), ClassTree.class);
if (tree.getModifiers().getFlags().contains(Modifier.DEFAULT)
&& IS_FUNCTIONAL_INTERFACE.matches(enclosingClazz, state)) {
Types types = Types.instance(state.context);
Set<Symbol> functionalSuperInterfaceSams =
enclosingClazz.getImplementsClause().stream()
.filter(t -> IS_FUNCTIONAL_INTERFACE.matches(t, state))
.map(ASTHelpers::getSymbol)
.map(TypeSymbol.class::cast)
.map(types::findDescriptorSymbol) // TypeSymbol to single abstract method of the type
.collect(toImmutableSet());
// We designate an override of a superinterface SAM "behavior preserving" if it just
// calls the SAM of this interface.
Symbol thisInterfaceSam = types.findDescriptorSymbol(ASTHelpers.getSymbol(enclosingClazz));
// relatively crude: doesn't verify that the same args are passed in the same order
// so it can get false positives for behavior-preservingness (false negatives for the check)
TreeVisitor<Boolean, VisitorState> behaviorPreserving =
new BehaviorPreservingChecker(thisInterfaceSam);
if (!Collections.disjoint(
ASTHelpers.findSuperMethods(ASTHelpers.getSymbol(tree), types),
functionalSuperInterfaceSams)
&& !tree.accept(behaviorPreserving, state)) {
return describeMatch(tree);
}
}
return Description.NO_MATCH;
}
代码示例来源:origin: org.netbeans.api/org-netbeans-modules-java-source-base
/**
* Find the element of the method descriptor associated to the functional interface.
*
* @param origin functional interface element
* @return associated method descriptor element or <code>null</code> if the <code>origin</code> is not a functional interface.
* @since 2.14
*/
public ExecutableElement getDescriptorElement(TypeElement origin) {
com.sun.tools.javac.code.Types types = com.sun.tools.javac.code.Types.instance(info.impl.getJavacTask().getContext());
if (types.isFunctionalInterface((TypeSymbol)origin)) {
Symbol sym = types.findDescriptorSymbol((TypeSymbol)origin);
if (sym != null && sym.getKind() == ElementKind.METHOD) {
return (ExecutableElement)sym;
}
}
return null;
}
代码示例来源:origin: org.kohsuke.sorcerer/sorcerer-javac
public void checkFunctionalInterface(JCClassDecl tree, ClassSymbol cs) {
Compound functionalType = cs.attribute(syms.functionalInterfaceType.tsym);
if (functionalType != null) {
try {
types.findDescriptorSymbol((TypeSymbol)cs);
} catch (Types.FunctionDescriptorLookupError ex) {
DiagnosticPosition pos = tree.pos();
for (JCAnnotation a : tree.getModifiers().annotations) {
if (a.annotationType.type.tsym == syms.functionalInterfaceType.tsym) {
pos = a.pos();
break;
}
}
log.error(pos, "bad.functional.intf.anno.1", ex.getDiagnostic());
}
}
}
}
代码示例来源:origin: konsoletyper/teavm-javac
public void checkFunctionalInterface(JCClassDecl tree, ClassSymbol cs) {
Compound functionalType = cs.attribute(syms.functionalInterfaceType.tsym);
if (functionalType != null) {
try {
types.findDescriptorSymbol((TypeSymbol)cs);
} catch (Types.FunctionDescriptorLookupError ex) {
DiagnosticPosition pos = tree.pos();
for (JCAnnotation a : tree.getModifiers().annotations) {
if (a.annotationType.type.tsym == syms.functionalInterfaceType.tsym) {
pos = a.pos();
break;
}
}
log.error(pos, "bad.functional.intf.anno.1", ex.getDiagnostic());
}
}
}
}
代码示例来源:origin: org.checkerframework/dataflow
Element overriddenElement =
com.sun.tools.javac.code.Types.instance(ctx)
.findDescriptorSymbol(
((Type) trees.getTypeMirror(lambdaTreePath)).tsym);
代码示例来源:origin: org.kohsuke.sorcerer/sorcerer-javac
private void checkLambdaCandidate(JCNewClass tree, ClassSymbol csym, Type clazztype) {
if (allowLambda &&
identifyLambdaCandidate &&
clazztype.hasTag(CLASS) &&
!pt().hasTag(NONE) &&
types.isFunctionalInterface(clazztype.tsym)) {
Symbol descriptor = types.findDescriptorSymbol(clazztype.tsym);
int count = 0;
boolean found = false;
for (Symbol sym : csym.members().getElements()) {
if ((sym.flags() & SYNTHETIC) != 0 ||
sym.isConstructor()) continue;
count++;
if (sym.kind != MTH ||
!sym.name.equals(descriptor.name)) continue;
Type mtype = types.memberType(clazztype, sym);
if (types.overrideEquivalent(mtype, types.memberType(clazztype, descriptor))) {
found = true;
}
}
if (found && count == 1) {
log.note(tree.def, "potential.lambda.found");
}
}
}
代码示例来源:origin: konsoletyper/teavm-javac
private void checkLambdaCandidate(JCNewClass tree, ClassSymbol csym, Type clazztype) {
if (allowLambda &&
identifyLambdaCandidate &&
clazztype.hasTag(CLASS) &&
!pt().hasTag(NONE) &&
types.isFunctionalInterface(clazztype.tsym)) {
Symbol descriptor = types.findDescriptorSymbol(clazztype.tsym);
int count = 0;
boolean found = false;
for (Symbol sym : csym.members().getElements()) {
if ((sym.flags() & SYNTHETIC) != 0 ||
sym.isConstructor()) continue;
count++;
if (sym.kind != MTH ||
!sym.name.equals(descriptor.name)) continue;
Type mtype = types.memberType(clazztype, sym);
if (types.overrideEquivalent(mtype, types.memberType(clazztype, descriptor))) {
found = true;
}
}
if (found && count == 1) {
log.note(tree.def, "potential.lambda.found");
}
}
}
代码示例来源:origin: konsoletyper/teavm-javac
/**
* Create a symbol for a class that implements a given functional interface
* and overrides its functional descriptor. This routine is used for two
* main purposes: (i) checking well-formedness of a functional interface;
* (ii) perform functional interface bridge calculation.
*/
public ClassSymbol makeFunctionalInterfaceClass(Env<AttrContext> env, Name name, List<Type> targets, long cflags) {
if (targets.isEmpty()) {
return null;
}
Symbol descSym = findDescriptorSymbol(targets.head.tsym);
Type descType = findDescriptorType(targets.head);
ClassSymbol csym = new ClassSymbol(cflags, name, env.enclClass.sym.outermostClass());
csym.completer = null;
csym.members_field = new Scope(csym);
MethodSymbol instDescSym = new MethodSymbol(descSym.flags(), descSym.name, descType, csym);
csym.members_field.enter(instDescSym);
Type.ClassType ctype = new Type.ClassType(Type.noType, List.<Type>nil(), csym);
ctype.supertype_field = syms.objectType;
ctype.interfaces_field = targets;
csym.type = ctype;
csym.sourcefile = ((ClassSymbol)csym.owner).sourcefile;
return csym;
}
代码示例来源:origin: org.kohsuke.sorcerer/sorcerer-javac
/**
* Create a symbol for a class that implements a given functional interface
* and overrides its functional descriptor. This routine is used for two
* main purposes: (i) checking well-formedness of a functional interface;
* (ii) perform functional interface bridge calculation.
*/
public ClassSymbol makeFunctionalInterfaceClass(Env<AttrContext> env, Name name, List<Type> targets, long cflags) {
if (targets.isEmpty() || !isFunctionalInterface(targets.head)) {
return null;
}
Symbol descSym = findDescriptorSymbol(targets.head.tsym);
Type descType = findDescriptorType(targets.head);
ClassSymbol csym = new ClassSymbol(cflags, name, env.enclClass.sym.outermostClass());
csym.completer = null;
csym.members_field = new Scope(csym);
MethodSymbol instDescSym = new MethodSymbol(descSym.flags(), descSym.name, descType, csym);
csym.members_field.enter(instDescSym);
Type.ClassType ctype = new Type.ClassType(Type.noType, List.<Type>nil(), csym);
ctype.supertype_field = syms.objectType;
ctype.interfaces_field = targets;
csym.type = ctype;
csym.sourcefile = ((ClassSymbol)csym.owner).sourcefile;
return csym;
}
代码示例来源:origin: konsoletyper/teavm-javac
/**
* Find the minimal set of methods that are overridden by the functional
* descriptor in 'origin'. All returned methods are assumed to have different
* erased signatures.
*/
public List<Symbol> functionalInterfaceBridges(TypeSymbol origin) {
Assert.check(isFunctionalInterface(origin));
Symbol descSym = findDescriptorSymbol(origin);
CompoundScope members = membersClosure(origin.type, false);
ListBuffer<Symbol> overridden = new ListBuffer<>();
outer: for (Symbol m2 : members.getElementsByName(descSym.name, bridgeFilter)) {
if (m2 == descSym) continue;
else if (descSym.overrides(m2, origin, Types.this, false)) {
for (Symbol m3 : overridden) {
if (isSameType(m3.erasure(Types.this), m2.erasure(Types.this)) ||
(m3.overrides(m2, origin, Types.this, false) &&
(pendingBridges((ClassSymbol)origin, m3.enclClass()) ||
(((MethodSymbol)m2).binaryImplementation((ClassSymbol)m3.owner, Types.this) != null)))) {
continue outer;
}
}
overridden.add(m2);
}
}
return overridden.toList();
}
//where
代码示例来源:origin: org.kohsuke.sorcerer/sorcerer-javac
/**
* Find the minimal set of methods that are overridden by the functional
* descriptor in 'origin'. All returned methods are assumed to have different
* erased signatures.
*/
public List<Symbol> functionalInterfaceBridges(TypeSymbol origin) {
Assert.check(isFunctionalInterface(origin));
Symbol descSym = findDescriptorSymbol(origin);
CompoundScope members = membersClosure(origin.type, false);
ListBuffer<Symbol> overridden = new ListBuffer<>();
outer: for (Symbol m2 : members.getElementsByName(descSym.name, bridgeFilter)) {
if (m2 == descSym) continue;
else if (descSym.overrides(m2, origin, Types.this, false)) {
for (Symbol m3 : overridden) {
if (isSameType(m3.erasure(Types.this), m2.erasure(Types.this)) ||
(m3.overrides(m2, origin, Types.this, false) &&
(pendingBridges((ClassSymbol)origin, m3.enclClass()) ||
(((MethodSymbol)m2).binaryImplementation((ClassSymbol)m3.owner, Types.this) != null)))) {
continue outer;
}
}
overridden.add(m2);
}
}
return overridden.toList();
}
//where
代码示例来源:origin: org.kohsuke.sorcerer/sorcerer-javac
JCFunctionalExpression tree = context.tree;
MethodSymbol samSym = (MethodSymbol) types.findDescriptorSymbol(tree.type.tsym);
List<Object> staticArgs = List.<Object>of(
typeToMethodType(samSym.type),
内容来源于网络,如有侵权,请联系作者删除!