use of com.sun.source.tree.MethodTree in project checker-framework by typetools.
the class GuiEffectVisitor method visitReturn.
// Returning a lambda expression also triggers inference based on the method's return type.
@Override
public Void visitReturn(ReturnTree node, Void p) {
Void result = super.visitReturn(node, p);
if (node.getExpression() != null && node.getExpression().getKind() == Tree.Kind.LAMBDA_EXPRESSION) {
// Unfortunatelly, need to duplicate a fair bit of BaseTypeVisitor.visitReturn after lambdas have been
// inferred.
Pair<Tree, AnnotatedTypeMirror> preAssCtxt = visitorState.getAssignmentContext();
try {
Tree enclosing = TreeUtils.enclosingMethodOrLambda(getCurrentPath());
AnnotatedTypeMirror ret = null;
if (enclosing.getKind() == Tree.Kind.METHOD) {
MethodTree enclosingMethod = (MethodTree) enclosing;
boolean valid = validateTypeOf(enclosing);
if (valid) {
ret = atypeFactory.getMethodReturnType(enclosingMethod, node);
}
} else {
ret = atypeFactory.getFnInterfaceFromTree((LambdaExpressionTree) enclosing).second.getReturnType();
}
if (ret != null) {
visitorState.setAssignmentContext(Pair.of((Tree) node, ret));
lambdaAssignmentCheck(ret, (LambdaExpressionTree) node.getExpression(), "return.type.incompatible");
}
} finally {
visitorState.setAssignmentContext(preAssCtxt);
}
}
return result;
}
use of com.sun.source.tree.MethodTree in project st-js by st-js.
the class ClassWriter method getAnnotationDescription.
/**
* build the annotation description element
*
* <pre>
* $annotations : {
* _: {....}
* field1: {...}
* method1: {...}
* method1$0: {...}
* method1$1: {...}...
* }
* </pre>
*
* for each annotation list you have:
*
* <pre>
* {
* "annotationType1": [expr1, expr2, expr3],
* "annotationType2": []
* }
* </pre>
*/
private JS getAnnotationDescription(WriterVisitor<JS> visitor, ClassTree classTree, GenerationContext<JS> context) {
List<NameValue<JS>> props = new ArrayList<NameValue<JS>>();
addAnnotationsForElement("_", props, visitor, classTree.getModifiers().getAnnotations(), context);
for (Tree member : classTree.getMembers()) {
if (MemberWriters.shouldSkip(context.getCurrentWrapper().child(member))) {
continue;
}
if (member instanceof VariableTree) {
VariableTree field = (VariableTree) member;
addAnnotationsForElement(field.getName().toString(), props, visitor, field.getModifiers().getAnnotations(), context);
} else if (member instanceof MethodTree) {
addAnnotationsForMethod((MethodTree) member, props, visitor, context);
}
}
return context.js().object(props);
}
use of com.sun.source.tree.MethodTree in project st-js by st-js.
the class NewClassWriter method getInlineFunctionDeclaration.
/**
* check by {@link org.stjs.generator.check.expression.NewClassInlineFunctionCheck} generate the code for inline
* functions:
*
* <pre>
* new FunctionInterface(){
* public void $invoke(args){
* }
* }
* </pre>
*
* is transformed in
*
* <pre>
* function(args){
* }
* </pre>
*/
private JS getInlineFunctionDeclaration(WriterVisitor<JS> visitor, TreeWrapper<NewClassTree, JS> tw) {
// special construction for inline function definition
if (!tw.child(tw.getTree().getIdentifier()).isJavaScriptFunction()) {
return null;
}
// the check verifies the existence of a single method (first is the generated
// constructor)
Tree method = tw.getTree().getClassBody().getMembers().get(1);
JS func = visitor.scan(method, tw.getContext());
int specialThisParamPos = MethodWriter.getTHISParamPos(((MethodTree) method).getParameters());
// accessOuterScope(visitor, tree, context) ||
if (specialThisParamPos >= 0) {
// bind for inline functions accessing the outher scope or with special this
JavaScriptBuilder<JS> js = tw.getContext().js();
JS target = js.keyword(Keyword.THIS);
JS stjsBind = js.property(js.name("stjs"), "bind");
return js.functionCall(stjsBind, Arrays.asList(target, func, js.number(specialThisParamPos)));
}
return func;
}
use of com.sun.source.tree.MethodTree in project st-js by st-js.
the class MethodOverloadCheck method visit.
/**
* {@inheritDoc}
*/
@Override
public Void visit(CheckVisitor visitor, MethodTree tree, GenerationContext<Void> context) {
ExecutableElement methodElement = TreeUtils.elementFromDeclaration(tree);
if (JavaNodes.isNative(methodElement)) {
// no need to check the native ones - only the one with the body
return null;
}
TreeWrapper<Tree, Void> tw = context.getCurrentWrapper();
if (MemberWriters.shouldSkip(tw)) {
return null;
}
boolean hasVarArgs = tree.getParameters().size() == 1 && InternalUtils.isVarArg(tree.getParameters().get(0));
TypeElement typeElement = (TypeElement) methodElement.getEnclosingElement();
// for constructors take only the class's other constructors. For regular methods, checks agains all the methods
// in the class' hierarchy
List<? extends Element> allMembers = methodElement.getKind() == ElementKind.CONSTRUCTOR ? typeElement.getEnclosedElements() : context.getElements().getAllMembers(typeElement);
for (Element memberElement : allMembers) {
if (memberElement.getAnnotation(ServerSide.class) == null) {
checkMember(memberElement, methodElement, tree, context, hasVarArgs);
}
}
return null;
}
use of com.sun.source.tree.MethodTree in project error-prone by google.
the class ASTHelpersTest method testHasDirectAnnotationWithSimpleName.
@Test
public void testHasDirectAnnotationWithSimpleName() {
writeFile(//
"A.java", "public class A {", " @Deprecated public void doIt() {}", "}");
TestScanner scanner = new TestScanner() {
@Override
public Void visitMethod(MethodTree tree, VisitorState state) {
if (tree.getName().contentEquals("doIt")) {
setAssertionsComplete();
Symbol sym = ASTHelpers.getSymbol(tree);
assertThat(ASTHelpers.hasDirectAnnotationWithSimpleName(sym, "Deprecated")).isTrue();
assertThat(ASTHelpers.hasDirectAnnotationWithSimpleName(sym, "Nullable")).isFalse();
}
return super.visitMethod(tree, state);
}
};
tests.add(scanner);
assertCompiles(scanner);
}
Aggregations