Search in sources :

Example 6 with Modifier

use of org.eclipse.jdt.core.dom.Modifier in project AutoRefactor by JnRouvignac.

the class RemoveUselessModifiersRefactoring method removeFinalModifier.

private boolean removeFinalModifier(List<IExtendedModifier> modifiers) {
    boolean result = VISIT_SUBTREE;
    for (Modifier m : getModifiersOnly(modifiers)) {
        if (m.isFinal()) {
            ctx.getRefactorings().remove(m);
            result = DO_NOT_VISIT_SUBTREE;
        }
    }
    return result;
}
Also used : IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier)

Example 7 with Modifier

use of org.eclipse.jdt.core.dom.Modifier in project AutoRefactor by JnRouvignac.

the class RemoveUselessModifiersRefactoring method removePublicStaticFinalModifiers.

private boolean removePublicStaticFinalModifiers(FieldDeclaration node) {
    // remove modifiers implied by the context
    boolean result = VISIT_SUBTREE;
    for (Modifier m : getModifiersOnly(modifiers(node))) {
        if (m.isPublic() || m.isStatic() || m.isFinal()) {
            ctx.getRefactorings().remove(m);
            result = DO_NOT_VISIT_SUBTREE;
        }
    }
    return result;
}
Also used : IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier)

Example 8 with Modifier

use of org.eclipse.jdt.core.dom.Modifier in project jbosstools-hibernate by jbosstools.

the class ProcessEntityInfo method visit.

@SuppressWarnings("unchecked")
public boolean visit(TypeDeclaration node) {
    ITypeBinding typeBinding = node.resolveBinding();
    String nodeName = typeBinding == null ? null : typeBinding.getBinaryName();
    if (nodeName == null) {
        return false;
    }
    entityInfo = entityInfos.getEntityInfo(nodeName);
    if (entityInfo == null) {
        return false;
    }
    if (entityInfo.isAddMappedSuperclassFlag()) {
        MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
        matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_MAPPEDSUPERCLASS));
        ListRewrite lrw = rewriter.getListRewrite(node, TypeDeclaration.MODIFIERS2_PROPERTY);
        lrw.insertFirst(matd, null);
    }
    if (entityInfo.isAddEntityFlag()) {
        MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
        matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_ENTITY));
        ListRewrite lrw = rewriter.getListRewrite(node, TypeDeclaration.MODIFIERS2_PROPERTY);
        lrw.insertFirst(matd, null);
    }
    /**
     * /
     *		if (!entityInfo.isImplicitConstructorFlag() && !entityInfo.isDefaultConstructorFlag() &&
     *				entityInfo.isAddSerializableInterfaceFlag()) {
     *			// add serializable interface
     *			SimpleName sn = null;
     *			//if (!entityInfo.isAddSerializableInterfaceImportFlag()) {
     *				sn = rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_SERIALIZABLE);
     *			//}
     *			//else {
     *			//	sn = rewriter.getAST().newSimpleName(JPAConst.IMPORT_SERIALIZABLE);
     *			//}
     *			SimpleType st = rewriter.getAST().newSimpleType(sn);
     *			ListRewrite lrw = rewriter.getListRewrite(node, TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY);
     *			lrw.insertFirst(st, null);
     *			// add "private static final long serialVersionUID = 1L;"
     *			// ...
     *		}
     *		/*
     */
    if (!entityInfo.isImplicitConstructorFlag() && !entityInfo.isDefaultConstructorFlag() && entityInfo.isAddSerializableInterfaceFlag()) {
        MethodDeclaration md = rewriter.getAST().newMethodDeclaration();
        md.setConstructor(true);
        Modifier modifier = rewriter.getAST().newModifier(Modifier.ModifierKeyword.PROTECTED_KEYWORD);
        md.modifiers().add(modifier);
        Block body = rewriter.getAST().newBlock();
        md.setBody(body);
        SimpleName sn = rewriter.getAST().newSimpleName(entityInfo.getName());
        md.setName(sn);
        ListRewrite lrw = rewriter.getListRewrite(node, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
        List<?> list = lrw.getOriginalList();
        MethodDeclaration insertBeforeNode = null;
        Iterator<?> it = list.iterator();
        while (it.hasNext()) {
            Object obj = it.next();
            if (obj instanceof MethodDeclaration) {
                insertBeforeNode = (MethodDeclaration) obj;
                break;
            }
        }
        if (insertBeforeNode == null) {
            lrw.insertLast(md, null);
        } else {
            lrw.insertBefore(md, insertBeforeNode, null);
        }
    }
    if (enableOptLock && entityInfo.isAddVersionFlag() && !entityInfo.hasVersionAnnotation()) {
        // add property "version", add getter/setter getVersion/setVersion,
        // add annotation for the property or for the getter
        // 
        // $NON-NLS-1$
        final String version = "version";
        // $NON-NLS-1$
        final String versionType = "Integer";
        // 
        VariableDeclarationFragment vdFragment = rewriter.getAST().newVariableDeclarationFragment();
        SimpleName variableName = rewriter.getAST().newSimpleName(version);
        vdFragment.setName(variableName);
        FieldDeclaration fieldVersion = rewriter.getAST().newFieldDeclaration(vdFragment);
        Modifier modifier = rewriter.getAST().newModifier(Modifier.ModifierKeyword.PROTECTED_KEYWORD);
        fieldVersion.modifiers().add(modifier);
        Name typeName = rewriter.getAST().newName(versionType);
        SimpleType type = rewriter.getAST().newSimpleType(typeName);
        fieldVersion.setType(type);
        // 
        MethodDeclaration mdGetter = rewriter.getAST().newMethodDeclaration();
        modifier = rewriter.getAST().newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
        mdGetter.modifiers().add(modifier);
        Block body = rewriter.getAST().newBlock();
        ReturnStatement returnVersion = rewriter.getAST().newReturnStatement();
        variableName = rewriter.getAST().newSimpleName(version);
        returnVersion.setExpression(variableName);
        body.statements().add(returnVersion);
        mdGetter.setBody(body);
        // $NON-NLS-1$
        SimpleName sn = rewriter.getAST().newSimpleName("getVersion");
        mdGetter.setName(sn);
        typeName = rewriter.getAST().newName(versionType);
        type = rewriter.getAST().newSimpleType(typeName);
        mdGetter.setReturnType2(type);
        // 
        MethodDeclaration mdSetter = rewriter.getAST().newMethodDeclaration();
        modifier = rewriter.getAST().newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
        mdSetter.modifiers().add(modifier);
        body = rewriter.getAST().newBlock();
        Assignment assignment = rewriter.getAST().newAssignment();
        FieldAccess fieldAccess = rewriter.getAST().newFieldAccess();
        ThisExpression thisExpression = rewriter.getAST().newThisExpression();
        fieldAccess.setExpression(thisExpression);
        variableName = rewriter.getAST().newSimpleName(version);
        fieldAccess.setName(variableName);
        assignment.setLeftHandSide(fieldAccess);
        variableName = rewriter.getAST().newSimpleName(version);
        assignment.setRightHandSide(variableName);
        ExpressionStatement expressionStatement = rewriter.getAST().newExpressionStatement(assignment);
        body.statements().add(expressionStatement);
        mdSetter.setBody(body);
        // $NON-NLS-1$
        sn = rewriter.getAST().newSimpleName("setVersion");
        mdSetter.setName(sn);
        SingleVariableDeclaration svd = rewriter.getAST().newSingleVariableDeclaration();
        variableName = rewriter.getAST().newSimpleName(version);
        svd.setName(variableName);
        typeName = rewriter.getAST().newName(versionType);
        type = rewriter.getAST().newSimpleType(typeName);
        svd.setType(type);
        mdSetter.parameters().add(svd);
        // 
        ListRewrite lrw = rewriter.getListRewrite(node, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
        if (entityInfo.getVersionFieldGetter() != FieldGetterType.FIELD && entityInfo.getVersionFieldGetter() != FieldGetterType.FIELD_GETTER) {
            lrw.insertLast(fieldVersion, null);
        }
        if (entityInfo.getVersionFieldGetter() != FieldGetterType.GETTER && entityInfo.getVersionFieldGetter() != FieldGetterType.FIELD_GETTER) {
            lrw.insertLast(mdGetter, null);
            lrw.insertLast(mdSetter, null);
        }
        if (annotationStyle == AnnotStyle.FIELDS) {
            MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
            matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_VERSION));
            lrw = rewriter.getListRewrite(fieldVersion, FieldDeclaration.MODIFIERS2_PROPERTY);
            lrw.insertFirst(matd, null);
        } else if (annotationStyle == AnnotStyle.GETTERS) {
            MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
            matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_VERSION));
            lrw = rewriter.getListRewrite(mdGetter, MethodDeclaration.MODIFIERS2_PROPERTY);
            lrw.insertFirst(matd, null);
        }
    }
    return true;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Assignment(org.eclipse.jdt.core.dom.Assignment) SimpleType(org.eclipse.jdt.core.dom.SimpleType) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) MarkerAnnotation(org.eclipse.jdt.core.dom.MarkerAnnotation) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) Modifier(org.eclipse.jdt.core.dom.Modifier)

Example 9 with Modifier

use of org.eclipse.jdt.core.dom.Modifier in project eclipse-cs by checkstyle.

the class DesignForExtensionQuickfix method handleGetCorrectingASTVisitor.

/**
 * {@inheritDoc}
 */
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {
    return new ASTVisitor() {

        @SuppressWarnings("unchecked")
        @Override
        public boolean visit(MethodDeclaration node) {
            // recalculate start position because optional javadoc is mixed
            // into the original start position
            int pos = node.getStartPosition() + (node.getJavadoc() != null ? node.getJavadoc().getLength() + JAVADOC_COMMENT_LENGTH : 0);
            if (containsPosition(lineInfo, pos)) {
                if (!Modifier.isFinal(node.getModifiers())) {
                    Modifier finalModifier = node.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD);
                    node.modifiers().add(finalModifier);
                    // reorder modifiers into their correct order
                    List<ASTNode> reorderedModifiers = ModifierOrderQuickfix.reOrderModifiers(node.modifiers());
                    node.modifiers().clear();
                    node.modifiers().addAll(reorderedModifiers);
                }
            }
            return true;
        }
    };
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Modifier(org.eclipse.jdt.core.dom.Modifier) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 10 with Modifier

use of org.eclipse.jdt.core.dom.Modifier in project eclipse-cs by checkstyle.

the class FinalParametersQuickfix method handleGetCorrectingASTVisitor.

/**
 * {@inheritDoc}
 */
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {
    return new ASTVisitor() {

        @SuppressWarnings("unchecked")
        @Override
        public boolean visit(SingleVariableDeclaration node) {
            if (containsPosition(node, markerStartOffset) && !Modifier.isFinal(node.getModifiers())) {
                if (!Modifier.isFinal(node.getModifiers())) {
                    Modifier finalModifier = node.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD);
                    node.modifiers().add(finalModifier);
                }
            }
            return true;
        }
    };
}
Also used : SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) Modifier(org.eclipse.jdt.core.dom.Modifier) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Aggregations

Modifier (org.eclipse.jdt.core.dom.Modifier)49 IExtendedModifier (org.eclipse.jdt.core.dom.IExtendedModifier)39 ASTNode (org.eclipse.jdt.core.dom.ASTNode)16 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)12 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)11 TextEditGroup (org.eclipse.text.edits.TextEditGroup)11 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)9 AST (org.eclipse.jdt.core.dom.AST)7 ArrayList (java.util.ArrayList)6 Block (org.eclipse.jdt.core.dom.Block)6 ModifierKeyword (org.eclipse.jdt.core.dom.Modifier.ModifierKeyword)6 SimpleType (org.eclipse.jdt.core.dom.SimpleType)6 Type (org.eclipse.jdt.core.dom.Type)6 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)5 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)5 Expression (org.eclipse.jdt.core.dom.Expression)5 SimpleName (org.eclipse.jdt.core.dom.SimpleName)5 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)5 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)5 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)4