Search in sources :

Example 26 with ClassInstanceCreation

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

the class ConvertAnonymousToNestedRefactoring method setSuperType.

private void setSuperType(TypeDeclaration declaration) {
    ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) fAnonymousInnerClassNode.getParent();
    ITypeBinding binding = classInstanceCreation.resolveTypeBinding();
    if (binding == null)
        return;
    Type newType = (Type) ASTNode.copySubtree(fAnonymousInnerClassNode.getAST(), classInstanceCreation.getType());
    if (binding.getSuperclass().getQualifiedName().equals("java.lang.Object")) {
        //$NON-NLS-1$
        Assert.isTrue(binding.getInterfaces().length <= 1);
        if (binding.getInterfaces().length == 0)
            return;
        declaration.superInterfaceTypes().add(0, newType);
    } else {
        declaration.setSuperclassType(newType);
    }
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) Type(org.eclipse.jdt.core.dom.Type) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 27 with ClassInstanceCreation

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

the class AbstractSerialVersionOperation method rewriteAST.

/**
	 * {@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));
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) AST(org.eclipse.jdt.core.dom.AST) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) Javadoc(org.eclipse.jdt.core.dom.Javadoc) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 28 with ClassInstanceCreation

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

the class Java50Fix method createRawTypeReferenceOperations.

private static SimpleType createRawTypeReferenceOperations(CompilationUnit compilationUnit, IProblemLocation[] locations, List<CompilationUnitRewriteOperation> operations) {
    if (hasFatalError(compilationUnit))
        return null;
    List<SimpleType> result = new ArrayList<SimpleType>();
    for (int i = 0; i < locations.length; i++) {
        IProblemLocation problem = locations[i];
        if (isRawTypeReferenceProblem(problem.getProblemId())) {
            ASTNode node = problem.getCoveredNode(compilationUnit);
            if (node instanceof ClassInstanceCreation) {
                Type rawReference = (Type) node.getStructuralProperty(ClassInstanceCreation.TYPE_PROPERTY);
                if (isRawTypeReference(rawReference)) {
                    result.add((SimpleType) rawReference);
                }
            } else if (node instanceof SimpleName) {
                ASTNode rawReference = node.getParent();
                if (isRawTypeReference(rawReference)) {
                    ASTNode parent = rawReference.getParent();
                    if (!(parent instanceof ArrayType || parent instanceof ParameterizedType))
                        result.add((SimpleType) rawReference);
                }
            } else if (node instanceof MethodInvocation) {
                MethodInvocation invocation = (MethodInvocation) node;
                SimpleType rawReference = getRawReference(invocation, compilationUnit);
                if (rawReference != null) {
                    result.add(rawReference);
                }
            }
        }
    }
    if (result.size() == 0)
        return null;
    SimpleType[] types = result.toArray(new SimpleType[result.size()]);
    operations.add(new AddTypeParametersOperation(types));
    return types[0];
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 29 with ClassInstanceCreation

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

the class IntroduceFactoryRefactoring method replaceConstructorCalls.

/**
	 * Use the given <code>ASTRewrite</code> to replace direct calls to the constructor
	 * with calls to the newly-created factory method.
	 * @param rg the <code>SearchResultGroup</code> indicating all of the constructor references
	 * @param unit the <code>CompilationUnit</code> to be rewritten
	 * @param unitRewriter the rewriter
	 * @param unitChange the compilation unit change
	 * @throws CoreException
	 * @return true iff at least one constructor call site was rewritten.
	 */
private boolean replaceConstructorCalls(SearchResultGroup rg, CompilationUnit unit, ASTRewrite unitRewriter, CompilationUnitChange unitChange) throws CoreException {
    Assert.isTrue(ASTCreator.getCu(unit).equals(rg.getCompilationUnit()));
    SearchMatch[] hits = rg.getSearchResults();
    Arrays.sort(hits, new Comparator<SearchMatch>() {

        /**
			 * Sort by descending offset, such that nested constructor calls are processed first.
			 * This is necessary, since they can only be moved into the factory method invocation
			 * after they have been rewritten.
			 */
        public int compare(SearchMatch m1, SearchMatch m2) {
            return m2.getOffset() - m1.getOffset();
        }
    });
    boolean someCallPatched = false;
    for (int i = 0; i < hits.length; i++) {
        ASTNode ctrCall = getCtorCallAt(hits[i].getOffset(), hits[i].getLength(), unit);
        if (ctrCall instanceof ClassInstanceCreation) {
            TextEditGroup gd = new TextEditGroup(RefactoringCoreMessages.IntroduceFactory_replaceCalls);
            rewriteFactoryMethodCall((ClassInstanceCreation) ctrCall, unitRewriter, gd);
            unitChange.addTextEditGroup(gd);
            someCallPatched = true;
        } else if (ctrCall instanceof MethodRef) {
            TextEditGroup gd = new TextEditGroup(RefactoringCoreMessages.IntroduceFactoryRefactoring_replaceJavadocReference);
            rewriteJavadocReference((MethodRef) ctrCall, unitRewriter, gd);
            unitChange.addTextEditGroup(gd);
            someCallPatched = true;
        }
    }
    return someCallPatched;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) MethodRef(org.eclipse.jdt.core.dom.MethodRef) ASTNode(org.eclipse.jdt.core.dom.ASTNode) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 30 with ClassInstanceCreation

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

the class IntroduceFactoryRefactoring method createFactoryMethod.

/**
	 * Creates and returns a new MethodDeclaration that represents the factory method to be used in
	 * place of direct calls to the constructor in question.
	 * 
	 * @param ast An AST used as a factory for various AST nodes
	 * @param ctorBinding binding for the constructor being wrapped
	 * @param unitRewriter the ASTRewrite to be used
	 * @return the new method declaration
	 * @throws CoreException if an exception occurs while accessing its corresponding resource
	 */
private MethodDeclaration createFactoryMethod(AST ast, IMethodBinding ctorBinding, ASTRewrite unitRewriter) throws CoreException {
    MethodDeclaration newMethod = ast.newMethodDeclaration();
    SimpleName newMethodName = ast.newSimpleName(fNewMethodName);
    ClassInstanceCreation newCtorCall = ast.newClassInstanceCreation();
    ReturnStatement ret = ast.newReturnStatement();
    Block body = ast.newBlock();
    List<Statement> stmts = body.statements();
    String retTypeName = ctorBinding.getName();
    createFactoryMethodSignature(ast, newMethod);
    newMethod.setName(newMethodName);
    newMethod.setBody(body);
    ITypeBinding declaringClass = fCtorBinding.getDeclaringClass();
    ITypeBinding[] ctorOwnerTypeParameters = declaringClass.getTypeParameters();
    setMethodReturnType(newMethod, retTypeName, ctorOwnerTypeParameters, ast);
    newMethod.modifiers().addAll(ASTNodeFactory.newModifiers(ast, Modifier.STATIC | Modifier.PUBLIC));
    setCtorTypeArguments(newCtorCall, retTypeName, ctorOwnerTypeParameters, ast);
    createFactoryMethodConstructorArgs(ast, newCtorCall);
    if (Modifier.isAbstract(declaringClass.getModifiers())) {
        AnonymousClassDeclaration decl = ast.newAnonymousClassDeclaration();
        IMethodBinding[] unimplementedMethods = getUnimplementedMethods(declaringClass);
        CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(fCUHandle.getJavaProject());
        ImportRewriteContext context = new ContextSensitiveImportRewriteContext(fFactoryCU, decl.getStartPosition(), fImportRewriter);
        for (int i = 0; i < unimplementedMethods.length; i++) {
            IMethodBinding unImplementedMethod = unimplementedMethods[i];
            MethodDeclaration newMethodDecl = StubUtility2.createImplementationStub(fCUHandle, unitRewriter, fImportRewriter, context, unImplementedMethod, unImplementedMethod.getDeclaringClass().getName(), settings, false);
            decl.bodyDeclarations().add(newMethodDecl);
        }
        newCtorCall.setAnonymousClassDeclaration(decl);
    }
    ret.setExpression(newCtorCall);
    stmts.add(ret);
    return newMethod;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) CodeGenerationSettings(org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Statement(org.eclipse.jdt.core.dom.Statement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block)

Aggregations

ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)36 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)18 ASTNode (org.eclipse.jdt.core.dom.ASTNode)17 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)13 SimpleName (org.eclipse.jdt.core.dom.SimpleName)13 Expression (org.eclipse.jdt.core.dom.Expression)10 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)9 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)9 Type (org.eclipse.jdt.core.dom.Type)9 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)8 ArrayList (java.util.ArrayList)7 IType (org.eclipse.jdt.core.IType)6 CastExpression (org.eclipse.jdt.core.dom.CastExpression)6 SuperConstructorInvocation (org.eclipse.jdt.core.dom.SuperConstructorInvocation)6 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)5 AST (org.eclipse.jdt.core.dom.AST)5 ArrayType (org.eclipse.jdt.core.dom.ArrayType)5 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)5 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)5 SimpleType (org.eclipse.jdt.core.dom.SimpleType)5