public static boolean isGenerated(org.eclipse.jdt.core.dom.ASTNode node) { boolean result = false; try { result = ((Boolean)node.getClass().getField("$isGenerated").get(node)).booleanValue(); if (!result && node.getParent() != null && node.getParent() instanceof org.eclipse.jdt.core.dom.QualifiedName) { result = isGenerated(node.getParent()); } } catch (Exception e) { // better to assume it isn't generated } return result; }
private ASTRewrite doAddEnumConst(CompilationUnit astRoot) { SimpleName node= fOriginalNode; ASTNode newTypeDecl= astRoot.findDeclaringNode(fSenderBinding); if (newTypeDecl == null) { astRoot= ASTResolving.createQuickFixAST(getCompilationUnit(), null); newTypeDecl= astRoot.findDeclaringNode(fSenderBinding.getKey()); } if (newTypeDecl != null) { AST ast= newTypeDecl.getAST(); ASTRewrite rewrite= ASTRewrite.create(ast); EnumConstantDeclaration constDecl= ast.newEnumConstantDeclaration(); constDecl.setName(ast.newSimpleName(node.getIdentifier())); ListRewrite listRewriter= rewrite.getListRewrite(newTypeDecl, EnumDeclaration.ENUM_CONSTANTS_PROPERTY); listRewriter.insertLast(constDecl, null); return rewrite; } return null; }
@Override public void updateNode() throws JavaModelException { int start= fNode.getStartPosition(); int length= fNode.getLength(); String msg= "Cannot update found node: nodeType=" + fNode.getNodeType() + "; " //$NON-NLS-1$//$NON-NLS-2$ + fNode.toString() + "[" + start + ", " + length + "] in " + fCuRewrite.getCu(); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ JavaPlugin.log(new Exception(msg + ":\n" + fCuRewrite.getCu().getSource().substring(start, start + length))); //$NON-NLS-1$ fResult.addError(msg, JavaStatusContext.create(fCuRewrite.getCu(), fNode)); } @Override
private void copyPackageDeclarationToDestination(IPackageDeclaration declaration, ASTRewrite targetRewrite, CompilationUnit sourceCuNode, CompilationUnit destinationCuNode) throws JavaModelException { if (destinationCuNode.getPackage() != null) { return; } PackageDeclaration sourceNode= ASTNodeSearchUtil.getPackageDeclarationNode(declaration, sourceCuNode); PackageDeclaration copiedNode= (PackageDeclaration) ASTNode.copySubtree(targetRewrite.getAST(), sourceNode); targetRewrite.set(destinationCuNode, CompilationUnit.PACKAGE_PROPERTY, copiedNode, null); }
@Override public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModelCore linkedModel) throws CoreException { ASTRewrite rewrite= cuRewrite.getASTRewrite(); IBinding binding= fUnusedName.resolveBinding(); CompilationUnit root= (CompilationUnit) fUnusedName.getRoot(); String displayString= FixMessages.UnusedCodeFix_RemoveUnusedTypeParameter_description; TextEditGroup group= createTextEditGroup(displayString, cuRewrite); if (binding.getKind() == IBinding.TYPE) { ITypeBinding decl= ((ITypeBinding) binding).getTypeDeclaration(); ASTNode declaration= root.findDeclaringNode(decl); if (declaration.getParent() instanceof TypeDeclarationStatement) { declaration= declaration.getParent(); } rewrite.remove(declaration, group); } } }
private void addMethodStubForAbstractMethod(final IMethod sourceMethod, final CompilationUnit declaringCuNode, final AbstractTypeDeclaration typeToCreateStubIn, final ICompilationUnit newCu, final CompilationUnitRewrite rewriter, final Map adjustments, final IProgressMonitor monitor, final RefactoringStatus status) throws CoreException { final MethodDeclaration methodToCreateStubFor= ASTNodeSearchUtil.getMethodDeclarationNode(sourceMethod, declaringCuNode); final AST ast= rewriter.getRoot().getAST(); final MethodDeclaration newMethod= ast.newMethodDeclaration(); newMethod.setBody(createMethodStub(methodToCreateStubFor, ast)); newMethod.setConstructor(false); newMethod.setExtraDimensions(methodToCreateStubFor.getExtraDimensions()); newMethod.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getModifiersWithUpdatedVisibility(sourceMethod, JdtFlags.clearFlag(Modifier.NATIVE | Modifier.ABSTRACT, methodToCreateStubFor.getModifiers()), adjustments, new SubProgressMonitor(monitor, 1), false, status))); newMethod.setName(((SimpleName) ASTNode.copySubtree(ast, methodToCreateStubFor.getName()))); final TypeVariableMaplet[] mapping= TypeVariableUtil.composeMappings(TypeVariableUtil.subTypeToSuperType(getDeclaringType(), getDestinationType()), TypeVariableUtil.superTypeToInheritedType(getDestinationType(), ((IType) typeToCreateStubIn.resolveBinding().getJavaElement()))); copyReturnType(rewriter.getASTRewrite(), getDeclaringType().getCompilationUnit(), methodToCreateStubFor, newMethod, mapping); copyParameters(rewriter.getASTRewrite(), getDeclaringType().getCompilationUnit(), methodToCreateStubFor, newMethod, mapping); copyThrownExceptions(methodToCreateStubFor, newMethod); newMethod.setJavadoc(createJavadocForStub(typeToCreateStubIn.getName().getIdentifier(), methodToCreateStubFor, newMethod, newCu, rewriter.getASTRewrite())); ImportRewriteUtil.addImports(rewriter, newMethod, new HashMap(), new HashMap(), false); rewriter.getASTRewrite().getListRewrite(typeToCreateStubIn, typeToCreateStubIn.getBodyDeclarationsProperty()).insertAt(newMethod, ASTNodes.getInsertionIndex(newMethod, typeToCreateStubIn.bodyDeclarations()), rewriter.createCategorizedGroupDescription(RefactoringCoreMessages.PullUpRefactoring_add_method_stub, SET_PULL_UP)); }
protected static ASTNode moveNode(CompilationUnitRewrite cuRewrite, ASTNode node) { ASTRewrite rewrite= cuRewrite.getASTRewrite(); if (rewrite.getAST() != node.getAST()) { String str= ASTNodes.getNodeSource(node, true, true); if (str != null) { return rewrite.createStringPlaceholder(str, node.getNodeType()); } return ASTNode.copySubtree(rewrite.getAST(), node); } return rewrite.createMoveTarget(node); } /**
private SimpleName getNewName(ASTRewrite rewrite) { AST ast= rewrite.getAST(); String name; if (fInvocationNode.getLocationInParent() == MemberValuePair.NAME_PROPERTY) { name= ((SimpleName) fInvocationNode).getIdentifier(); if (ast == fInvocationNode.getAST()) { addLinkedPosition(rewrite.track(fInvocationNode), true, KEY_NAME); } } else { name= "value"; //$NON-NLS-1$ } SimpleName newNameNode= ast.newSimpleName(name); addLinkedPosition(rewrite.track(newNameNode), false, KEY_NAME); return newNameNode; }
protected SimpleName getNewName(ASTRewrite rewrite) { ASTNode invocationNode= getInvocationNode(); String name; if (invocationNode instanceof MethodInvocation) { name= ((MethodInvocation)invocationNode).getName().getIdentifier(); } else if (invocationNode instanceof SuperMethodInvocation) { name= ((SuperMethodInvocation)invocationNode).getName().getIdentifier(); } else { name= getSenderBinding().getName(); // name of the class } AST ast= rewrite.getAST(); SimpleName newNameNode= ast.newSimpleName(name); addLinkedPosition(rewrite.track(newNameNode), false, KEY_NAME); ASTNode invocationName= getInvocationNameNode(); if (invocationName != null && invocationName.getAST() == ast) { // in the same CU addLinkedPosition(rewrite.track(invocationName), true, KEY_NAME); } return newNameNode; }
String proposeLocalName(SimpleName fieldName, CompilationUnit root, IJavaProject javaProject) { // don't propose names that are already in use: Collection<String> variableNames= new ScopeAnalyzer(root).getUsedVariableNames(this.enclosingMethod.getStartPosition(), this.enclosingMethod.getLength()); String[] names = new String[variableNames.size()+1]; variableNames.toArray(names); // don't propose the field name itself, either: String identifier= fieldName.getIdentifier(); names[names.length-1] = identifier; return StubUtility.getLocalNameSuggestions(javaProject, identifier, 0, names)[0]; }
public static boolean isComplainingAboutReturn(ASTNode selectedNode) { if (selectedNode.getParent().getNodeType() == ASTNode.RETURN_STATEMENT) return true; while (!(selectedNode instanceof Type)) { if (selectedNode == null) return false; selectedNode= selectedNode.getParent(); } return selectedNode.getLocationInParent() == MethodDeclaration.RETURN_TYPE2_PROPERTY; }
private void copyImportToDestination(IImportDeclaration declaration, ASTRewrite targetRewrite, CompilationUnit sourceCuNode, CompilationUnit destinationCuNode) throws JavaModelException { ImportDeclaration sourceNode= ASTNodeSearchUtil.getImportDeclarationNode(declaration, sourceCuNode); ImportDeclaration copiedNode= (ImportDeclaration) ASTNode.copySubtree(targetRewrite.getAST(), sourceNode); targetRewrite.getListRewrite(destinationCuNode, CompilationUnit.IMPORTS_PROPERTY).insertLast(copiedNode, null); }
private void removeStatement(ASTRewrite rewrite, ASTNode statementNode, TextEditGroup group) { if (ASTNodes.isControlStatementBody(statementNode.getLocationInParent())) { rewrite.replace(statementNode, rewrite.getAST().newBlock(), group); } else { rewrite.remove(statementNode, group); } }
public static RefactoringStatus checkMethodSyntaxErrors(int selectionStart, int selectionLength, CompilationUnit cuNode, String invalidSelectionMessage){ SelectionAnalyzer analyzer= new SelectionAnalyzer(Selection.createFromStartLength(selectionStart, selectionLength), true); cuNode.accept(analyzer); ASTNode coveringNode= analyzer.getLastCoveringNode(); if (! (coveringNode instanceof Block) || ! (coveringNode.getParent() instanceof MethodDeclaration)) return RefactoringStatus.createFatalErrorStatus(invalidSelectionMessage); if (ASTNodes.getMessages(coveringNode, ASTNodes.NODE_ONLY).length == 0) return RefactoringStatus.createFatalErrorStatus(invalidSelectionMessage); MethodDeclaration methodDecl= (MethodDeclaration)coveringNode.getParent(); String message= Messages.format(RefactoringCoreMessages.CodeRefactoringUtil_error_message, BasicElementLabels.getJavaElementName(methodDecl.getName().getIdentifier())); return RefactoringStatus.createFatalErrorStatus(message); }
public static void addFallThroughProposals(IInvocationContext context, IProblemLocation problem, Collection proposals) { ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot()); if (selectedNode instanceof SwitchCase && selectedNode.getParent() instanceof SwitchStatement) { AST ast= selectedNode.getAST(); ASTRewrite rewrite= ASTRewrite.create(ast); ListRewrite listRewrite= rewrite.getListRewrite(selectedNode.getParent(), SwitchStatement.STATEMENTS_PROPERTY); listRewrite.insertBefore(ast.newBreakStatement(), selectedNode, null); String label= CorrectionMessages.LocalCorrectionsSubProcessor_insert_break_statement; Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, 5, image); proposals.add(proposal); } }
private SimpleName getNewName(ASTRewrite rewrite) { AST ast= rewrite.getAST(); String name; if (fInvocationNode.getLocationInParent() == MemberValuePair.NAME_PROPERTY) { name= ((SimpleName) fInvocationNode).getIdentifier(); } else { name= "value"; //$NON-NLS-1$ } SimpleName newNameNode= ast.newSimpleName(name); return newNameNode; }
@Override public boolean consumes(SemanticToken token) { // 1: match types in type parameter lists SimpleName name= token.getNode(); ASTNode node= name.getParent(); if (node.getNodeType() != ASTNode.SIMPLE_TYPE && node.getNodeType() != ASTNode.TYPE_PARAMETER) return false; // 2: match generic type variable references IBinding binding= token.getBinding(); return binding instanceof ITypeBinding && ((ITypeBinding) binding).isTypeVariable(); } }
protected void pushNode(ASTNode n, String label) { int type = n.getNodeType(); String typeName = n.getClass().getSimpleName(); push(type, typeName, label, n.getStartPosition(), n.getLength()); }
public static void getInvalidQualificationProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) { ASTNode node= problem.getCoveringNode(context.getASTRoot()); if (!(node instanceof Name)) { return; } Name name= (Name) node; IBinding binding= name.resolveBinding(); if (!(binding instanceof ITypeBinding)) { return; } ITypeBinding typeBinding= (ITypeBinding)binding; AST ast= node.getAST(); ASTRewrite rewrite= ASTRewrite.create(ast); rewrite.replace(name, ast.newName(typeBinding.getQualifiedName()), null); String label= CorrectionMessages.JavadocTagsSubProcessor_qualifylinktoinner_description; Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.QUALIFY_INNER_TYPE_NAME, image); proposals.add(proposal); } }
static CUCorrectionProposal createTypeRefChangeFullProposal(ICompilationUnit cu, ITypeBinding binding, ASTNode node, int relevance) { ASTRewrite rewrite= ASTRewrite.create(node.getAST()); String label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_change_full_type_description, BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_DEFAULT)); ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, cu, rewrite, relevance); ImportRewrite imports= proposal.createImportRewrite((CompilationUnit) node.getRoot()); Type type= imports.addImport(binding, node.getAST()); rewrite.replace(node, type, null); return proposal; }