Search in sources :

Example 11 with SourceRange

use of org.eclipse.jdt.core.SourceRange in project che by eclipse.

the class SourceMapper method enterAbstractMethod.

private void enterAbstractMethod(MethodInfo methodInfo) {
    if (this.typeDepth >= 0) {
        this.memberName[this.typeDepth] = new String(methodInfo.name);
        this.memberNameRange[this.typeDepth] = new SourceRange(methodInfo.nameSourceStart, methodInfo.nameSourceEnd - methodInfo.nameSourceStart + 1);
        this.memberDeclarationStart[this.typeDepth] = methodInfo.declarationStart;
        IType currentType = this.types[this.typeDepth];
        int currenTypeModifiers = this.typeModifiers[this.typeDepth];
        char[][] parameterTypes = methodInfo.parameterTypes;
        if (methodInfo.isConstructor && currentType.getDeclaringType() != null && !Flags.isStatic(currenTypeModifiers)) {
            IType declaringType = currentType.getDeclaringType();
            String declaringTypeName = declaringType.getElementName();
            if (declaringTypeName.length() == 0) {
                IClassFile classFile = declaringType.getClassFile();
                int length = parameterTypes != null ? parameterTypes.length : 0;
                char[][] newParameterTypes = new char[length + 1][];
                declaringTypeName = classFile.getElementName();
                declaringTypeName = declaringTypeName.substring(0, declaringTypeName.indexOf('.'));
                newParameterTypes[0] = declaringTypeName.toCharArray();
                if (length != 0) {
                    System.arraycopy(parameterTypes, 0, newParameterTypes, 1, length);
                }
                this.methodParameterTypes[this.typeDepth] = newParameterTypes;
            } else {
                int length = parameterTypes != null ? parameterTypes.length : 0;
                char[][] newParameterTypes = new char[length + 1][];
                newParameterTypes[0] = declaringTypeName.toCharArray();
                if (length != 0) {
                    System.arraycopy(parameterTypes, 0, newParameterTypes, 1, length);
                }
                this.methodParameterTypes[this.typeDepth] = newParameterTypes;
            }
        } else {
            this.methodParameterTypes[this.typeDepth] = parameterTypes;
        }
        this.methodParameterNames[this.typeDepth] = methodInfo.parameterNames;
        IMethod method = currentType.getMethod(this.memberName[this.typeDepth], convertTypeNamesToSigs(this.methodParameterTypes[this.typeDepth]));
        // type parameters
        if (methodInfo.typeParameters != null) {
            for (int i = 0, length = methodInfo.typeParameters.length; i < length; i++) {
                TypeParameterInfo typeParameterInfo = methodInfo.typeParameters[i];
                ITypeParameter typeParameter = method.getTypeParameter(new String(typeParameterInfo.name));
                setSourceRange(typeParameter, new SourceRange(typeParameterInfo.declarationStart, typeParameterInfo.declarationEnd - typeParameterInfo.declarationStart + 1), new SourceRange(typeParameterInfo.nameSourceStart, typeParameterInfo.nameSourceEnd - typeParameterInfo.nameSourceStart + 1));
            }
        }
        // parameters infos
        if (methodInfo.parameterInfos != null) {
            for (int i = 0, length = methodInfo.parameterInfos.length; i < length; i++) {
                ParameterInfo parameterInfo = methodInfo.parameterInfos[i];
                LocalVariableElementKey key = new LocalVariableElementKey(method, new String(parameterInfo.name));
                SourceRange[] allRanges = new SourceRange[] { new SourceRange(parameterInfo.declarationStart, parameterInfo.declarationEnd - parameterInfo.declarationStart + 1), new SourceRange(parameterInfo.nameSourceStart, parameterInfo.nameSourceEnd - parameterInfo.nameSourceStart + 1) };
                this.parametersRanges.put(key, allRanges);
                if (parameterInfo.modifiers != 0) {
                    if (this.finalParameters == null) {
                        this.finalParameters = new HashSet();
                    }
                    this.finalParameters.add(key);
                }
            }
        }
        // categories
        addCategories(method, methodInfo.categories);
    }
}
Also used : IClassFile(org.eclipse.jdt.core.IClassFile) ITypeParameter(org.eclipse.jdt.core.ITypeParameter) IType(org.eclipse.jdt.core.IType) IMethod(org.eclipse.jdt.core.IMethod) ISourceRange(org.eclipse.jdt.core.ISourceRange) SourceRange(org.eclipse.jdt.core.SourceRange) HashSet(java.util.HashSet)

Example 12 with SourceRange

use of org.eclipse.jdt.core.SourceRange in project che by eclipse.

the class SourceMapper method getSourceRange.

/**
     * Returns the <code>SourceRange</code> for the given element, or
     * {-1, -1} if no source range is known for the element.
     */
public SourceRange getSourceRange(IJavaElement element) {
    switch(element.getElementType()) {
        case IJavaElement.METHOD:
            if (((IMember) element).isBinary()) {
                IJavaElement[] el = getUnqualifiedMethodHandle((IMethod) element, false);
                if (el[1] != null && this.sourceRanges.get(el[0]) == null) {
                    element = getUnqualifiedMethodHandle((IMethod) element, true)[0];
                } else {
                    element = el[0];
                }
            }
            break;
        case IJavaElement.TYPE_PARAMETER:
            IJavaElement parent = element.getParent();
            if (parent.getElementType() == IJavaElement.METHOD) {
                IMethod method = (IMethod) parent;
                if (method.isBinary()) {
                    IJavaElement[] el = getUnqualifiedMethodHandle(method, false);
                    if (el[1] != null && this.sourceRanges.get(el[0]) == null) {
                        method = (IMethod) getUnqualifiedMethodHandle(method, true)[0];
                    } else {
                        method = (IMethod) el[0];
                    }
                    element = method.getTypeParameter(element.getElementName());
                }
            }
            break;
        case IJavaElement.LOCAL_VARIABLE:
            LocalVariableElementKey key = new LocalVariableElementKey(element.getParent(), element.getElementName());
            SourceRange[] ranges = (SourceRange[]) this.parametersRanges.get(key);
            if (ranges == null) {
                return UNKNOWN_RANGE;
            } else {
                return ranges[0];
            }
    }
    SourceRange[] ranges = (SourceRange[]) this.sourceRanges.get(element);
    if (ranges == null) {
        return UNKNOWN_RANGE;
    } else {
        return ranges[0];
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IMethod(org.eclipse.jdt.core.IMethod) ISourceRange(org.eclipse.jdt.core.ISourceRange) SourceRange(org.eclipse.jdt.core.SourceRange) IMember(org.eclipse.jdt.core.IMember)

Example 13 with SourceRange

use of org.eclipse.jdt.core.SourceRange in project che by eclipse.

the class InlineTempRefactoring method checkAssignments.

private RefactoringStatus checkAssignments(VariableDeclaration decl) {
    TempAssignmentFinder assignmentFinder = new TempAssignmentFinder(decl);
    getASTRoot().accept(assignmentFinder);
    if (!assignmentFinder.hasAssignments())
        return new RefactoringStatus();
    ASTNode firstAssignment = assignmentFinder.getFirstAssignment();
    int start = firstAssignment.getStartPosition();
    int length = firstAssignment.getLength();
    ISourceRange range = new SourceRange(start, length);
    RefactoringStatusContext context = JavaStatusContext.create(fCu, range);
    String message = Messages.format(RefactoringCoreMessages.InlineTempRefactoring_assigned_more_once, BasicElementLabels.getJavaElementName(decl.getName().getIdentifier()));
    return RefactoringStatus.createFatalErrorStatus(message, context);
}
Also used : RefactoringStatusContext(org.eclipse.ltk.core.refactoring.RefactoringStatusContext) ASTNode(org.eclipse.jdt.core.dom.ASTNode) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) ISourceRange(org.eclipse.jdt.core.ISourceRange) SourceRange(org.eclipse.jdt.core.SourceRange) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 14 with SourceRange

use of org.eclipse.jdt.core.SourceRange in project bndtools by bndtools.

the class MemberValuePairLocationRetriever method visit.

/**
     * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.SingleMemberAnnotation)
     */
@Override
public boolean visit(SingleMemberAnnotation node) {
    final IAnnotationBinding annotationBinding = node.resolveAnnotationBinding();
    if (annotationBinding != null) {
        final String nodeName = annotationBinding.getAnnotationType().getQualifiedName();
        boolean match;
        try {
            match = this.annotationNameMatch.test(nodeName);
        } catch (Exception e) {
            match = false;
        }
        if (match) {
            this.locatedSourceRange = new SourceRange(node.getValue().getStartPosition(), node.getValue().getLength());
        }
    }
    return false;
}
Also used : IAnnotationBinding(org.eclipse.jdt.core.dom.IAnnotationBinding) ISourceRange(org.eclipse.jdt.core.ISourceRange) SourceRange(org.eclipse.jdt.core.SourceRange)

Aggregations

SourceRange (org.eclipse.jdt.core.SourceRange)14 ISourceRange (org.eclipse.jdt.core.ISourceRange)13 IType (org.eclipse.jdt.core.IType)6 IMethod (org.eclipse.jdt.core.IMethod)4 IJavaElement (org.eclipse.jdt.core.IJavaElement)2 IMember (org.eclipse.jdt.core.IMember)2 ITypeParameter (org.eclipse.jdt.core.ITypeParameter)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 Expression (org.eclipse.jdt.core.dom.Expression)2 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)2 RefactoringStatusContext (org.eclipse.ltk.core.refactoring.RefactoringStatusContext)2 HashSet (java.util.HashSet)1 CoreException (org.eclipse.core.runtime.CoreException)1 IClassFile (org.eclipse.jdt.core.IClassFile)1 IField (org.eclipse.jdt.core.IField)1 IScanner (org.eclipse.jdt.core.compiler.IScanner)1 Assignment (org.eclipse.jdt.core.dom.Assignment)1 CastExpression (org.eclipse.jdt.core.dom.CastExpression)1 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)1 IAnnotationBinding (org.eclipse.jdt.core.dom.IAnnotationBinding)1