Search in sources :

Example 6 with AnonymousClassDeclaration

use of org.eclipse.jdt.core.dom.AnonymousClassDeclaration in project che by eclipse.

the class LambdaExpressionsFix method isFunctionalAnonymous.

static boolean isFunctionalAnonymous(ClassInstanceCreation node) {
    ITypeBinding typeBinding = node.resolveTypeBinding();
    if (typeBinding == null)
        return false;
    ITypeBinding[] interfaces = typeBinding.getInterfaces();
    if (interfaces.length != 1)
        return false;
    if (interfaces[0].getFunctionalInterfaceMethod() == null)
        return false;
    AnonymousClassDeclaration anonymTypeDecl = node.getAnonymousClassDeclaration();
    if (anonymTypeDecl == null || anonymTypeDecl.resolveBinding() == null)
        return false;
    List<BodyDeclaration> bodyDeclarations = anonymTypeDecl.bodyDeclarations();
    // cannot convert if there are fields or additional methods
    if (bodyDeclarations.size() != 1)
        return false;
    BodyDeclaration bodyDeclaration = bodyDeclarations.get(0);
    if (!(bodyDeclaration instanceof MethodDeclaration))
        return false;
    MethodDeclaration methodDecl = (MethodDeclaration) bodyDeclaration;
    IMethodBinding methodBinding = methodDecl.resolveBinding();
    if (methodBinding == null)
        return false;
    // generic lambda expressions are not allowed
    if (methodBinding.isGenericMethod())
        return false;
    // lambda cannot refer to 'this'/'super' literals
    if (SuperThisReferenceFinder.hasReference(methodDecl))
        return false;
    if (!isInTargetTypeContext(node))
        return false;
    return true;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration)

Example 7 with AnonymousClassDeclaration

use of org.eclipse.jdt.core.dom.AnonymousClassDeclaration in project buck by facebook.

the class JavaFileParser method getFullyQualifiedTypeName.

@Nullable
private String getFullyQualifiedTypeName(AbstractTypeDeclaration node) {
    LinkedList<String> nameParts = Lists.newLinkedList();
    nameParts.add(node.getName().toString());
    ASTNode parent = node.getParent();
    while (!(parent instanceof CompilationUnit)) {
        if (parent instanceof AbstractTypeDeclaration) {
            nameParts.addFirst(((AbstractTypeDeclaration) parent).getName().toString());
            parent = parent.getParent();
        } else if (parent instanceof AnonymousClassDeclaration) {
            // name.
            return null;
        } else {
            throw new RuntimeException("Unexpected parent " + parent + " for " + node);
        }
    }
    // A Java file might not have a package. Hopefully all of ours do though...
    PackageDeclaration packageDecl = ((CompilationUnit) parent).getPackage();
    if (packageDecl != null) {
        nameParts.addFirst(packageDecl.getName().toString());
    }
    return Joiner.on(".").join(nameParts);
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) PackageDeclaration(org.eclipse.jdt.core.dom.PackageDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) Nullable(javax.annotation.Nullable)

Example 8 with AnonymousClassDeclaration

use of org.eclipse.jdt.core.dom.AnonymousClassDeclaration in project AutoRefactor by JnRouvignac.

the class RemoveUnneededThisExpressionRefactoring method thisExpressionRefersToEnclosingType.

private static boolean thisExpressionRefersToEnclosingType(Name thisQualifierName, ASTNode node) {
    if (thisQualifierName == null) {
        return true;
    }
    final ASTNode enclosingType = getEnclosingType(node);
    if (enclosingType instanceof AnonymousClassDeclaration) {
        return false;
    }
    final AbstractTypeDeclaration ancestor = (AbstractTypeDeclaration) enclosingType;
    if (thisQualifierName instanceof SimpleName) {
        return isEqual((SimpleName) thisQualifierName, ancestor.getName());
    } else if (thisQualifierName instanceof QualifiedName) {
        final QualifiedName qn = (QualifiedName) thisQualifierName;
        return isEqual(qn.getName(), ancestor.getName()) && thisExpressionRefersToEnclosingType(qn.getQualifier(), ancestor);
    }
    throw new NotImplementedException(thisQualifierName);
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) NotImplementedException(org.autorefactor.util.NotImplementedException) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 9 with AnonymousClassDeclaration

use of org.eclipse.jdt.core.dom.AnonymousClassDeclaration in project AutoRefactor by JnRouvignac.

the class RemoveUnneededThisExpressionRefactoring method isCallingMethodDeclaredInEnclosingType.

private boolean isCallingMethodDeclaredInEnclosingType(MethodInvocation node) {
    final ASTNode currentType = getEnclosingType(node);
    final IMethodBinding mb = node.resolveMethodBinding();
    if (currentType instanceof AnonymousClassDeclaration) {
        final AnonymousClassDeclaration c = (AnonymousClassDeclaration) currentType;
        final ITypeBinding enclosingTypeBinding = c.resolveBinding();
        return enclosingTypeBinding.isSubTypeCompatible(mb.getDeclaringClass());
    } else if (currentType instanceof AbstractTypeDeclaration) {
        final AbstractTypeDeclaration ed = (AbstractTypeDeclaration) currentType;
        final ITypeBinding enclosingTypeBinding = ed.resolveBinding();
        return enclosingTypeBinding.isSubTypeCompatible(mb.getDeclaringClass());
    }
    throw new NotImplementedException(node, node);
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) NotImplementedException(org.autorefactor.util.NotImplementedException) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 10 with AnonymousClassDeclaration

use of org.eclipse.jdt.core.dom.AnonymousClassDeclaration in project che by eclipse.

the class IntroduceIndirectionRefactoring method typeToBodyDeclarationProperty.

private ChildListPropertyDescriptor typeToBodyDeclarationProperty(IType type, CompilationUnit root) throws JavaModelException {
    ASTNode typeDeclaration = typeToDeclaration(type, root);
    if (typeDeclaration instanceof AbstractTypeDeclaration)
        return ((AbstractTypeDeclaration) typeDeclaration).getBodyDeclarationsProperty();
    else if (typeDeclaration instanceof AnonymousClassDeclaration)
        return AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY;
    Assert.isTrue(false);
    return null;
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Aggregations

AnonymousClassDeclaration (org.eclipse.jdt.core.dom.AnonymousClassDeclaration)22 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)17 ASTNode (org.eclipse.jdt.core.dom.ASTNode)12 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)10 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)8 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)7 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)4 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)4 SimpleName (org.eclipse.jdt.core.dom.SimpleName)4 NotImplementedException (org.autorefactor.util.NotImplementedException)3 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)3 EnumDeclaration (org.eclipse.jdt.core.dom.EnumDeclaration)3 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)3 StructuralPropertyDescriptor (org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)3 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)3 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)3 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)3 CodeGenerationSettings (org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings)3 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)3