Example usage for org.eclipse.jdt.internal.core CompilationUnit getElementAt

List of usage examples for org.eclipse.jdt.internal.core CompilationUnit getElementAt

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core CompilationUnit getElementAt.

Prototype

@Override
public IJavaElement getElementAt(int position) throws JavaModelException 

Source Link

Usage

From source file:org.eclipse.ajdt.core.parserbridge.AJCompilationUnitProblemFinder.java

License:Open Source License

public static boolean isARealProblem(CategorizedProblem categorizedProblem, CompilationUnit unit,
        AJProjectModelFacade model, boolean hasModel, boolean isJavaFileInAJEditor) {

    int numArgs = categorizedProblem.getArguments() == null ? 0 : categorizedProblem.getArguments().length;
    String firstArg = numArgs > 0 ? categorizedProblem.getArguments()[0] : null;
    String secondArg = numArgs > 1 ? categorizedProblem.getArguments()[1] : null;

    int id = categorizedProblem.getID();

    if (!hasModel && (id == IProblem.UndefinedType || id == IProblem.UndefinedName
            || id == IProblem.UnresolvedVariable || // AJDT 3.6
            id == IProblem.UndefinedField || id == IProblem.UndefinedMethod
            || id == IProblem.UndefinedConstructor || id == IProblem.IllegalCast
            || id == IProblem.AbstractMethodMustBeImplemented) // anonymous interface with ITDs implementing abstract method
    ) {/*from w ww.  j  a va 2 s  .co m*/
        // if there is no model, don't take any chances.
        // everything that might be an ITD reference is ignored
        return false;
    }

    if (categorizedProblem.getSourceStart() == 0) {
        // a place for all problems that don't have source locations
        // because they come from ITDs
        return false;
    }

    if (numArgs > 0 && id == IProblem.UndefinedMethod && (extraAspectMethods.contains(firstArg))
            || extraAspectMethods.contains(secondArg)) {
        // probably hasAspect or aspectOf
        return false;
    }

    if (numArgs > 1 && (id == IProblem.DuplicateField || id == IProblem.DuplicateMethod)
            && (aspectMemberNames.contains(firstArg) || aspectMemberNames.contains(secondArg))
            || declareAnnotationKinds.contains(firstArg) || declareAnnotationKinds.contains(secondArg)) {
        // declare statement if more than one exist in a file
        // or around advice if more than one of the same kind exists in the aspect
        return false;
    }

    if (numArgs > 1 && id == IProblem.DuplicateMethod && isTranslatedAdviceName(firstArg, secondArg)) {
        // more than one before or after advice exists
        // in same file with same number and types of arguments
        // as per bug 318132, before and after names are translated
        // to 'b' and 'a' respectively
        return false;
    }

    if (numArgs == 0 && id == IProblem.MissingReturnType) {
        // ITD constructors don't have return types
        // check the name to see if there is a $ in it
        String problemRegion = extractProblemRegion(categorizedProblem, unit);
        if (problemRegion.indexOf("$") != -1) {
            return false;
        }
        String[] parts = problemRegion.split("\\(");
        String name = parts[0].trim();
        if (aspectMemberNames.contains(name)) {
            // advice---before or after
            return false;
        }
    }

    if (numArgs == 0 && id == IProblem.InvalidExplicitConstructorCall) {
        // ITD constructor making explicit this() call.
        // lots of potential for false negatives
        return false;
    }

    if (numArgs == 0 && id == IProblem.MethodRequiresBody) {
        // Likely to be a pointcut definition
        return false;
    }

    if (numArgs == 2 && id == IProblem.ParsingErrorInsertToComplete && firstArg.equals(";")
            && secondArg.equals("FieldDeclaration")) {
        // might be a declare statement
        String problemRegion = extractProblemRegion(categorizedProblem, unit);
        if (aspectMemberNames.contains(problemRegion)) {
            return false;
        }
    }

    // this one is not used any more since the '@' is being removed from the text
    //        if (numArgs == 1 && 
    //                id == IProblem.ParsingErrorDeleteToken &&
    //                firstArg.equals("@")) {
    //            // likely to be declare annotation declaration
    //            // declare @type, declare @constructor, declare @method, declare @field
    //            String problemRegion = extractNextJavaIdentifier(unit, categorizedProblem.getSourceEnd());
    //            if (declareAnnotationKinds.contains(problemRegion)) {
    //                return false;
    //            }
    //        }

    if (numArgs == 1 && id == IProblem.UndefinedType && declareAnnotationKinds.contains(firstArg)) {
        // alternate error of declare annotations
        return false;
    }

    if (numArgs == 1 && id == IProblem.UndefinedType && firstArg.equals("declare")) {
        // from a declare declaration
        return false;
    }

    if (numArgs == 1 && id == IProblem.UndefinedType && firstArg.equals("pointcut")) {
        // from a pointcut declaration
        return false;
    }

    if (numArgs > 0 && (id == IProblem.UndefinedName || id == IProblem.UnresolvedVariable || // AJDT 3.6 
            id == IProblem.UndefinedField || id == IProblem.UndefinedMethod || id == IProblem.UndefinedType
            || id == IProblem.UndefinedConstructor)
            && isITDName(categorizedProblem, unit, model, isJavaFileInAJEditor)) {
        // a reference inside an aspect to an ITD that it declares
        return false;
    }

    if (hasModel && id == IProblem.ShouldReturnValue && categorizedProblem.getSourceStart() == 0
            && categorizedProblem.getSourceEnd() == 0) {
        // from an inserted ITD that has already been removed
        // this problem comes because the bodies of the ITDs inserted by ITDInserter 
        // are always empty even when there should be a return value
        return false;
    }

    if (hasModel && (id == IProblem.NotVisibleType || id == IProblem.MethodReducesVisibility)
            && categorizedProblem.getSourceStart() == 0) {
        // declare parents type that is not visible by current
        // type.  this is fine as long as it is visible
        // in the scope of the declare parents declaration.
        return false;
    }

    if ((id == IProblem.NotVisibleConstructor || id == IProblem.NotVisibleField
            || id == IProblem.NotVisibleMethod || id == IProblem.NotVisibleType)
            && isPrivilegedAspect(categorizedProblem, unit, isJavaFileInAJEditor)) {

        // a privileged aspect should be able to see all private/protected members
        return false;
    }

    try {

        if (numArgs > 1 && id == IProblem.DuplicateMethod && simpleNamesEquals(firstArg, secondArg)) {
            // bug 280385
            // no arg constructor ITD when the target type 
            // has an implicit no arg constructor

            IJavaElement elt = unit.getElementAt(categorizedProblem.getSourceStart());
            // here, check to see if the method name is the same as the 
            // type name. If so, then look for the default constructor,
            // if none exists, then we can ignore this problem
            if (elt.getElementType() == IJavaElement.TYPE) {
                IType type = (IType) elt;
                if (type.getElementName().equals(firstArg)) {
                    IMethod method = type.getMethod(type.getElementName(), new String[0]);
                    if (!method.exists()) {
                        return false;
                    }
                }
            }
        }

        if (id == IProblem.ReturnTypeMismatch && numArgs == 2
                && typeAtPositionIsArg(categorizedProblem, unit, firstArg)) {
            if (findLastSegment(getITDTargetType(categorizedProblem, unit, isJavaFileInAJEditor))
                    .equals(findLastSegment(secondArg))) {
                // bug 284358
                // this problem occurs when 'this' is returned from an ITD method
                // the resolver thinks there is a type mismath because it was 
                // expecting the aspect type (argument 1) instead of the ITD type
                // (argument 2)
                return false;
            }

            if (insideITD(categorizedProblem, unit, isJavaFileInAJEditor)
                    && isThisExpression(categorizedProblem, unit)) {
                // Bug 361170
                // Likely a this expresion that is casted to a super type of the ITD
                return false;
            }
        }

        if (numArgs > 0 && (id == IProblem.UndefinedMethod || id == IProblem.UndefinedName
                || id == IProblem.UnresolvedVariable) && // AJDT 3.6 
                (adviceBodyNames.contains(firstArg) || adviceBodyNames.contains(secondArg))
                && insideAdvice(categorizedProblem, unit)) {
            // proceed/thisJoinPoint... statement
            return false;
        }

        if (numArgs == 1 && (id == IProblem.ParsingErrorDeleteToken || id == IProblem.ParsingErrorDeleteTokens)
                && aspectMemberNames.contains(firstArg)
                && insideITD(categorizedProblem, unit, isJavaFileInAJEditor)) {
            // the implements or extends clause of a declare statement
            return false;
        }

        if (id == IProblem.ParameterMismatch && insideITD(categorizedProblem, unit, isJavaFileInAJEditor)) {
            // Probably a reference to 'this' inside an ITD
            // compiler thinks 'this' refers to the containing aspect
            // not the target type
            return false;
        }

        if (id == IProblem.AbstractMethodInAbstractClass
                && insideITD(categorizedProblem, unit, isJavaFileInAJEditor)) {
            // an abstract method ITD inside a concrete aspect
            // ITDs are allowed to be abstract if the target
            // type is an abstract class, but problem finder does not know this
            return false;
        }

        if (id == IProblem.IllegalAbstractModifierCombinationForMethod
                && insideITD(categorizedProblem, unit, isJavaFileInAJEditor)) {
            // private abstract itd in aspect
            return false;
        }

        if (id == IProblem.UnusedPrivateField && (insideITD(categorizedProblem, unit, isJavaFileInAJEditor)
                || getITDNames(unit, model).size() > 0)) {
            // private itd is said to be unused, even if it is really used elsewhere
            // also, if this type has some ITDs, then we really don't know if it is used in the
            // ITDs, so just be safe and ignore this problem
            return false;
        }

        if (numArgs > 0 && (id == IProblem.UndefinedName || id == IProblem.UnresolvedVariable || // AJDT 3.6 
                id == IProblem.UndefinedField || id == IProblem.UndefinedMethod || id == IProblem.UndefinedType
                || id == IProblem.UndefinedConstructor)
                && insideITD(categorizedProblem, unit, isJavaFileInAJEditor)) {
            // likely to be a reference inside an ITD to a name in the target type
            // also will erroneously filter out truly undefined names
            return false;
        }

        if (numArgs > 0 && (id == IProblem.UndefinedType || id == IProblem.InternalTypeNameProvided)
                && firstArg.indexOf('$') != -1) {
            // based on previous test, we are not inside of an ITD, 
            // so we may be defining a field or variable with a 
            // type of an inner class using a '.'.
            // the AspectsConvertingParser converts this '.' into a '$'
            // ignore.

            return false;
        }

        if (id == IProblem.NonStaticAccessToStaticField
                && isITDName(categorizedProblem, unit, model, isJavaFileInAJEditor)) {
            // this is a reference to an ITD field on an interface
            // compiler thinks that all fields in interfaces are static final
            return false;
        }

        if ((id == IProblem.UnhandledException || id == IProblem.UnhandledExceptionInImplicitConstructorCall
                || id == IProblem.UnhandledExceptionInDefaultConstructor)
                && (!model.hasModel() || isSoftened(categorizedProblem, unit, model, isJavaFileInAJEditor))) {
            return false;
        }

        if (id == IProblem.UninitializedBlankFinalField
                && unit.getElementAt(categorizedProblem.getSourceStart()) == null) {
            // likely to be inserted dummy fields for organize imports
            // this only happens when the last declaration is an interface
            // these dummy fields are implicitly converted to public static final
            return false;
        }

        if (id == IProblem.AbstractMethodsInConcreteClass
                && isAspect(categorizedProblem, unit, isJavaFileInAJEditor)) {
            /* AJDT 1.7 */
            // an aspect that has an abstract ITD will have this problem
            // in this case it is a spurious problem.  Filter it
            // unfortunately, this also means filtering real problems
            // where concrete aspects have abstract methods
            // new for 1.7
            return false;
        }

        if (id == IProblem.JavadocMissingReturnTag && insidePointcut(categorizedProblem, unit)) {
            // pointcuts are parsed as methods with 'pointcut' 
            // as the return type
            // when JavaDoc checking is set, the parser thinks that
            // 'pointcut' should have its own javadoc tag
            return false;
        }

        if (numArgs == 1 && id == IProblem.ShouldReturnValue && firstArg.equals("int")
                && insideAdvice(categorizedProblem, unit)) {
            // Bug 318132: after keyword is changed to 'int a' to avoid throwing exceptions while 
            // evaluating variables during debug
            return false;
        }

        if ((id == IProblem.InvalidTypeForCollection || id == IProblem.InvalidTypeForCollectionTarget14
                || id == IProblem.IncompatibleTypesInConditionalOperator || id == IProblem.IllegalCast)
                && insideITD(categorizedProblem, unit, isJavaFileInAJEditor) &&
                // I wish there were a more precise way of doing this.  Need to 
                // look for a 'this' expression.
                extractProblemRegion(categorizedProblem, unit).contains("this")) {

            // Bug 347021 
            // a 'this' expression in an ITD refers to the target type, not the aspect.
            // these problems here indicate that the aspect type is being used instead 
            // of the target type.
            return false;
        }

    } catch (JavaModelException e) {
    }

    if (id == IProblem.AbstractMethodMustBeImplemented
            && isITDName(categorizedProblem, unit, model, isJavaFileInAJEditor)) {
        // a type implements an interface with an ITD method on it
        return false;
    }

    if (id == IProblem.AbstractMethodMustBeImplemented
            && (!hasModel || isAbstractITD(categorizedProblem, model, unit, isJavaFileInAJEditor))) {
        // this one is very tricky and rare.
        // there is a abstract method ITD defined on a supertype
        // since this type was altered using AspectConvertingParser, 
        // the implementation of this abstract method is not necessarily there
        return false;
    }
    return true;
}

From source file:org.eclipse.ajdt.core.parserbridge.AJCompilationUnitProblemFinder.java

License:Open Source License

private static String getTypeNameAtPosition(CategorizedProblem categorizedProblem, CompilationUnit unit)
        throws JavaModelException {
    IJavaElement elt = unit.getElementAt(categorizedProblem.getSourceStart());
    IType type = elt != null ? (IType) elt.getAncestor(IJavaElement.TYPE) : null;
    if (type == null) {
        // just return the name of the CU
        int dotIndex = unit.getElementName().indexOf('.');
        if (dotIndex > 0) {
            return unit.getElementName().substring(0, dotIndex);
        } else {/*ww w.  j  av  a  2s .co  m*/
            return unit.getElementName();
        }
    }
    return type.getElementName();
}

From source file:org.eclipse.ajdt.core.parserbridge.AJCompilationUnitProblemFinder.java

License:Open Source License

private static boolean insideAdvice(CategorizedProblem categorizedProblem, CompilationUnit unit)
        throws JavaModelException {
    IJavaElement candidate = unit.getElementAt(categorizedProblem.getSourceStart());
    while (candidate != null && !(candidate instanceof ICompilationUnit)) {
        if (candidate instanceof AdviceElement) {
            return true;
        }/*w ww  .j  av a 2s. c  o m*/
        candidate = candidate.getParent();
    }
    return false;
}

From source file:org.eclipse.ajdt.core.parserbridge.AJCompilationUnitProblemFinder.java

License:Open Source License

private static boolean insidePointcut(CategorizedProblem categorizedProblem, CompilationUnit unit)
        throws JavaModelException {
    IJavaElement candidate = unit.getElementAt(categorizedProblem.getSourceStart());
    while (candidate != null && !(candidate instanceof ICompilationUnit)) {
        if (candidate instanceof PointcutElement) {
            return true;
        }/*from ww w.  j a va  2  s .  c om*/
        candidate = candidate.getParent();
    }
    return false;
}

From source file:org.eclipse.ajdt.core.parserbridge.AJCompilationUnitProblemFinder.java

License:Open Source License

private static boolean isSoftened(CategorizedProblem problem, CompilationUnit unit, AJProjectModelFacade model,
        boolean isJavaFileInAJEditor) throws JavaModelException {
    if (isJavaFileInAJEditor) {
        // we don't know...be safe and 
        // let compiler do the errors
        return true;
    }//w  w  w .  j  av  a 2 s  . co m
    IJavaElement elt = unit.getElementAt(problem.getSourceStart());
    List softens = model.getRelationshipsForElement(elt, AJRelationshipManager.SOFTENED_BY, true);
    return softens.size() > 0;
}

From source file:org.eclipse.ajdt.core.parserbridge.AJCompilationUnitProblemFinder.java

License:Open Source License

private static boolean isAspect(CategorizedProblem problem, CompilationUnit unit,
        boolean isJavaFileInAJEditor) {
    if (isJavaFileInAJEditor) {
        // we don't know...be safe and 
        // let compiler do the errors
        return true;
    }/* ww  w  .j  a va 2 s  .c  o  m*/

    if (unit instanceof AJCompilationUnit) {
        try {
            IJavaElement elt = unit.getElementAt(problem.getSourceStart());
            if (elt != null) {
                IType type = (IType) elt.getAncestor(IJavaElement.TYPE);
                return type != null && type instanceof AspectElement;
            }
        } catch (JavaModelException e) {
        }
    }
    return false;
}

From source file:org.eclipse.ajdt.core.parserbridge.AJCompilationUnitProblemFinder.java

License:Open Source License

private static boolean isPrivilegedAspect(CategorizedProblem problem, CompilationUnit unit,
        boolean isJavaFileInAJEditor) {
    if (isJavaFileInAJEditor) {
        // we don't know...be safe and 
        // let compiler do the errors
        return true;
    }/*  ww w . j  a  v a 2s.c om*/

    if (unit instanceof AJCompilationUnit) {
        try {
            IJavaElement elt = unit.getElementAt(problem.getSourceStart());
            if (elt != null) {
                IType type = (IType) elt.getAncestor(IJavaElement.TYPE);
                if (type != null && type instanceof AspectElement) {
                    AspectElement aspectType = (AspectElement) type;
                    return aspectType.isPrivileged();
                }
            }
        } catch (JavaModelException e) {
        }
    }
    return false;
}

From source file:org.eclipse.ajdt.core.parserbridge.AJCompilationUnitProblemFinder.java

License:Open Source License

private static boolean insideITD(CategorizedProblem categorizedProblem, CompilationUnit unit,
        boolean isJavaFileInAJEditor) throws JavaModelException {
    if (isJavaFileInAJEditor) {
        // we don't know...be safe and 
        // let compiler do the errors
        return true;
    }/*  w  w  w. jav a  2 s. c o m*/
    IJavaElement elementAt = unit.getElementAt(categorizedProblem.getSourceStart());
    return elementAt instanceof IntertypeElement || elementAt instanceof DeclareElement;
}

From source file:org.eclipse.ajdt.core.parserbridge.AJCompilationUnitProblemFinder.java

License:Open Source License

private static String getITDTargetType(CategorizedProblem categorizedProblem, CompilationUnit unit,
        boolean isJavaFileInAJEditor) throws JavaModelException {
    if (isJavaFileInAJEditor) {
        // we don't know...be safe and 
        // let compiler do the errors
        return "";
    }//from   www.  j a  v  a2  s .  c  om
    IJavaElement elementAt = unit.getElementAt(categorizedProblem.getSourceStart());
    if (elementAt instanceof IntertypeElement) {
        IntertypeElement itd = (IntertypeElement) elementAt;
        return new String(itd.getTargetType());
    }
    return "";
}