use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.
the class UTemplater method visitMethodInvocation.
@Override
public UExpression visitMethodInvocation(MethodInvocationTree tree, Void v) {
if (anyMatch(ANY_OF, tree.getMethodSelect(), new Unifier(context))) {
return UAnyOf.create(templateExpressions(tree.getArguments()));
} else if (anyMatch(IS_INSTANCE, tree.getMethodSelect(), new Unifier(context))) {
return UInstanceOf.create(template(Iterables.getOnlyElement(tree.getArguments())), templateType(getSingleExplicitTypeArgument(tree)));
} else if (anyMatch(CLAZZ, tree.getMethodSelect(), new Unifier(context))) {
Tree typeArg = getSingleExplicitTypeArgument(tree);
return UMemberSelect.create(templateType(typeArg), "class", UClassType.create("java.lang.Class", template(((JCTree) typeArg).type)));
} else if (anyMatch(NEW_ARRAY, tree.getMethodSelect(), new Unifier(context))) {
Tree typeArg = getSingleExplicitTypeArgument(tree);
ExpressionTree lengthArg = Iterables.getOnlyElement(tree.getArguments());
return UNewArray.create(templateType(typeArg), ImmutableList.of(template(lengthArg)), null);
} else if (anyMatch(ENUM_VALUE_OF, tree.getMethodSelect(), new Unifier(context))) {
Tree typeArg = getSingleExplicitTypeArgument(tree);
ExpressionTree strArg = Iterables.getOnlyElement(tree.getArguments());
return UMethodInvocation.create(UMemberSelect.create(templateType(typeArg), "valueOf", UMethodType.create(template(((JCTree) typeArg).type), UClassType.create("java.lang.String"))), template(strArg));
} else if (anyMatch(AS_VARARGS, tree.getMethodSelect(), new Unifier(context))) {
ExpressionTree arg = Iterables.getOnlyElement(tree.getArguments());
checkArgument(ASTHelpers.hasAnnotation(arg, Repeated.class, new VisitorState(context)));
return template(arg);
}
Map<MethodSymbol, PlaceholderMethod> placeholderMethods = context.get(RefasterRuleBuilderScanner.PLACEHOLDER_METHODS_KEY);
if (placeholderMethods != null && placeholderMethods.containsKey(ASTHelpers.getSymbol(tree))) {
return UPlaceholderExpression.create(placeholderMethods.get(ASTHelpers.getSymbol(tree)), templateExpressions(tree.getArguments()));
} else {
return UMethodInvocation.create(template(tree.getMethodSelect()), templateExpressions(tree.getArguments()));
}
}
use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.
the class UTemplater method createTemplate.
/**
* Returns a template based on a method. One-line methods starting with a {@code return} statement
* are guessed to be expression templates, and all other methods are guessed to be block
* templates.
*/
public static Template<?> createTemplate(Context context, MethodTree decl) {
MethodSymbol declSym = ASTHelpers.getSymbol(decl);
ImmutableClassToInstanceMap<Annotation> annotations = UTemplater.annotationMap(declSym);
ImmutableMap<String, VarSymbol> freeExpressionVars = freeExpressionVariables(decl);
Context subContext = new SubContext(context);
final UTemplater templater = new UTemplater(freeExpressionVars, subContext);
ImmutableMap<String, UType> expressionVarTypes = ImmutableMap.copyOf(Maps.transformValues(freeExpressionVars, new Function<VarSymbol, UType>() {
@Override
public UType apply(VarSymbol sym) {
return templater.template(sym.type);
}
}));
UType genericType = templater.template(declSym.type);
List<UTypeVar> typeParameters;
UMethodType methodType;
if (genericType instanceof UForAll) {
UForAll forAllType = (UForAll) genericType;
typeParameters = forAllType.getTypeVars();
methodType = (UMethodType) forAllType.getQuantifiedType();
} else if (genericType instanceof UMethodType) {
typeParameters = ImmutableList.of();
methodType = (UMethodType) genericType;
} else {
throw new IllegalArgumentException("Expected genericType to be either a ForAll or a UMethodType, but was " + genericType);
}
List<? extends StatementTree> bodyStatements = decl.getBody().getStatements();
if (bodyStatements.size() == 1 && Iterables.getOnlyElement(bodyStatements).getKind() == Kind.RETURN && context.get(REQUIRE_BLOCK_KEY) == null) {
ExpressionTree expression = ((ReturnTree) Iterables.getOnlyElement(bodyStatements)).getExpression();
return ExpressionTemplate.create(annotations, typeParameters, expressionVarTypes, templater.template(expression), methodType.getReturnType());
} else {
List<UStatement> templateStatements = new ArrayList<>();
for (StatementTree statement : bodyStatements) {
templateStatements.add(templater.template(statement));
}
return BlockTemplate.create(annotations, typeParameters, expressionVarTypes, templateStatements);
}
}
use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.
the class Template method infer.
/**
* Returns the inferred method type of the template based on the given actual argument types.
*
* @throws InferException if no instances of the specified type variables would allow the
* {@code actualArgTypes} to match the {@code expectedArgTypes}
*/
private Type infer(Warner warner, Inliner inliner, List<Type> freeTypeVariables, List<Type> expectedArgTypes, Type returnType, List<Type> actualArgTypes) throws InferException {
Symtab symtab = inliner.symtab();
Type methodType = new MethodType(expectedArgTypes, returnType, List.<Type>nil(), symtab.methodClass);
if (!freeTypeVariables.isEmpty()) {
methodType = new ForAll(freeTypeVariables, methodType);
}
Enter enter = inliner.enter();
MethodSymbol methodSymbol = new MethodSymbol(0, inliner.asName("__m__"), methodType, symtab.unknownSymbol);
Type site = symtab.methodClass.type;
Env<AttrContext> env = enter.getTopLevelEnv(TreeMaker.instance(inliner.getContext()).TopLevel(List.<JCTree>nil()));
// Set up the resolution phase:
try {
Field field = AttrContext.class.getDeclaredField("pendingResolutionPhase");
field.setAccessible(true);
field.set(env.info, newMethodResolutionPhase(autoboxing()));
} catch (ReflectiveOperationException e) {
throw new LinkageError(e.getMessage(), e);
}
Object resultInfo;
try {
Class<?> resultInfoClass = Class.forName("com.sun.tools.javac.comp.Attr$ResultInfo");
Constructor<?> resultInfoCtor = resultInfoClass.getDeclaredConstructor(Attr.class, KindSelector.class, Type.class);
resultInfoCtor.setAccessible(true);
resultInfo = resultInfoCtor.newInstance(Attr.instance(inliner.getContext()), KindSelector.PCK, Type.noType);
} catch (ReflectiveOperationException e) {
throw new LinkageError(e.getMessage(), e);
}
// Type inference sometimes produces diagnostics, so we need to catch them to avoid interfering
// with the enclosing compilation.
Log.DeferredDiagnosticHandler handler = new Log.DeferredDiagnosticHandler(Log.instance(inliner.getContext()));
try {
MethodType result = callCheckMethod(warner, inliner, resultInfo, actualArgTypes, methodSymbol, site, env);
if (!handler.getDiagnostics().isEmpty()) {
throw new InferException(handler.getDiagnostics());
}
return result;
} finally {
Log.instance(inliner.getContext()).popDiagnosticHandler(handler);
}
}
use of com.sun.tools.javac.code.Symbol.MethodSymbol in project ceylon-compiler by ceylon.
the class Attr method getSyntheticScopeMapping.
/** Creates a synthetic scope containing fake generic constructors.
* Assuming that the original scope contains a constructor of the kind:
* Foo(X x, Y y), where X,Y are class type-variables declared in Foo,
* the synthetic scope is added a generic constructor of the kind:
* <X,Y>Foo<X,Y>(X x, Y y). This is crucial in order to enable diamond
* inference. The inferred return type of the synthetic constructor IS
* the inferred type for the diamond operator.
*/
private Pair<Scope, Scope> getSyntheticScopeMapping(Type ctype) {
if (ctype.tag != CLASS) {
return erroneousMapping;
}
Pair<Scope, Scope> mapping = new Pair<Scope, Scope>(ctype.tsym.members(), new Scope(ctype.tsym));
//declared, and insert it into the new scope.
for (Scope.Entry e = mapping.fst.lookup(names.init); e.scope != null; e = e.next()) {
Type synthRestype = new ClassType(ctype.getEnclosingType(), ctype.tsym.type.getTypeArguments(), ctype.tsym);
MethodSymbol synhConstr = new MethodSymbol(e.sym.flags(), names.init, types.createMethodTypeWithReturn(e.sym.type, synthRestype), e.sym.owner);
mapping.snd.enter(synhConstr);
}
return mapping;
}
use of com.sun.tools.javac.code.Symbol.MethodSymbol in project ceylon-compiler by ceylon.
the class TypeVariableImpl method owner.
/**
* Return the class, interface, method, or constructor within
* which this type variable is declared.
*/
public ProgramElementDoc owner() {
Symbol osym = type.tsym.owner;
if ((osym.kind & Kinds.TYP) != 0) {
return env.getClassDoc((ClassSymbol) osym);
}
Names names = osym.name.table.names;
if (osym.name == names.init) {
return env.getConstructorDoc((MethodSymbol) osym);
} else {
return env.getMethodDoc((MethodSymbol) osym);
}
}
Aggregations