public static Optional<GroovyClosure> asClosure(ASTNodeFactory nodeFactory, GroovyClosureBuilder groovyClosureBuilder, Expression expression, String methodName) { if (expression instanceof ClassInstanceCreation) { ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) expression; if (classInstanceCreation.getAnonymousClassDeclaration() != null) { AnonymousClassDeclaration classDeclaration = classInstanceCreation.getAnonymousClassDeclaration(); if (classDeclaration.bodyDeclarations().size() == 1 && classDeclaration.bodyDeclarations().get(0) instanceof MethodDeclaration && ((MethodDeclaration) classDeclaration.bodyDeclarations().get(0)) .getName().getIdentifier().equals(methodName)) { MethodDeclaration methodDeclaration = (MethodDeclaration) classDeclaration.bodyDeclarations().get(0); List<Statement> statements = nodeFactory.clone(methodDeclaration.getBody()).statements(); GroovyClosure closure = groovyClosureBuilder.aClosure() .withBodyStatements(statements) .withTypeLiteral(nodeFactory.typeLiteral(type(nodeFactory, classInstanceCreation))) .withArgument(nodeFactory.clone((SingleVariableDeclaration) methodDeclaration.parameters().get(0))) .build(); return Optional.of(closure); } } } return empty(); }
/** * Returns the type binding of the node's type context or null if the node is inside * an annotation, type parameter, super type declaration, or Javadoc of a top level type. * The result of this method is equal to the result of {@link #getBindingOfParentType(ASTNode)} for nodes in the type's body. * * @param node an AST node * @return the type binding of the node's parent type context, or <code>null</code> */ public static ITypeBinding getBindingOfParentTypeContext(ASTNode node) { StructuralPropertyDescriptor lastLocation= null; while (node != null) { if (node instanceof AbstractTypeDeclaration) { AbstractTypeDeclaration decl= (AbstractTypeDeclaration) node; if (lastLocation == decl.getBodyDeclarationsProperty() || lastLocation == decl.getJavadocProperty()) { return decl.resolveBinding(); } else if (decl instanceof EnumDeclaration && lastLocation == EnumDeclaration.ENUM_CONSTANTS_PROPERTY) { return decl.resolveBinding(); } } else if (node instanceof AnonymousClassDeclaration) { return ((AnonymousClassDeclaration) node).resolveBinding(); } lastLocation= node.getLocationInParent(); node= node.getParent(); } return null; }
public static IBinding getEnclosingDeclaration(ASTNode node) { while(node != null) { if (node instanceof AbstractTypeDeclaration) { return ((AbstractTypeDeclaration)node).resolveBinding(); } else if (node instanceof AnonymousClassDeclaration) { return ((AnonymousClassDeclaration)node).resolveBinding(); } else if (node instanceof MethodDeclaration) { return ((MethodDeclaration)node).resolveBinding(); } else if (node instanceof FieldDeclaration) { List<?> fragments= ((FieldDeclaration)node).fragments(); if (fragments.size() > 0) { return ((VariableDeclarationFragment)fragments.get(0)).resolveBinding(); } } else if (node instanceof VariableDeclarationFragment) { IVariableBinding variableBinding= ((VariableDeclarationFragment)node).resolveBinding(); if (variableBinding.getDeclaringMethod() != null || variableBinding.getDeclaringClass() != null) { return variableBinding; // workaround for incomplete wiring of DOM bindings: keep searching when variableBinding is unparented } } node= node.getParent(); } return null; }
private static boolean isTypeBindingNull(ASTNode typeNode) { if (typeNode instanceof AbstractTypeDeclaration) { AbstractTypeDeclaration abstractTypeDeclaration= (AbstractTypeDeclaration) typeNode; if (abstractTypeDeclaration.resolveBinding() == null) { return true; } return false; } else if (typeNode instanceof AnonymousClassDeclaration) { AnonymousClassDeclaration anonymousClassDeclaration= (AnonymousClassDeclaration) typeNode; if (anonymousClassDeclaration.resolveBinding() == null) { return true; } return false; } else if (typeNode instanceof EnumConstantDeclaration) { return false; } else { return true; } }
@Override public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel positionGroups) throws CoreException { final ASTRewrite rewrite = cuRewrite.getASTRewrite(); VariableDeclarationFragment fragment = null; for (int i = 0; i < fNodes.length; i++) { final ASTNode node = fNodes[i]; final AST ast = node.getAST(); fragment = ast.newVariableDeclarationFragment(); fragment.setName(ast.newSimpleName(NAME_FIELD)); final FieldDeclaration declaration = ast.newFieldDeclaration(fragment); declaration.setType(ast.newPrimitiveType(PrimitiveType.LONG)); declaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL)); if (!addInitializer(fragment, node)) { continue; } if (fragment.getInitializer() != null) { final TextEditGroup editGroup = createTextEditGroup(FixMessages.SerialVersion_group_description, cuRewrite); if (node instanceof AbstractTypeDeclaration) { rewrite.getListRewrite(node, ((AbstractTypeDeclaration) node).getBodyDeclarationsProperty()).insertAt(declaration, 0, editGroup); } else if (node instanceof AnonymousClassDeclaration) { rewrite.getListRewrite(node, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY).insertAt(declaration, 0, editGroup); } else if (node instanceof ParameterizedType) { final ParameterizedType type = (ParameterizedType) node; final ASTNode parent = type.getParent(); if (parent instanceof ClassInstanceCreation) { final ClassInstanceCreation creation = (ClassInstanceCreation) parent; final AnonymousClassDeclaration anonymous = creation.getAnonymousClassDeclaration(); if (anonymous != null) { rewrite.getListRewrite(anonymous, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY).insertAt(declaration, 0, editGroup); } } } else { Assert.isTrue(false); } addLinkedPositions(rewrite, fragment, positionGroups); } final String comment = CodeGeneration.getFieldComment(fUnit, declaration.getType().toString(), NAME_FIELD, "\n" /* StubUtility.getLineDelimiterUsed(fUnit) */); if (comment != null && comment.length() > 0 && !"/**\n *\n */\n".equals(comment)) { final Javadoc doc = (Javadoc) rewrite.createStringPlaceholder(comment, ASTNode.JAVADOC); declaration.setJavadoc(doc); } } if (fragment == null) { return; } positionGroups.setEndPosition(rewrite.track(fragment)); }
public static List<Match> perform(ASTNode start, ASTNode[] snippet) { Assert.isTrue(start instanceof AbstractTypeDeclaration || start instanceof AnonymousClassDeclaration); SnippetFinder finder = new SnippetFinder(snippet); start.accept(finder); for (Iterator<Match> iter = finder.fResult.iterator(); iter.hasNext();) { Match match = iter.next(); ASTNode[] nodes = match.getNodes(); // doesn't match if the candidate is the left hand side of an // assignment and the snippet consists of a single node. // Otherwise y= i; i= z; results in y= e(); e()= z; if (nodes.length == 1 && isLeftHandSideOfAssignment(nodes[0])) { iter.remove(); } } return finder.fResult; }
/** * Returns the type binding of the node's type context or null if the node is inside an * annotation, type parameter, super type declaration, or Javadoc of a top level type. The result * of this method is equal to the result of {@link #getBindingOfParentType(ASTNode)} for nodes in * the type's body. * * @param node an AST node * @return the type binding of the node's parent type context, or <code>null</code> */ public static ITypeBinding getBindingOfParentTypeContext(ASTNode node) { StructuralPropertyDescriptor lastLocation = null; while (node != null) { if (node instanceof AbstractTypeDeclaration) { AbstractTypeDeclaration decl = (AbstractTypeDeclaration) node; if (lastLocation == decl.getBodyDeclarationsProperty() || lastLocation == decl.getJavadocProperty()) { return decl.resolveBinding(); } else if (decl instanceof EnumDeclaration && lastLocation == EnumDeclaration.ENUM_CONSTANTS_PROPERTY) { return decl.resolveBinding(); } } else if (node instanceof AnonymousClassDeclaration) { return ((AnonymousClassDeclaration) node).resolveBinding(); } lastLocation = node.getLocationInParent(); node = node.getParent(); } return null; }
private boolean addOuterDeclarationsForLocalType( ITypeBinding localBinding, int flags, IBindingRequestor requestor) { ASTNode node = fRoot.findDeclaringNode(localBinding); if (node == null) { return false; } if (node instanceof AbstractTypeDeclaration || node instanceof AnonymousClassDeclaration) { if (addLocalDeclarations(node.getParent(), flags, requestor)) return true; ITypeBinding parentTypeBinding = Bindings.getBindingOfParentType(node.getParent()); if (parentTypeBinding != null) { if (addTypeDeclarations(parentTypeBinding, flags, requestor)) return true; } } return false; }
private static boolean isTypeBindingNull(ASTNode typeNode) { if (typeNode instanceof AbstractTypeDeclaration) { AbstractTypeDeclaration abstractTypeDeclaration = (AbstractTypeDeclaration) typeNode; if (abstractTypeDeclaration.resolveBinding() == null) return true; return false; } else if (typeNode instanceof AnonymousClassDeclaration) { AnonymousClassDeclaration anonymousClassDeclaration = (AnonymousClassDeclaration) typeNode; if (anonymousClassDeclaration.resolveBinding() == null) return true; return false; } else if (typeNode instanceof EnumConstantDeclaration) { return false; } else { return true; } }
/** {@inheritDoc} */ @Override public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel positionGroups) throws CoreException { final ASTRewrite rewrite = cuRewrite.getASTRewrite(); VariableDeclarationFragment fragment = null; for (int i = 0; i < fNodes.length; i++) { final ASTNode node = fNodes[i]; final AST ast = node.getAST(); fragment = ast.newVariableDeclarationFragment(); fragment.setName(ast.newSimpleName(NAME_FIELD)); final FieldDeclaration declaration = ast.newFieldDeclaration(fragment); declaration.setType(ast.newPrimitiveType(PrimitiveType.LONG)); declaration .modifiers() .addAll( ASTNodeFactory.newModifiers( ast, Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL)); if (!addInitializer(fragment, node)) continue; if (fragment.getInitializer() != null) { final TextEditGroup editGroup = createTextEditGroup(FixMessages.SerialVersion_group_description, cuRewrite); if (node instanceof AbstractTypeDeclaration) rewrite .getListRewrite(node, ((AbstractTypeDeclaration) node).getBodyDeclarationsProperty()) .insertAt(declaration, 0, editGroup); else if (node instanceof AnonymousClassDeclaration) rewrite .getListRewrite(node, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY) .insertAt(declaration, 0, editGroup); else if (node instanceof ParameterizedType) { final ParameterizedType type = (ParameterizedType) node; final ASTNode parent = type.getParent(); if (parent instanceof ClassInstanceCreation) { final ClassInstanceCreation creation = (ClassInstanceCreation) parent; final AnonymousClassDeclaration anonymous = creation.getAnonymousClassDeclaration(); if (anonymous != null) rewrite .getListRewrite(anonymous, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY) .insertAt(declaration, 0, editGroup); } } else Assert.isTrue(false); addLinkedPositions(rewrite, fragment, positionGroups); } final String comment = CodeGeneration.getFieldComment( fUnit, declaration.getType().toString(), NAME_FIELD, StubUtility.getLineDelimiterUsed(fUnit)); if (comment != null && comment.length() > 0) { final Javadoc doc = (Javadoc) rewrite.createStringPlaceholder(comment, ASTNode.JAVADOC); declaration.setJavadoc(doc); } } if (fragment == null) return; positionGroups.setEndPosition(rewrite.track(fragment)); }
public static Match[] perform(ASTNode start, ASTNode[] snippet) { Assert.isTrue( start instanceof AbstractTypeDeclaration || start instanceof AnonymousClassDeclaration); SnippetFinder finder = new SnippetFinder(snippet); start.accept(finder); for (Iterator<Match> iter = finder.fResult.iterator(); iter.hasNext(); ) { Match match = iter.next(); ASTNode[] nodes = match.getNodes(); // doesn't match if the candidate is the left hand side of an // assignment and the snippet consists of a single node. // Otherwise y= i; i= z; results in y= e(); e()= z; if (nodes.length == 1 && isLeftHandSideOfAssignment(nodes[0])) { iter.remove(); } } return finder.fResult.toArray(new Match[finder.fResult.size()]); }
@Override public boolean visit(AnonymousClassDeclaration node) { ITypeBinding binding = node.resolveBinding(); Type type; if (binding != null) type = importer.createTypeFromTypeBinding(binding); else { type = importer.createTypeNamedInUnknownNamespace(""); logNullBinding("anonymous type declaration", node.getParent().toString().replaceAll("\n", " "), ((CompilationUnit) node.getRoot()).getLineNumber(node.getStartPosition())); } importer.ensureTypeFromAnonymousDeclaration(type, node); type.setIsStub(false); importer.createSourceAnchor(type, node, (CompilationUnit) node.getRoot()); importer.pushOnContainerStack(type); return true; }
public static Match[] perform(ASTNode start, ASTNode[] snippet) { Assert.isTrue(start instanceof AbstractTypeDeclaration || start instanceof AnonymousClassDeclaration); SnippetFinder finder= new SnippetFinder(snippet); start.accept(finder); for (Iterator<Match> iter = finder.fResult.iterator(); iter.hasNext();) { Match match = iter.next(); ASTNode[] nodes= match.getNodes(); // doesn't match if the candidate is the left hand side of an // assignment and the snippet consists of a single node. // Otherwise y= i; i= z; results in y= e(); e()= z; if (nodes.length == 1 && isLeftHandSideOfAssignment(nodes[0])) { iter.remove(); } } return finder.fResult.toArray(new Match[finder.fResult.size()]); }
private static boolean isTypeBindingNull(ASTNode typeNode) { if (typeNode instanceof AbstractTypeDeclaration) { AbstractTypeDeclaration abstractTypeDeclaration= (AbstractTypeDeclaration) typeNode; if (abstractTypeDeclaration.resolveBinding() == null) return true; return false; } else if (typeNode instanceof AnonymousClassDeclaration) { AnonymousClassDeclaration anonymousClassDeclaration= (AnonymousClassDeclaration) typeNode; if (anonymousClassDeclaration.resolveBinding() == null) return true; return false; } else if (typeNode instanceof EnumConstantDeclaration) { return false; } else { return true; } }
private String getLabel(ASTNode node) { if (node instanceof AbstractTypeDeclaration) { return ((AbstractTypeDeclaration)node).getName().getIdentifier(); } else if (node instanceof AnonymousClassDeclaration) { if (node.getLocationInParent() == ClassInstanceCreation.ANONYMOUS_CLASS_DECLARATION_PROPERTY) { ClassInstanceCreation creation= (ClassInstanceCreation)node.getParent(); return Messages.format( RefactoringMessages.ExtractMethodInputPage_anonymous_type_label, BasicElementLabels.getJavaElementName(ASTNodes.asString(creation.getType()))); } else if (node.getLocationInParent() == EnumConstantDeclaration.ANONYMOUS_CLASS_DECLARATION_PROPERTY) { EnumConstantDeclaration decl= (EnumConstantDeclaration)node.getParent(); return decl.getName().getIdentifier(); } } return "UNKNOWN"; //$NON-NLS-1$ }
private boolean addOuterDeclarationsForLocalType(ITypeBinding localBinding, int flags, IBindingRequestor requestor) { ASTNode node= fRoot.findDeclaringNode(localBinding); if (node == null) { return false; } if (node instanceof AbstractTypeDeclaration || node instanceof AnonymousClassDeclaration) { if (addLocalDeclarations(node.getParent(), flags, requestor)) return true; ITypeBinding parentTypeBinding= Bindings.getBindingOfParentType(node.getParent()); if (parentTypeBinding != null) { if (addTypeDeclarations(parentTypeBinding, flags, requestor)) return true; } } return false; }
/** * Creates a new add unimplemented methods operation. * * @param astRoot the compilation unit AST node * @param type the type to add the methods to * @param methodsToImplement the method bindings to implement or <code>null</code> to implement all unimplemented methods * @param insertPos the insertion point, or <code>-1</code> * @param imports <code>true</code> if the import edits should be applied, <code>false</code> otherwise * @param apply <code>true</code> if the resulting edit should be applied, <code>false</code> otherwise * @param save <code>true</code> if the changed compilation unit should be saved, <code>false</code> otherwise */ public AddUnimplementedMethodsOperation(CompilationUnit astRoot, ITypeBinding type, IMethodBinding[] methodsToImplement, int insertPos, final boolean imports, final boolean apply, final boolean save) { if (astRoot == null || !(astRoot.getJavaElement() instanceof ICompilationUnit)) { throw new IllegalArgumentException("AST must not be null and has to be created from a ICompilationUnit"); //$NON-NLS-1$ } if (type == null) { throw new IllegalArgumentException("The type must not be null"); //$NON-NLS-1$ } ASTNode node= astRoot.findDeclaringNode(type); if (!(node instanceof AnonymousClassDeclaration || node instanceof AbstractTypeDeclaration)) { throw new IllegalArgumentException("type has to map to a type declaration in the AST"); //$NON-NLS-1$ } fType= type; fInsertPos= insertPos; fASTRoot= astRoot; fMethodsToImplement= methodsToImplement; fSave= save; fApply= apply; fImports= imports; fDoCreateComments= StubUtility.doAddComments(astRoot.getJavaElement().getJavaProject()); }
/** * Creates a new add unimplemented constructors operation. * * @param astRoot the compilation unit AST node * @param type the type to add the methods to * @param constructorsToImplement the method binding keys to implement * @param insertPos the insertion point, or <code>-1</code> * @param imports <code>true</code> if the import edits should be applied, <code>false</code> otherwise * @param apply <code>true</code> if the resulting edit should be applied, <code>false</code> otherwise * @param save <code>true</code> if the changed compilation unit should be saved, <code>false</code> otherwise */ public AddUnimplementedConstructorsOperation(CompilationUnit astRoot, ITypeBinding type, IMethodBinding[] constructorsToImplement, int insertPos, final boolean imports, final boolean apply, final boolean save) { if (astRoot == null || !(astRoot.getJavaElement() instanceof ICompilationUnit)) { throw new IllegalArgumentException("AST must not be null and has to be created from a ICompilationUnit"); //$NON-NLS-1$ } if (type == null) { throw new IllegalArgumentException("The type must not be null"); //$NON-NLS-1$ } ASTNode node= astRoot.findDeclaringNode(type); if (!(node instanceof AnonymousClassDeclaration || node instanceof AbstractTypeDeclaration)) { throw new IllegalArgumentException("type has to map to a type declaration in the AST"); //$NON-NLS-1$ } fType= type; fInsertPos= insertPos; fASTRoot= astRoot; fConstructorsToImplement= constructorsToImplement; fSave= save; fApply= apply; fImports= imports; fCreateComments= StubUtility.doAddComments(astRoot.getJavaElement().getJavaProject()); fVisibility= Modifier.PUBLIC; fOmitSuper= false; }
/** * Finds the ancestor type of <code>node</code> (includes <code>node</code> in the search). * * @param node the node to start the search from, can be <code>null</code> * @param treatModifiersOutside if set, modifiers are not part of their type, but of the type's * parent * @return returns the ancestor type of <code>node</code> (AbstractTypeDeclaration or * AnonymousTypeDeclaration) if any (including <code>node</code>), <code>null</code> * otherwise */ public static ASTNode findParentType(ASTNode node, boolean treatModifiersOutside) { StructuralPropertyDescriptor lastLocation= null; while (node != null) { if (node instanceof AbstractTypeDeclaration) { AbstractTypeDeclaration decl= (AbstractTypeDeclaration) node; if (!treatModifiersOutside || lastLocation != decl.getModifiersProperty()) { return decl; } } else if (node instanceof AnonymousClassDeclaration) { return node; } lastLocation= node.getLocationInParent(); node= node.getParent(); } return null; }
public static ICompilationUnit findCompilationUnitForBinding(ICompilationUnit cu, CompilationUnit astRoot, ITypeBinding binding) throws JavaModelException { if (binding == null || !binding.isFromSource() || binding.isTypeVariable() || binding.isWildcardType()) { return null; } ASTNode node= astRoot.findDeclaringNode(binding.getTypeDeclaration()); if (node == null) { ICompilationUnit targetCU= Bindings.findCompilationUnit(binding, cu.getJavaProject()); if (targetCU != null) { return targetCU; } return null; } else if (node instanceof AbstractTypeDeclaration || node instanceof AnonymousClassDeclaration) { return cu; } return null; }
public boolean visit(AnonymousClassDeclaration node) { ASTNode name; ASTNode parent = node.getParent(); switch (parent.getNodeType()) { case ASTNode.CLASS_INSTANCE_CREATION: name = ((ClassInstanceCreation) parent).getType(); if (name.getNodeType() == ASTNode.PARAMETERIZED_TYPE) { name = ((ParameterizedType) name).getType(); } break; case ASTNode.ENUM_CONSTANT_DECLARATION: name = ((EnumConstantDeclaration) parent).getName(); break; default: return true; } if (found(node, name) && this.resolveBinding) this.foundBinding = node.resolveBinding(); return true; }
private static boolean isDescendantOfAnonymousClassDeclaration(ASTNode node) { while (node != null) { if (node instanceof AnonymousClassDeclaration) { return true; } node = node.getParent(); } return false; }
@Override public boolean visit(AnonymousClassDeclaration node) { if (node.getParent() instanceof ClassInstanceCreation) { ClassInstanceCreation parent = (ClassInstanceCreation) node.getParent(); ITypeBinding typeBinding = parent.getType().resolveBinding(); if (typeBinding != null && typeBinding.isFromSource()) { SDType type = model.createAnonymousType(containerStack.peek(), sourceFilePath, ""); containerStack.push(type); extractSupertypesForPostProcessing(type, typeBinding); return true; } } return false; }
@Override public void endVisit(AnonymousClassDeclaration node) { if (node.getParent() instanceof ClassInstanceCreation) { ClassInstanceCreation parent = (ClassInstanceCreation) node.getParent(); ITypeBinding typeBinding = parent.getType().resolveBinding(); if (typeBinding != null && typeBinding.isFromSource()) { containerStack.pop(); } } }
/** * Returns the type binding of the node's enclosing type declaration. * * @param node an AST node * @return the type binding of the node's parent type declaration, or <code>null</code> */ public static ITypeBinding getBindingOfParentType(ASTNode node) { while (node != null) { if (node instanceof AbstractTypeDeclaration) { return ((AbstractTypeDeclaration) node).resolveBinding(); } else if (node instanceof AnonymousClassDeclaration) { return ((AnonymousClassDeclaration) node).resolveBinding(); } node= node.getParent(); } return null; }
public static List<BodyDeclaration> getBodyDeclarations(ASTNode node) { if (node instanceof AbstractTypeDeclaration) { return ((AbstractTypeDeclaration)node).bodyDeclarations(); } else if (node instanceof AnonymousClassDeclaration) { return ((AnonymousClassDeclaration)node).bodyDeclarations(); } // should not happen. Assert.isTrue(false); return null; }
/** * Returns the structural property descriptor for the "bodyDeclarations" property * of this node (element type: {@link BodyDeclaration}). * * @param node the node, either an {@link AbstractTypeDeclaration} or an {@link AnonymousClassDeclaration} * @return the property descriptor */ public static ChildListPropertyDescriptor getBodyDeclarationsProperty(ASTNode node) { if (node instanceof AbstractTypeDeclaration) { return ((AbstractTypeDeclaration)node).getBodyDeclarationsProperty(); } else if (node instanceof AnonymousClassDeclaration) { return AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY; } // should not happen. Assert.isTrue(false); return null; }
public static ITypeBinding getEnclosingType(ASTNode node) { while(node != null) { if (node instanceof AbstractTypeDeclaration) { return ((AbstractTypeDeclaration)node).resolveBinding(); } else if (node instanceof AnonymousClassDeclaration) { return ((AnonymousClassDeclaration)node).resolveBinding(); } node= node.getParent(); } return null; }
@Override public void endVisit(AnonymousClassDeclaration node) { if (skipNode(node)) { return; } FlowInfo info = processSequential(node, node.bodyDeclarations()); info.setNoReturn(); }
@Override public boolean visit(AnonymousClassDeclaration node) { boolean result = super.visit(node); if (isFirstSelectedNode(node)) { invalidSelection(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_anonymous_type, JavaStatusContext.create(fCUnit, node)); return false; } return result; }
private int evaluateFieldModifiers(ASTNode newTypeDecl) { if (fSenderBinding.isAnnotation()) { return 0; } if (fSenderBinding.isInterface()) { // for interface members copy the modifiers from an existing field FieldDeclaration[] fieldDecls= ((TypeDeclaration) newTypeDecl).getFields(); if (fieldDecls.length > 0) { return fieldDecls[0].getModifiers(); } return 0; } int modifiers= 0; if (fVariableKind == CONST_FIELD) { modifiers |= Modifier.FINAL | Modifier.STATIC; } else { ASTNode parent= fOriginalNode.getParent(); if (parent instanceof QualifiedName) { IBinding qualifierBinding= ((QualifiedName)parent).getQualifier().resolveBinding(); if (qualifierBinding instanceof ITypeBinding) { modifiers |= Modifier.STATIC; } } else if (ASTResolving.isInStaticContext(fOriginalNode)) { modifiers |= Modifier.STATIC; } } ASTNode node= ASTResolving.findParentType(fOriginalNode, true); if (newTypeDecl.equals(node)) { modifiers |= Modifier.PRIVATE; } else if (node instanceof AnonymousClassDeclaration) { modifiers |= Modifier.PROTECTED; } else { modifiers |= Modifier.PUBLIC; } return modifiers; }
private void findFormalsForVariable(ClassInstanceCreation ctorCall) throws JavaModelException, CoreException { final int paramNumber = getParamNumber(ctorCall.arguments(), this.name); IMethod meth = (IMethod) ctorCall.resolveConstructorBinding() .getJavaElement(); if (meth == null && ctorCall.getAnonymousClassDeclaration() != null) { // most likely an anonymous class. final AnonymousClassDeclaration acd = ctorCall .getAnonymousClassDeclaration(); final ITypeBinding binding = acd.resolveBinding(); final ITypeBinding superBinding = binding.getSuperclass(); for (final Iterator it = Arrays.asList( superBinding.getDeclaredMethods()).iterator(); it.hasNext();) { final IMethodBinding imb = (IMethodBinding) it.next(); if (imb.isConstructor()) { final ITypeBinding[] itb = imb.getParameterTypes(); if (itb.length > paramNumber) { final ITypeBinding ithParamType = itb[paramNumber]; if (ithParamType.isEqualTo(((Expression) ctorCall .arguments().get(paramNumber)) .resolveTypeBinding())) { meth = (IMethod) imb.getJavaElement(); break; } } } } } final IMethod top = Util.getTopMostSourceMethod(meth, this.monitor); if (top == null) throw new DefinitelyNotEnumerizableException(Messages.ASTNodeProcessor_SourceNotPresent, ctorCall); else this.findFormalsForVariable(top, paramNumber); }
/** * Returns the type binding of the node's enclosing type declaration. * * @param node an AST node * @return the type binding of the node's parent type declaration, or <code>null</code> */ public static ITypeBinding getBindingOfParentType(ASTNode node) { while (node != null) { if (node instanceof AbstractTypeDeclaration) { return ((AbstractTypeDeclaration) node).resolveBinding(); } else if (node instanceof AnonymousClassDeclaration) { return ((AnonymousClassDeclaration) node).resolveBinding(); } node = node.getParent(); } return null; }
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; }