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;
}
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);
}
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);
}
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);
}
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;
}
Aggregations