public static org.eclipse.jdt.core.dom.MethodDeclaration getRealMethodDeclarationNode(org.eclipse.jdt.core.IMethod sourceMethod, org.eclipse.jdt.core.dom.CompilationUnit cuUnit) throws JavaModelException { MethodDeclaration methodDeclarationNode = ASTNodeSearchUtil.getMethodDeclarationNode(sourceMethod, cuUnit); if (isGenerated(methodDeclarationNode)) { IType declaringType = sourceMethod.getDeclaringType();
private static SuperConstructorInvocation getSuperConstructorCallNode(IMethod constructor, CompilationUnit cuNode) throws JavaModelException { Assert.isTrue(constructor.isConstructor()); MethodDeclaration constructorNode= ASTNodeSearchUtil.getMethodDeclarationNode(constructor, cuNode); Assert.isTrue(constructorNode.isConstructor()); Block body= constructorNode.getBody(); Assert.isNotNull(body); List statements= body.statements(); if (! statements.isEmpty() && statements.get(0) instanceof SuperConstructorInvocation) return (SuperConstructorInvocation)statements.get(0); return null; } }
protected static List getDeclarationNodes(final CompilationUnit cuNode, final List members) throws JavaModelException { final List result= new ArrayList(members.size()); for (final Iterator iterator= members.iterator(); iterator.hasNext();) { final IMember member= (IMember) iterator.next(); ASTNode node= null; if (member instanceof IField) { if (Flags.isEnum(member.getFlags())) node= ASTNodeSearchUtil.getEnumConstantDeclaration((IField) member, cuNode); else node= ASTNodeSearchUtil.getFieldDeclarationFragmentNode((IField) member, cuNode); } else if (member instanceof IType) node= ASTNodeSearchUtil.getAbstractTypeDeclarationNode((IType) member, cuNode); else if (member instanceof IMethod) node= ASTNodeSearchUtil.getMethodDeclarationNode((IMethod) member, cuNode); if (node != null) result.add(node); } return result; }
private static SuperConstructorInvocation getSuperConstructorCallNode(IMethod constructor, CompilationUnit cuNode) throws JavaModelException { Assert.isTrue(constructor.isConstructor()); MethodDeclaration constructorNode= ASTNodeSearchUtil.getMethodDeclarationNode(constructor, cuNode); Assert.isTrue(constructorNode.isConstructor()); Block body= constructorNode.getBody(); Assert.isNotNull(body); List<Statement> statements= body.statements(); if (! statements.isEmpty() && statements.get(0) instanceof SuperConstructorInvocation) return (SuperConstructorInvocation)statements.get(0); return null; } }
private static SuperConstructorInvocation getSuperConstructorCallNode(IMethod constructor, CompilationUnit cuNode) throws JavaModelException { Assert.isTrue(constructor.isConstructor()); MethodDeclaration constructorNode= ASTNodeSearchUtil.getMethodDeclarationNode(constructor, cuNode); Assert.isTrue(constructorNode.isConstructor()); Block body= constructorNode.getBody(); Assert.isNotNull(body); List<Statement> statements= body.statements(); if (! statements.isEmpty() && statements.get(0) instanceof SuperConstructorInvocation) return (SuperConstructorInvocation)statements.get(0); return null; } }
protected static List<ASTNode> getDeclarationNodes(final CompilationUnit cuNode, final List<IMember> members) throws JavaModelException { final List<ASTNode> result= new ArrayList<>(members.size()); for (final Iterator<IMember> iterator= members.iterator(); iterator.hasNext();) { final IMember member= iterator.next(); ASTNode node= null; if (member instanceof IField) { if (Flags.isEnum(member.getFlags())) node= ASTNodeSearchUtil.getEnumConstantDeclaration((IField) member, cuNode); else node= ASTNodeSearchUtil.getFieldDeclarationFragmentNode((IField) member, cuNode); } else if (member instanceof IType) node= ASTNodeSearchUtil.getAbstractTypeDeclarationNode((IType) member, cuNode); else if (member instanceof IMethod) node= ASTNodeSearchUtil.getMethodDeclarationNode((IMethod) member, cuNode); if (node != null) result.add(node); } return result; }
protected static List<ASTNode> getDeclarationNodes(final CompilationUnit cuNode, final List<IMember> members) throws JavaModelException { final List<ASTNode> result= new ArrayList<>(members.size()); for (final Iterator<IMember> iterator= members.iterator(); iterator.hasNext();) { final IMember member= iterator.next(); ASTNode node= null; if (member instanceof IField) { if (Flags.isEnum(member.getFlags())) node= ASTNodeSearchUtil.getEnumConstantDeclaration((IField) member, cuNode); else node= ASTNodeSearchUtil.getFieldDeclarationFragmentNode((IField) member, cuNode); } else if (member instanceof IType) node= ASTNodeSearchUtil.getAbstractTypeDeclarationNode((IType) member, cuNode); else if (member instanceof IMethod) node= ASTNodeSearchUtil.getMethodDeclarationNode((IMethod) member, cuNode); if (node != null) result.add(node); } return result; }
public static BodyDeclaration getMethodOrAnnotationTypeMemberDeclarationNode(IMethod iMethod, CompilationUnit cuNode) throws JavaModelException { if (JdtFlags.isAnnotation(iMethod.getDeclaringType())) return getAnnotationTypeMemberDeclarationNode(iMethod, cuNode); else return getMethodDeclarationNode(iMethod, cuNode); }
public static BodyDeclaration getMethodOrAnnotationTypeMemberDeclarationNode(IMethod iMethod, CompilationUnit cuNode) throws JavaModelException { if (JdtFlags.isAnnotation(iMethod.getDeclaringType())) return getAnnotationTypeMemberDeclarationNode(iMethod, cuNode); else return getMethodDeclarationNode(iMethod, cuNode); }
/** * Creates the method annotations and comments of the extracted methods in * the source type. * * @param sourceRewrite * the source compilation unit rewrite * @param replacements * the set of variable binding keys of formal parameters which * must be replaced * @throws CoreException * if an error occurs */ protected final void createMethodComments(final CompilationUnitRewrite sourceRewrite, final Set replacements) throws CoreException { Assert.isNotNull(sourceRewrite); Assert.isNotNull(replacements); if (fComments && fMembers.length > 0) { final IJavaProject project= fSubType.getJavaProject(); final boolean javadoc= project.getOption(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, true).equals(JavaCore.ENABLED); IMember member= null; for (int index= 0; index < fMembers.length; index++) { member= fMembers[index]; if (member instanceof IMethod) createMethodComment(sourceRewrite, ASTNodeSearchUtil.getMethodDeclarationNode((IMethod) member, sourceRewrite.getRoot()), replacements, javadoc); } } }
public static BodyDeclaration getMethodOrAnnotationTypeMemberDeclarationNode(IMethod iMethod, CompilationUnit cuNode) throws JavaModelException { if (JdtFlags.isAnnotation(iMethod.getDeclaringType())) return getAnnotationTypeMemberDeclarationNode(iMethod, cuNode); else return getMethodDeclarationNode(iMethod, cuNode); }
private void addMethodDelegate(IMethod getter, String newName, CompilationUnitRewrite rewrite) throws JavaModelException { MethodDeclaration declaration= ASTNodeSearchUtil.getMethodDeclarationNode(getter, rewrite.getRoot()); DelegateCreator creator= new DelegateMethodCreator(); creator.setDeclareDeprecated(fDelegateDeprecation); creator.setDeclaration(declaration); creator.setNewElementName(newName); creator.setSourceRewrite(rewrite); creator.prepareDelegate(); creator.createEdit(); }
private void addMethodDelegate(IMethod getter, String newName, CompilationUnitRewrite rewrite) throws JavaModelException { MethodDeclaration declaration= ASTNodeSearchUtil.getMethodDeclarationNode(getter, rewrite.getRoot()); DelegateCreator creator= new DelegateMethodCreator(); creator.setDeclareDeprecated(fDelegateDeprecation); creator.setDeclaration(declaration); creator.setNewElementName(newName); creator.setSourceRewrite(rewrite); creator.prepareDelegate(); creator.createEdit(); }
private void addMethodDelegate(IMethod getter, String newName, CompilationUnitRewrite rewrite) throws JavaModelException { MethodDeclaration declaration= ASTNodeSearchUtil.getMethodDeclarationNode(getter, rewrite.getRoot()); DelegateCreator creator= new DelegateMethodCreator(); creator.setDeclareDeprecated(fDelegateDeprecation); creator.setDeclaration(declaration); creator.setNewElementName(newName); creator.setSourceRewrite(rewrite); creator.prepareDelegate(); creator.createEdit(); }
public final RefactoringStatus checkInitialConditions(final IProgressMonitor monitor) throws CoreException, OperationCanceledException { Assert.isNotNull(monitor); final RefactoringStatus status= new RefactoringStatus(); try { monitor.beginTask("", 4); //$NON-NLS-1$ monitor.setTaskName(RefactoringCoreMessages.MoveInstanceMethodProcessor_checking); status.merge(Checks.checkIfCuBroken(fMethod)); if (!status.hasError()) { checkMethodDeclaration(new SubProgressMonitor(monitor, 1), status); if (status.isOK()) { final MethodDeclaration declaration= ASTNodeSearchUtil.getMethodDeclarationNode(fMethod, fSourceRewrite.getRoot()); checkGenericTypes(new SubProgressMonitor(monitor, 1), declaration, status); checkMethodBody(new SubProgressMonitor(monitor, 1), declaration, status); checkPossibleTargets(new SubProgressMonitor(monitor, 1), declaration, status); } } } finally { monitor.done(); } return status; }
private void declareMethodAbstract(MemberActionInfo info, CompilationUnitRewrite sourceRewrite, CompilationUnitRewrite unitRewrite) throws JavaModelException { Assert.isTrue(!info.isFieldInfo()); IMethod method= (IMethod) info.getMember(); if (JdtFlags.isAbstract(method)) return; final MethodDeclaration declaration= ASTNodeSearchUtil.getMethodDeclarationNode(method, sourceRewrite.getRoot()); unitRewrite.getASTRewrite().remove(declaration.getBody(), null); sourceRewrite.getImportRemover().registerRemovedNode(declaration.getBody()); ModifierRewrite.create(unitRewrite.getASTRewrite(), declaration).setModifiers(info.getNewModifiersForOriginal(declaration.getModifiers()), null); }
private void declareMethodAbstract(MemberActionInfo info, CompilationUnitRewrite sourceRewrite, CompilationUnitRewrite unitRewrite) throws JavaModelException { Assert.isTrue(!info.isFieldInfo()); IMethod method= (IMethod) info.getMember(); if (JdtFlags.isAbstract(method)) return; final MethodDeclaration declaration= ASTNodeSearchUtil.getMethodDeclarationNode(method, sourceRewrite.getRoot()); unitRewrite.getASTRewrite().remove(declaration.getBody(), null); sourceRewrite.getImportRemover().registerRemovedNode(declaration.getBody()); ModifierRewrite.create(unitRewrite.getASTRewrite(), declaration).setModifiers(info.getNewModifiersForOriginal(declaration.getModifiers()), null); }
private void declareMethodAbstract(MemberActionInfo info, CompilationUnitRewrite sourceRewrite, CompilationUnitRewrite unitRewrite) throws JavaModelException { Assert.isTrue(!info.isFieldInfo()); IMethod method= (IMethod) info.getMember(); if (JdtFlags.isAbstract(method)) return; final MethodDeclaration declaration= ASTNodeSearchUtil.getMethodDeclarationNode(method, sourceRewrite.getRoot()); unitRewrite.getASTRewrite().remove(declaration.getBody(), null); sourceRewrite.getImportRemover().registerRemovedNode(declaration.getBody()); ModifierRewrite.create(unitRewrite.getASTRewrite(), declaration).setModifiers(info.getNewModifiersForOriginal(declaration.getModifiers()), null); }
private void createAbstractMethod(final IMethod sourceMethod, final CompilationUnitRewrite sourceRewriter, final CompilationUnit declaringCuNode, final AbstractTypeDeclaration destination, final TypeVariableMaplet[] mapping, final CompilationUnitRewrite targetRewrite, final Map adjustments, final IProgressMonitor monitor, final RefactoringStatus status) throws JavaModelException { final MethodDeclaration oldMethod= ASTNodeSearchUtil.getMethodDeclarationNode(sourceMethod, declaringCuNode); if (JavaModelUtil.is50OrHigher(sourceMethod.getJavaProject()) && (fSettings.overrideAnnotation || JavaCore.ERROR.equals(sourceMethod.getJavaProject().getOption(JavaCore.COMPILER_PB_MISSING_OVERRIDE_ANNOTATION, true)))) { final MarkerAnnotation annotation= sourceRewriter.getAST().newMarkerAnnotation(); annotation.setTypeName(sourceRewriter.getAST().newSimpleName("Override")); //$NON-NLS-1$ sourceRewriter.getASTRewrite().getListRewrite(oldMethod, MethodDeclaration.MODIFIERS2_PROPERTY).insertFirst(annotation, sourceRewriter.createCategorizedGroupDescription(RefactoringCoreMessages.PullUpRefactoring_add_override_annotation, SET_PULL_UP)); } final MethodDeclaration newMethod= targetRewrite.getAST().newMethodDeclaration(); newMethod.setBody(null); newMethod.setConstructor(false); newMethod.setExtraDimensions(oldMethod.getExtraDimensions()); newMethod.setJavadoc(null); int modifiers= getModifiersWithUpdatedVisibility(sourceMethod, Modifier.ABSTRACT | JdtFlags.clearFlag(Modifier.NATIVE | Modifier.FINAL, sourceMethod.getFlags()), adjustments, monitor, false, status); if (oldMethod.isVarargs()) modifiers&= ~Flags.AccVarargs; newMethod.modifiers().addAll(ASTNodeFactory.newModifiers(targetRewrite.getAST(), modifiers)); newMethod.setName(((SimpleName) ASTNode.copySubtree(targetRewrite.getAST(), oldMethod.getName()))); copyReturnType(targetRewrite.getASTRewrite(), getDeclaringType().getCompilationUnit(), oldMethod, newMethod, mapping); copyParameters(targetRewrite.getASTRewrite(), getDeclaringType().getCompilationUnit(), oldMethod, newMethod, mapping); copyThrownExceptions(oldMethod, newMethod); ImportRewriteUtil.addImports(targetRewrite, newMethod, new HashMap(), new HashMap(), false); targetRewrite.getASTRewrite().getListRewrite(destination, destination.getBodyDeclarationsProperty()).insertAt(newMethod, ASTNodes.getInsertionIndex(newMethod, destination.bodyDeclarations()), targetRewrite.createCategorizedGroupDescription(RefactoringCoreMessages.PullUpRefactoring_add_abstract_method, SET_PULL_UP)); }
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)); }