Example usage for org.eclipse.jdt.core IJavaElement getElementName

List of usage examples for org.eclipse.jdt.core IJavaElement getElementName

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaElement getElementName.

Prototype

String getElementName();

Source Link

Document

Returns the name of this element.

Usage

From source file:org.eclipse.ajdt.core.text.ITDAwareSelectionRequestor.java

License:Open Source License

/**
 * for itits:/*from w  w w . j  a v a2 s. c om*/
 * If the initial type finding returns null, then look for a dot (ie- an inner type) in the type name
 * Get the parent type and look to see if it has any aspect declarations on it of the name of the inner
 * type.  if so, then use that one
 */
private IType findType(char[] declaringTypePackageName, char[] declaringTypeName) throws JavaModelException {
    if (javaProject == null) {
        javaProject = currentUnit.getJavaProject();
    }
    IType type = javaProject.findType(toQualifiedName(declaringTypePackageName, declaringTypeName));
    if (type != null) {
        return type;
    } else {
        // check to see if this type is an ITIT
        int index = CharOperation.lastIndexOf('.', declaringTypeName);
        if (index >= 0) {
            // definitely an inner type...now get the enclosing type to look for ITDs
            char[] enclosingTypeName = CharOperation.subarray(declaringTypeName, 0, index);
            char[] innerTypeName = CharOperation.subarray(declaringTypeName, index + 1,
                    declaringTypeName.length);
            IType enclosingType = javaProject
                    .findType(toQualifiedName(declaringTypePackageName, enclosingTypeName));
            if (enclosingType != null) {
                String innerTypeStr = String.valueOf(innerTypeName);
                IType innerType = enclosingType.getType(innerTypeStr);
                if (innerType.exists()) {
                    // a standard inner type
                    // probably won't get here since should have been returned above
                    return innerType;
                } else if (model.hasModel()) {
                    // now check to see if the type has any ITITs declared on it
                    List<IJavaElement> rels = model.getRelationshipsForElement(enclosingType,
                            AJRelationshipManager.ASPECT_DECLARATIONS);
                    for (IJavaElement rel : rels) {
                        if (rel.getElementType() == IJavaElement.TYPE
                                && innerTypeStr.equals(rel.getElementName())) {
                            return (IType) rel;
                        }
                    }
                }
            }
        }
    }
    // not found...probably an error or the model hasn't been built.
    return null;
}

From source file:org.eclipse.ajdt.core.text.ITDAwareSelectionRequestor.java

License:Open Source License

public void acceptMethod(char[] declaringTypePackageName, char[] declaringTypeName,
        String enclosingDeclaringTypeSignature, char[] selector, char[][] parameterPackageNames,
        char[][] parameterTypeNames, String[] parameterSignatures, char[][] typeParameterNames,
        char[][][] typeParameterBoundNames, boolean isConstructor, boolean isDeclaration, char[] uniqueKey,
        int start, int end) {
    try {//from   ww w .ja  va2s .co  m
        IType targetType = findType(declaringTypePackageName, declaringTypeName);
        if (targetType == null) {
            return;
        }

        String[] simpleParameterSigs;
        if (parameterSignatures != null) {
            simpleParameterSigs = new String[parameterSignatures.length];
            for (int i = 0; i < parameterSignatures.length; i++) {
                simpleParameterSigs[i] = toSimpleName(parameterSignatures[i]);
            }
        } else {
            simpleParameterSigs = null;
        }

        List<IJavaElement> itds = ensureModel(targetType).getRelationshipsForElement(targetType,
                AJRelationshipManager.ASPECT_DECLARATIONS);
        for (IJavaElement elt : itds) {
            if (matchedMethod(elt, selector, simpleParameterSigs)) {
                accepted.add(elt);
                return;
            }
        }

        IntertypeElement itd = maybeGetITD(start);
        String selectorStr = String.valueOf(selector);
        if (itd != null && !isDeclaration) {
            // if we are selecting inside of an ITD and the method being matched is a regular method, we find it here.
            IMethod method = targetType.getMethod(selectorStr, parameterSignatures);
            if (method.exists()) {
                accepted.add(method);
                return;
            }
        }

        // still need to determine if the ITD declaration itself is being selected

        // now check to see if we actually found a method in an ITIT
        IJavaElement parent = targetType.getParent();
        if (parent.getElementType() == IJavaElement.TYPE) {
            // definitely an inner type.  If the outer type does not match,
            // then we know that this was from an ITIT
            char[] enclosingName = (parent.getElementName() + ".").toCharArray();
            if (!CharOperation.prefixEquals(enclosingName, declaringTypeName)) {
                IMethod[] methods = targetType.getMethods();
                for (IMethod method : methods) {
                    if (method.getElementName().equals(selectorStr)
                            && matchedParameters(simpleParameterSigs, method.getParameterTypes())) {
                        accepted.add(method);
                    }
                }
            }
        }

    } catch (JavaModelException e) {
    }
}

From source file:org.eclipse.ajdt.core.text.ITDAwareSelectionRequestor.java

License:Open Source License

public void acceptType(char[] packageName, char[] annotationName, int modifiers, boolean isDeclaration,
        char[] genericTypeSignature, int start, int end) {
    try {//  w w  w .j  av a2 s.c o  m
        int origStart = AspectsConvertingParser.translatePositionToBeforeChanges(start, replacements);
        int origEnd = AspectsConvertingParser.translatePositionToBeforeChanges(end, replacements);
        IntertypeElement itd = maybeGetITD(origStart);
        if (itd != null) {
            // find out if we are selecting the target type name part of an itd
            // itd.getNameRange() returns the range of the name, but excludes the target type.  Must subtract from there.  
            // Make assumption that there are no spaces
            // or comments between '.' and the rest of the name
            ISourceRange nameRange = itd.getNameRange();

            String itdName = itd.getElementName();
            int typeNameLength = Math.max(itdName.lastIndexOf('.'), 0);
            String typeName = itdName.substring(0, typeNameLength);

            int typeNameStart;
            if (itd.getAJKind() == Kind.INTER_TYPE_CONSTRUCTOR) {
                typeNameStart = nameRange.getOffset();
            } else {
                typeNameStart = nameRange.getOffset() - 1 - typeName.length();
            }
            // now determine if the selected section is completely contained within the type name
            if (contained(origStart, origEnd, typeNameStart, typeNameStart + typeNameLength)) {
                IType targetType = itd.findTargetType();
                if (targetType != null && targetType.getFullyQualifiedName('.')
                        .equals(toQualifiedName(packageName, annotationName))) {
                    accepted.add(targetType);
                }
            }
        } else {

            // now check to see if we actually found an ITIT
            IType targetType = findType(packageName, annotationName);
            if (targetType != null) {
                IJavaElement parent = targetType.getParent();
                if (parent.getElementType() == IJavaElement.TYPE) {
                    // definitely an inner type.  If the outer type does not match,
                    // then we know that this was from an ITIT
                    char[] enclosingName = (parent.getElementName() + ".").toCharArray();
                    if (!CharOperation.prefixEquals(enclosingName, annotationName)) {
                        accepted.add(targetType);
                    }
                }
            }
        }
    } catch (JavaModelException e) {
    }
}

From source file:org.eclipse.ajdt.core.text.ITDAwareSelectionRequestor.java

License:Open Source License

private boolean matchedField(IJavaElement elt, char[] name) throws JavaModelException {
    if (elt instanceof IntertypeElement) {
        IntertypeElementInfo info = (IntertypeElementInfo) ((IntertypeElement) elt).getElementInfo();
        if (info.getAJKind() == Kind.INTER_TYPE_FIELD) {
            if (extractName(elt.getElementName()).equals(new String(name))) {
                return true;
            }//from w  w w.j  a  v a 2s .c  o  m
        }
    }
    return false;
}

From source file:org.eclipse.ajdt.core.text.ITDAwareSelectionRequestor.java

License:Open Source License

private boolean matchedMethod(IJavaElement elt, char[] selector, String[] simpleParameterSigs)
        throws JavaModelException {
    if (elt instanceof IntertypeElement) {
        IntertypeElement itd = (IntertypeElement) elt;
        IntertypeElementInfo info = (IntertypeElementInfo) ((IntertypeElement) elt).getElementInfo();
        if (info.getAJKind() == Kind.INTER_TYPE_METHOD || info.getAJKind() == Kind.INTER_TYPE_CONSTRUCTOR) {
            if (extractName(elt.getElementName()).equals(String.valueOf(selector))) {
                String[] itdParameterSigs = itd.getParameterTypes();
                if (itdParameterSigs == null || simpleParameterSigs == null) {
                    return (itdParameterSigs == null || itdParameterSigs.length == 0)
                            && (simpleParameterSigs == null || simpleParameterSigs.length == 0);
                }//w w w. j a va 2  s  . c o  m
                if (itdParameterSigs.length == simpleParameterSigs.length) {
                    return matchedParameters(simpleParameterSigs, itdParameterSigs);
                }
            }
        }
    }
    return false;
}

From source file:org.eclipse.ajdt.internal.core.refactoring.ITDAccessorRenameParticipant.java

License:Open Source License

private boolean isSetter(IJavaElement elt) throws JavaModelException {
    if (elt instanceof IntertypeElement && elt.getElementName().endsWith("." + getOldSetterName())) {
        IntertypeElement itd = (IntertypeElement) elt;
        String[] parameterTypes = itd.getParameterTypes();
        if (itd.getAJKind() == Kind.INTER_TYPE_METHOD && parameterTypes != null && parameterTypes.length == 1
                && parameterTypes[0].equals(field.getTypeSignature())
                && itd.getReturnType().equals(Signature.SIG_VOID)) {
            return true;
        }/*from  ww w  .j  a  v a2s .c o m*/
    }
    return false;
}

From source file:org.eclipse.ajdt.internal.core.refactoring.ITDAccessorRenameParticipant.java

License:Open Source License

private boolean isGetter(IJavaElement elt) throws JavaModelException {
    boolean nameMatch = false;
    boolean isFound = false;
    if (elt instanceof IntertypeElement && elt.getElementName().endsWith("." + getOldGetterName(false))) {
        nameMatch = true;/*from w  w w.ja v  a 2 s .  c o  m*/
    } else if (elt instanceof IntertypeElement && elt.getElementName().endsWith("." + getOldGetterName(true))) {
        nameMatch = true;
    }
    if (nameMatch) {
        useIsForBooleanGetter = isFound;
        IntertypeElement itd = (IntertypeElement) elt;
        String[] parameterTypes = itd.getParameterTypes();
        if (itd.getAJKind() == Kind.INTER_TYPE_METHOD && (parameterTypes == null || parameterTypes.length == 0)
                && itd.getReturnType().equals(field.getTypeSignature())) {
            return true;
        }
    }
    return false;
}

From source file:org.eclipse.ajdt.internal.core.search.AJDTSearchProvider.java

License:Open Source License

/**
 * convert ITD matches into the target types
 * Or ignore otherwise//from  w w  w .  j a v  a 2s .  c  o m
 * @throws JavaModelException 
 */
public IJavaElement filterJUnit4TestMatch(IJavaElement possibleTest) throws JavaModelException {
    // the results are returned as ResolvedSourceMethod, not an ITD
    // so must do a little work to get to the real ITD
    if (!(possibleTest instanceof IMethod)) {
        return possibleTest;
    }
    IJavaElement parent = possibleTest.getAncestor(IJavaElement.TYPE);
    if (parent instanceof AspectElement) {
        String itdName = possibleTest.getElementName().replace('$', '.');
        IntertypeElement matchingITD = findMatchingITD((AspectElement) parent, (IMethod) possibleTest, itdName);
        if (matchingITD != null) {
            return matchingITD.createMockDeclaration();
        }
    }
    return possibleTest;
}

From source file:org.eclipse.ajdt.internal.launching.LTWUtils.java

License:Open Source License

/**
 * Get the fully qualified name for the given aspect (e.g. package.Class
 * or package.EnclosingClass.InnerClass)
 * @param aspect//from  ww  w.  ja  v a2 s.  c o m
 * @return
 */
private static String getFullyQualifiedName(IType aspect) {
    StringBuffer sb = new StringBuffer();
    IJavaElement parent = aspect.getCompilationUnit().getParent();
    if (parent instanceof IPackageFragment && !parent.getElementName().equals("")) { //$NON-NLS-1$
        sb.append(parent.getElementName());
        sb.append("."); //$NON-NLS-1$
    }
    sb.append(getFullTypeName(aspect));
    return sb.toString();
}

From source file:org.eclipse.ajdt.internal.ui.ajdocexport.AJdocWriter.java

License:Open Source License

private void sortSourceElement(IJavaElement[] iJavaElements, List sourcefiles, List packages) {
    for (int i = 0; i < iJavaElements.length; i++) {
        IJavaElement element = iJavaElements[i];
        IPath p = element.getResource().getLocation();
        if (p == null)
            continue;

        if (element instanceof ICompilationUnit) {
            String relative = getPathString(p);
            sourcefiles.add(relative);//from  w w w  .  ja  va  2s. c  o m
        } else if (element instanceof IPackageFragment) {
            packages.add(element.getElementName());
        }
    }
}