Search in sources :

Example 1 with FieldDeclaration

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

the class LocalVariableIndex method internalPerform.

private static int internalPerform(BodyDeclaration methodOrInitializer) {
    // we have to find the outermost method/initializer/field declaration since a local or anonymous
    // type can reference final variables from the outer scope.
    BodyDeclaration target = methodOrInitializer;
    ASTNode parent = target.getParent();
    while (parent != null) {
        if (parent instanceof MethodDeclaration || parent instanceof Initializer || parent instanceof FieldDeclaration) {
            target = (BodyDeclaration) parent;
        }
        parent = parent.getParent();
    }
    return doPerform(target);
}
Also used : Initializer(org.eclipse.jdt.core.dom.Initializer) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 2 with FieldDeclaration

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

the class RenameFieldProcessor method addDelegates.

private RefactoringStatus addDelegates() throws JavaModelException, CoreException {
    RefactoringStatus status = new RefactoringStatus();
    CompilationUnitRewrite rewrite = new CompilationUnitRewrite(fField.getCompilationUnit());
    rewrite.setResolveBindings(true);
    // add delegate for the field
    if (RefactoringAvailabilityTester.isDelegateCreationAvailable(fField)) {
        FieldDeclaration fieldDeclaration = ASTNodeSearchUtil.getFieldDeclarationNode(fField, rewrite.getRoot());
        if (fieldDeclaration.fragments().size() > 1) {
            status.addWarning(Messages.format(RefactoringCoreMessages.DelegateCreator_cannot_create_field_delegate_more_than_one_fragment, BasicElementLabels.getJavaElementName(fField.getElementName())), JavaStatusContext.create(fField));
        } else if (((VariableDeclarationFragment) fieldDeclaration.fragments().get(0)).getInitializer() == null) {
            status.addWarning(Messages.format(RefactoringCoreMessages.DelegateCreator_cannot_create_field_delegate_no_initializer, BasicElementLabels.getJavaElementName(fField.getElementName())), JavaStatusContext.create(fField));
        } else {
            DelegateFieldCreator creator = new DelegateFieldCreator();
            creator.setDeclareDeprecated(fDelegateDeprecation);
            creator.setDeclaration(fieldDeclaration);
            creator.setNewElementName(getNewElementName());
            creator.setSourceRewrite(rewrite);
            creator.prepareDelegate();
            creator.createEdit();
        }
    }
    // there may be getters even if the field is static final
    if (getGetter() != null && fRenameGetter)
        addMethodDelegate(getGetter(), getNewGetterName(), rewrite);
    if (getSetter() != null && fRenameSetter)
        addMethodDelegate(getSetter(), getNewSetterName(), rewrite);
    final CompilationUnitChange change = rewrite.createChange(true);
    if (change != null) {
        change.setKeepPreviewEdits(true);
        fChangeManager.manage(fField.getCompilationUnit(), change);
    }
    return status;
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) DelegateFieldCreator(org.eclipse.jdt.internal.corext.refactoring.delegates.DelegateFieldCreator) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 3 with FieldDeclaration

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

the class SuppressWarningsSubProcessor method addSuppressWarningsProposalIfPossible.

/**
	 * Adds a SuppressWarnings proposal if possible and returns whether parent nodes should be processed or not (and with what relevance).
	 *
	 * @param cu the compilation unit
	 * @param node the node on which to add a SuppressWarning token
	 * @param warningToken the warning token to add
	 * @param relevance the proposal's relevance
	 * @param proposals collector to which the proposal should be added
	 * @return <code>0</code> if no further proposals should be added to parent nodes, or the relevance of the next proposal
	 *
	 * @since 3.6
	 */
private static int addSuppressWarningsProposalIfPossible(ICompilationUnit cu, ASTNode node, String warningToken, int relevance, Collection<ICommandAccess> proposals) {
    ChildListPropertyDescriptor property;
    String name;
    boolean isLocalVariable = false;
    switch(node.getNodeType()) {
        case ASTNode.SINGLE_VARIABLE_DECLARATION:
            property = SingleVariableDeclaration.MODIFIERS2_PROPERTY;
            name = ((SingleVariableDeclaration) node).getName().getIdentifier();
            isLocalVariable = true;
            break;
        case ASTNode.VARIABLE_DECLARATION_STATEMENT:
            property = VariableDeclarationStatement.MODIFIERS2_PROPERTY;
            name = getFirstFragmentName(((VariableDeclarationStatement) node).fragments());
            isLocalVariable = true;
            break;
        case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
            property = VariableDeclarationExpression.MODIFIERS2_PROPERTY;
            name = getFirstFragmentName(((VariableDeclarationExpression) node).fragments());
            isLocalVariable = true;
            break;
        case ASTNode.TYPE_DECLARATION:
            property = TypeDeclaration.MODIFIERS2_PROPERTY;
            name = ((TypeDeclaration) node).getName().getIdentifier();
            break;
        case ASTNode.ANNOTATION_TYPE_DECLARATION:
            property = AnnotationTypeDeclaration.MODIFIERS2_PROPERTY;
            name = ((AnnotationTypeDeclaration) node).getName().getIdentifier();
            break;
        case ASTNode.ENUM_DECLARATION:
            property = EnumDeclaration.MODIFIERS2_PROPERTY;
            name = ((EnumDeclaration) node).getName().getIdentifier();
            break;
        case ASTNode.FIELD_DECLARATION:
            property = FieldDeclaration.MODIFIERS2_PROPERTY;
            name = getFirstFragmentName(((FieldDeclaration) node).fragments());
            break;
        // case ASTNode.INITIALIZER: not used, because Initializer cannot have annotations
        case ASTNode.METHOD_DECLARATION:
            property = MethodDeclaration.MODIFIERS2_PROPERTY;
            //$NON-NLS-1$
            name = ((MethodDeclaration) node).getName().getIdentifier() + "()";
            break;
        case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
            property = AnnotationTypeMemberDeclaration.MODIFIERS2_PROPERTY;
            //$NON-NLS-1$
            name = ((AnnotationTypeMemberDeclaration) node).getName().getIdentifier() + "()";
            break;
        case ASTNode.ENUM_CONSTANT_DECLARATION:
            property = EnumConstantDeclaration.MODIFIERS2_PROPERTY;
            name = ((EnumConstantDeclaration) node).getName().getIdentifier();
            break;
        default:
            return relevance;
    }
    String label = Messages.format(CorrectionMessages.SuppressWarningsSubProcessor_suppress_warnings_label, new String[] { warningToken, BasicElementLabels.getJavaElementName(name) });
    ASTRewriteCorrectionProposal proposal = new SuppressWarningsProposal(warningToken, label, cu, node, property, relevance);
    proposals.add(proposal);
    return isLocalVariable ? relevance - 1 : 0;
}
Also used : SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) ChildListPropertyDescriptor(org.eclipse.jdt.core.dom.ChildListPropertyDescriptor) EnumDeclaration(org.eclipse.jdt.core.dom.EnumDeclaration) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) EnumConstantDeclaration(org.eclipse.jdt.core.dom.EnumConstantDeclaration) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 4 with FieldDeclaration

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

the class DelegateFieldCreator method createBody.

@Override
protected ASTNode createBody(BodyDeclaration fd) throws JavaModelException {
    FieldDeclaration result = (FieldDeclaration) fd;
    Expression initializer = createDelegateFieldInitializer(result);
    return initializer;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 5 with FieldDeclaration

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

the class Java50Fix method getRawReference.

private static SimpleType getRawReference(SimpleName name, CompilationUnit compilationUnit) {
    SimpleName[] names = LinkedNodeFinder.findByNode(compilationUnit, name);
    for (int j = 0; j < names.length; j++) {
        if (names[j].getParent() instanceof VariableDeclarationFragment) {
            VariableDeclarationFragment fragment = (VariableDeclarationFragment) names[j].getParent();
            if (fragment.getParent() instanceof VariableDeclarationStatement) {
                VariableDeclarationStatement statement = (VariableDeclarationStatement) fragment.getParent();
                ASTNode result = (ASTNode) statement.getStructuralProperty(VariableDeclarationStatement.TYPE_PROPERTY);
                if (isRawTypeReference(result))
                    return (SimpleType) result;
            } else if (fragment.getParent() instanceof FieldDeclaration) {
                FieldDeclaration declaration = (FieldDeclaration) fragment.getParent();
                ASTNode result = (ASTNode) declaration.getStructuralProperty(FieldDeclaration.TYPE_PROPERTY);
                if (isRawTypeReference(result))
                    return (SimpleType) result;
            }
        } else if (names[j].getParent() instanceof SingleVariableDeclaration) {
            SingleVariableDeclaration declaration = (SingleVariableDeclaration) names[j].getParent();
            ASTNode result = (ASTNode) declaration.getStructuralProperty(SingleVariableDeclaration.TYPE_PROPERTY);
            if (isRawTypeReference(result))
                return (SimpleType) result;
        } else if (names[j].getParent() instanceof MethodDeclaration) {
            MethodDeclaration methodDecl = (MethodDeclaration) names[j].getParent();
            ASTNode result = (ASTNode) methodDecl.getStructuralProperty(MethodDeclaration.RETURN_TYPE2_PROPERTY);
            if (isRawTypeReference(result))
                return (SimpleType) result;
        }
    }
    return null;
}
Also used : SimpleType(org.eclipse.jdt.core.dom.SimpleType) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Aggregations

FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)83 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)42 ASTNode (org.eclipse.jdt.core.dom.ASTNode)38 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)35 AST (org.eclipse.jdt.core.dom.AST)21 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)19 Type (org.eclipse.jdt.core.dom.Type)19 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)19 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)18 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)14 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)14 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)13 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)13 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)12 Block (org.eclipse.jdt.core.dom.Block)11 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)11 SimpleName (org.eclipse.jdt.core.dom.SimpleName)11 Initializer (org.eclipse.jdt.core.dom.Initializer)10 Javadoc (org.eclipse.jdt.core.dom.Javadoc)10 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)10