Example usage for org.eclipse.jdt.core.dom MethodDeclaration getNodeType

List of usage examples for org.eclipse.jdt.core.dom MethodDeclaration getNodeType

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom MethodDeclaration getNodeType.

Prototype

public final int getNodeType() 

Source Link

Document

Returns an integer value identifying the type of this concrete AST node.

Usage

From source file:changenodes.matching.BestLeafTreeMatcher.java

License:Apache License

private void matchIdenticalMethods(ASTNode left, ASTNode right) {
    //fix in which some methods got pretty bad matches
    //feels pretty dirty though (but so does this whole file)
    for (Iterator<ASTNode> iterator = new BreadthFirstNodeIterator(left); iterator.hasNext();) {
        ASTNode n = iterator.next();/*from   w w w.j a va 2 s. c o  m*/
        ASTNode bestMatch = null;
        if (n.getNodeType() == ASTNode.METHOD_DECLARATION) {
            MethodDeclaration x = (MethodDeclaration) n;
            if (!leftMatching.containsKey(x)) {
                for (Iterator<ASTNode> rightIterator = new BreadthFirstNodeIterator(right); rightIterator
                        .hasNext();) {
                    ASTNode y = rightIterator.next();
                    if (y.getNodeType() == x.getNodeType() && x.subtreeMatch(new ASTMatcher(), y)) {
                        bestMatch = y;
                        break;
                    }
                }
            }
            //we have found the best node, lets now match them together
            //afaik this is also not in the original paper, but we sometimes got some weird matches
            if (bestMatch != null) {
                markMatchedNode(x, bestMatch);
            }
        }
    }
}

From source file:org.eclipse.objectteams.internal.jdt.nullity.quickfix.RewriteOperations.java

License:Open Source License

static SignatureAnnotationRewriteOperation createAddAnnotationOperation(CompilationUnit compilationUnit,
        IProblemLocation problem, String annotationToAdd, String annotationToRemove,
        Set<String> handledPositions, boolean thisUnitOnly, boolean allowRemove) {
    ICompilationUnit cu = (ICompilationUnit) compilationUnit.getJavaElement();
    if (!JavaModelUtil.is50OrHigher(cu.getJavaProject()))
        return null;

    ASTNode selectedNode = problem.getCoveringNode(compilationUnit);
    if (selectedNode == null)
        return null;

    ASTNode declaringNode = getDeclaringNode(selectedNode);

    switch (problem.getProblemId()) {
    case IllegalDefinitionToNonNullParameter:
        //      case IllegalRedefinitionToNonNullParameter:
        // these affect another method
        break;/*from w  ww  .j  av  a 2s .co  m*/
    case IllegalReturnNullityRedefinition:
        if (declaringNode == null)
            declaringNode = selectedNode;
        break; // do propose changes even if we already have an annotation
    default:
        // if this method has annotations, don't change'em
        if (QuickFixes.hasExplicitNullAnnotation(cu, problem.getOffset()))
            return null;
    }

    SignatureAnnotationRewriteOperation result = null;
    String message = null;
    String annotationNameLabel = annotationToAdd;
    int lastDot = annotationToAdd.lastIndexOf('.');
    if (lastDot != -1)
        annotationNameLabel = annotationToAdd.substring(lastDot + 1);
    annotationNameLabel = BasicElementLabels.getJavaElementName(annotationNameLabel);

    if (selectedNode.getParent() instanceof MethodInvocation) {
        // DefiniteNullToNonNullParameter || PotentialNullToNonNullParameter
        MethodInvocation methodInvocation = (MethodInvocation) selectedNode.getParent();
        int paramIdx = methodInvocation.arguments().indexOf(selectedNode);
        IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
        compilationUnit = findCUForMethod(compilationUnit, cu, methodBinding);
        if (compilationUnit == null)
            return null;
        if (thisUnitOnly && !compilationUnit.getJavaElement().equals(cu))
            return null;
        ASTNode methodDecl = compilationUnit.findDeclaringNode(methodBinding.getKey());
        if (methodDecl == null)
            return null;
        message = Messages.format(FixMessages.QuickFixes_declare_method_parameter_nullness,
                annotationNameLabel);
        result = new ParameterAnnotationRewriteOperation(compilationUnit, (MethodDeclaration) methodDecl,
                annotationToAdd, annotationToRemove, paramIdx, allowRemove, message);
    } else if (declaringNode instanceof MethodDeclaration) {

        // complaint is in signature of this method

        MethodDeclaration declaration = (MethodDeclaration) declaringNode;

        switch (problem.getProblemId()) {
        case ParameterLackingNonNullAnnotation:
        case ParameterLackingNullableAnnotation:
        case IllegalDefinitionToNonNullParameter:
        case IllegalRedefinitionToNonNullParameter:
        case IProblem.NonNullLocalVariableComparisonYieldsFalse:
        case IProblem.RedundantNullCheckOnNonNullLocalVariable:
            // statements suggest changing parameters:
            if (declaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
                String paramName = findAffectedParameterName(selectedNode);
                if (paramName != null) {
                    message = Messages.format(FixMessages.QuickFixes_declare_method_parameter_nullness,
                            annotationNameLabel);
                    result = new ParameterAnnotationRewriteOperation(compilationUnit, declaration,
                            annotationToAdd, annotationToRemove, paramName, allowRemove, message);
                }
            }
            break;
        case IllegalReturnNullityRedefinition:
        case RequiredNonNullButProvidedNull:
        case RequiredNonNullButProvidedPotentialNull:
        case RequiredNonNullButProvidedUnknown:
            message = Messages.format(FixMessages.QuickFixes_declare_method_return_nullness,
                    annotationNameLabel);
            result = new ReturnAnnotationRewriteOperation(compilationUnit, declaration, annotationToAdd,
                    annotationToRemove, allowRemove, message);
            break;
        }

    }
    return result;
}