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

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

Introduction

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

Prototype

int FIELD

To view the source code for org.eclipse.jdt.core IJavaElement FIELD.

Click Source Link

Document

Constant representing a field.

Usage

From source file:org.eclim.plugin.jdt.util.ASTUtils.java

License:Open Source License

/**
 * Finds the node at the specified offset that matches up with the supplied
 * IJavaElement.//from  www.jav a  2s  . c o m
 *
 * @param cu The CompilationUnit.
 * @param offset The node offset in the compilation unit.
 * @param element The IJavaElement to match.
 * @return The node at the specified offset.
 */
public static ASTNode findNode(CompilationUnit cu, int offset, IJavaElement element) throws Exception {
    ASTNode node = findNode(cu, offset);
    if (node == null) {
        return null;
    }

    if (element.getElementType() == IJavaElement.TYPE_PARAMETER) {
        element = element.getParent();
    }

    switch (element.getElementType()) {
    case IJavaElement.PACKAGE_DECLARATION:
        node = resolveNode(node, PackageDeclaration.class);
        break;
    case IJavaElement.IMPORT_DECLARATION:
        node = resolveNode(node, ImportDeclaration.class);
        break;
    case IJavaElement.TYPE:
        node = resolveNode(node, AbstractTypeDeclaration.class);
        break;
    case IJavaElement.INITIALIZER:
        node = resolveNode(node, Initializer.class);
        break;
    case IJavaElement.FIELD:
        node = resolveNode(node, FieldDeclaration.class);
        break;
    case IJavaElement.METHOD:
        node = resolveNode(node, MethodDeclaration.class);
        break;
    default:
        logger.info("findNode(CompilationUnit,int,IJavaElement) - " + "unrecognized element type "
                + element.getElementType());
    }
    return node;
}

From source file:org.eclim.plugin.jdt.util.JavaUtils.java

License:Open Source License

/**
 * Gets the fully qualified name of the supplied java element.
 * <p/>/* ww  w .j a v a 2  s. c  o m*/
 * NOTE: For easy of determining fields and method segments, they are appended
 * with a javadoc style '#' instead of the normal '.'.
 *
 * @param element The IJavaElement.
 *
 * @return The fully qualified name.
 */
public static String getFullyQualifiedName(IJavaElement element) {
    IJavaElement parent = element;
    while (parent.getElementType() != IJavaElement.COMPILATION_UNIT
            && parent.getElementType() != IJavaElement.CLASS_FILE) {
        parent = parent.getParent();
    }

    StringBuffer elementName = new StringBuffer().append(parent.getParent().getElementName()).append('.')
            .append(FileUtils.getFileName(parent.getElementName()));

    switch (element.getElementType()) {
    case IJavaElement.FIELD:
        IField field = (IField) element;
        elementName.append('#').append(field.getElementName());
        break;
    case IJavaElement.METHOD:
        IMethod method = (IMethod) element;
        elementName.append('#').append(method.getElementName()).append('(');
        String[] parameters = method.getParameterTypes();
        for (int ii = 0; ii < parameters.length; ii++) {
            if (ii != 0) {
                elementName.append(", ");
            }
            elementName.append(Signature.toString(parameters[ii]).replace('/', '.'));
        }
        elementName.append(')');
        break;
    }

    return elementName.toString();
}

From source file:org.eclipse.ajdt.core.javaelements.ITDAwareSourceTypeInfo.java

License:Open Source License

private List<IJavaElement> getITDs(SourceType type) throws JavaModelException {
    AJProjectModelFacade model = AJProjectModelFactory.getInstance().getModelForJavaElement(type);
    if (model.hasModel()) {
        List<IJavaElement> itds = new ArrayList<IJavaElement>();
        List<IJavaElement> rels = model.getRelationshipsForElement(type,
                AJRelationshipManager.ASPECT_DECLARATIONS);

        List<IMethod> childMethods = null;

        for (IJavaElement ije : rels) {
            if (ije instanceof IntertypeElement) {
                IntertypeElement elt = (IntertypeElement) ije;
                IMember member = elt.createMockDeclaration(type);
                // null if the ITD doesn't exist in the AspectJ hierarchy
                // will happen if the Java side has partial compilation 
                // and aspectj side does not
                if (member != null) {

                    // should not add this ITD if it is a duplicate
                    // of another ITD
                    if (!isAlreadyAnITD(itds, member)) {
                        continue;
                    }/*from w  w w.  j  a  v a2s. co m*/

                    itds.add(member);

                    // additional processing for interfaces
                    if (handle.isInterface()) {

                        if (member.getElementType() == IJavaElement.FIELD) {
                            // Bug 262969
                            // Interfaces can't have fields, so ignore
                            // Reconciling errors occur if an ITD field is
                            // referenced outside of the aspect that declares them, 
                            // but only if the declared type of the object is an interface.
                            itds.remove(member);
                        } else if (member.getElementType() == IJavaElement.METHOD) {
                            // now look to see if this ITD a method that provides
                            // a default implementation for an interface method
                            // use IMethod.isSimilar
                            if (childMethods == null) {
                                childMethods = (List<IMethod>) type.getChildrenOfType(IJavaElement.METHOD);
                            }
                            for (IMethod method : childMethods) {
                                if (method.isSimilar((IMethod) member)) {
                                    itds.remove(member);
                                    break;
                                }
                            }
                        }
                    }
                }
            } else if (ije instanceof DeclareElement) {
                DeclareElement elt = (DeclareElement) ije;

                // use createElementInfo, not getElementInfo because 
                // we don't want it cached
                DeclareElementInfo info = (DeclareElementInfo) elt.createElementInfo();
                if (info == null || info.getAJKind() != Kind.DECLARE_PARENTS) {
                    continue;
                }

                char[][] newSupers = info.getTypes();
                augmentHierarchy(newSupers);
            } else if (ije instanceof AspectElement || ije instanceof BinaryAspectElement) {
                // likely a declare parents instantiated in a concrete aspect, but declared in a super aspect
                IProgramElement ipe = model.javaElementToProgramElement(ije);
                Map<String, List<String>> declareParentsMap = ipe.getDeclareParentsMap();
                if (declareParentsMap != null) {
                    augmentHierarchy(declareParentsMap.get(type.getFullyQualifiedName()));
                }
            } else if (ije instanceof IType) {
                // an ITIT
                itds.add(new ITIT(type, (IType) ije, model.javaElementToProgramElement(ije)));
            }
        }
        return itds;
    }
    return new LinkedList<IJavaElement>();
}

From source file:org.eclipse.ajdt.core.javaelements.ITDAwareSourceTypeInfo.java

License:Open Source License

private boolean isAlreadyAnITD(List<IJavaElement> itds, IMember member) {
    boolean shouldAdd = true;

    if (member.getElementType() == IJavaElement.FIELD) {
        for (IJavaElement itdElt : itds) {
            if (itdElt instanceof IField) {
                IField itdField = (IField) itdElt;
                if (member.getElementName().equals(itdField.getElementName())) {
                    // may not be same type, but we want to avoid conflicts, so remove
                    shouldAdd = false;/* ww  w.j a  va  2  s . co  m*/
                    break;
                }
            }
        }
    } else if (member.getElementType() == IJavaElement.METHOD) {
        for (IJavaElement itdElt : itds) {
            if (itdElt instanceof IMethod) {
                IMethod itdMethod = (IMethod) itdElt;
                if (itdMethod.isSimilar((IMethod) member)) {
                    shouldAdd = false;
                    break;
                }
            }
        }
    }
    return shouldAdd;
}

From source file:org.eclipse.ajdt.core.model.AJModelChecker.java

License:Open Source License

private static List<String> itdsNotOnType(IRelationship rel, AJProjectModelFacade model) {
    List<String> problems = new ArrayList<String>();
    if (rel.getKind() == IRelationship.Kind.DECLARE_INTER_TYPE) {

        IJavaElement elt = model.programElementToJavaElement(rel.getSourceHandle());
        if (!elt.exists()) {
            problems.add("Java Element does not exist: " + rel.getSourceHandle()
                    + "\n\tIt is the source relationship of " + toRelString(rel)
                    + "\n\tThis may not actually be a problem if compiling broken code.");
        }//from  w w w . j a v a 2  s  .co  m
        if (elt != AJProjectModelFacade.ERROR_JAVA_ELEMENT
                && (elt.getElementType() == IJavaElement.FIELD || elt.getElementType() == IJavaElement.METHOD
                        || elt.getElementType() == IJavaElement.LOCAL_VARIABLE
                        || elt.getElementType() == IJavaElement.INITIALIZER
                        || elt.getElementType() == IJavaElement.COMPILATION_UNIT
                        || elt.getElementType() == IJavaElement.CLASS_FILE)
                && !(elt instanceof IntertypeElement || elt instanceof DeclareElement)) {
            problems.add(
                    "Java Element is wrong type (ITD relationships should only contain types and intertype elements): "
                            + rel.getSourceHandle() + "\n\tIt is the source relationship of "
                            + toRelString(rel));
        }

        for (Iterator<String> targetIter = rel.getTargets().iterator(); targetIter.hasNext();) {
            String target = targetIter.next();
            elt = model.programElementToJavaElement(target);
            if (!elt.exists()) {
                problems.add("Java Element does not exist: " + target + "\n\tIt is the source relationship of "
                        + toRelString(rel)
                        + "\n\tThis may not actually be a problem if compiling broken code.");
            }
            if (elt != AJProjectModelFacade.ERROR_JAVA_ELEMENT
                    && (elt.getElementType() == IJavaElement.FIELD
                            || elt.getElementType() == IJavaElement.METHOD
                            || elt.getElementType() == IJavaElement.LOCAL_VARIABLE
                            || elt.getElementType() == IJavaElement.INITIALIZER
                            || elt.getElementType() == IJavaElement.COMPILATION_UNIT
                            || elt.getElementType() == IJavaElement.CLASS_FILE)
                    && !(elt instanceof IntertypeElement || elt instanceof DeclareElement)) {
                problems.add(
                        "Java Element is wrong type (ITD relationships should only contain types and intertype elements): "
                                + target + "\n\tIt is the source relationship of " + toRelString(rel));
            }
        }

    }
    return problems;
}

From source file:org.eclipse.ajdt.internal.ui.lazystart.ImageDecorator.java

License:Open Source License

private static boolean isInterfaceField(IMember element) throws JavaModelException {
    // always show the final && static symbol on interface fields
    if (element.getElementType() == IJavaElement.FIELD) {
        return element.getDeclaringType().isInterface();
    }//w  ww.  j a  v a 2  s. c  o m
    return false;
}

From source file:org.eclipse.buildship.ui.launch.JavaElementResolver.java

License:Open Source License

private Optional<IType> resolveType(IJavaElement javaElement) {
    // exclude elements with no parent projects
    if (javaElement.getJavaProject() == null || javaElement.getJavaProject().getProject() == null) {
        return Optional.absent();
    }/*  w  ww.j  av a2 s.co m*/

    IType result = null;
    switch (javaElement.getElementType()) {
    case IJavaElement.TYPE:
        result = (IType) javaElement;
        break;
    case IJavaElement.FIELD:
        result = ((IField) javaElement).getDeclaringType();
        break;
    case IJavaElement.CLASS_FILE:
    case IJavaElement.COMPILATION_UNIT:
        result = ((ITypeRoot) javaElement).findPrimaryType();
        break;
    }

    return Optional.fromNullable(result);
}

From source file:org.eclipse.che.jdt.internal.core.search.BasicSearchEngine.java

License:Open Source License

/**
 * Searches for all declarations of the methods invoked in the given element.
 * The element can be a compilation unit or a source type/method/field.
 * Reports the method declarations using the given requestor.
 *
 * @see org.eclipse.jdt.core.search.SearchEngine#searchDeclarationsOfSentMessages(org.eclipse.jdt.core.IJavaElement, org.eclipse.jdt
 * .core.search.SearchRequestor, IProgressMonitor)
 *    for detailed comment//w w w.j  a v a 2  s.com
 */
public void searchDeclarationsOfSentMessages(IJavaElement enclosingElement, SearchRequestor requestor,
        IProgressMonitor monitor) throws JavaModelException {
    if (VERBOSE) {
        Util.verbose(
                "BasicSearchEngine.searchDeclarationsOfSentMessages(IJavaElement, SearchRequestor, SearchPattern, IProgressMonitor)");
        //$NON-NLS-1$
    }
    // Do not accept other kind of element type than those specified in the spec
    switch (enclosingElement.getElementType()) {
    case IJavaElement.FIELD:
    case IJavaElement.METHOD:
    case IJavaElement.TYPE:
    case IJavaElement.COMPILATION_UNIT:
        // valid element type
        break;
    default:
        throw new IllegalArgumentException();
    }
    SearchPattern pattern = new DeclarationOfReferencedMethodsPattern(enclosingElement);
    searchDeclarations(enclosingElement, requestor, pattern, monitor);
}

From source file:org.eclipse.che.jdt.internal.core.util.HandleFactory.java

License:Open Source License

/**
 * Create handle by adding child to parent obtained by recursing into parent scopes.
 *///from   w  w w .j a  va 2s  . c  o  m
public IJavaElement createElement(Scope scope, int elementPosition, ICompilationUnit unit,
        HashSet existingElements, HashMap knownScopes) {
    IJavaElement newElement = (IJavaElement) knownScopes.get(scope);
    if (newElement != null)
        return newElement;

    switch (scope.kind) {
    case Scope.COMPILATION_UNIT_SCOPE:
        newElement = unit;
        break;
    case Scope.CLASS_SCOPE:
        IJavaElement parentElement = createElement(scope.parent, elementPosition, unit, existingElements,
                knownScopes);
        switch (parentElement.getElementType()) {
        case IJavaElement.COMPILATION_UNIT:
            newElement = ((ICompilationUnit) parentElement)
                    .getType(new String(scope.enclosingSourceType().sourceName));
            break;
        case IJavaElement.TYPE:
            newElement = ((IType) parentElement).getType(new String(scope.enclosingSourceType().sourceName));
            break;
        case IJavaElement.FIELD:
        case IJavaElement.INITIALIZER:
        case IJavaElement.METHOD:
            IMember member = (IMember) parentElement;
            if (member.isBinary()) {
                return null;
            } else {
                newElement = member.getType(new String(scope.enclosingSourceType().sourceName), 1);
                // increment occurrence count if collision is detected
                if (newElement != null) {
                    while (!existingElements.add(newElement))
                        ((SourceRefElement) newElement).occurrenceCount++;
                }
            }
            break;
        }
        if (newElement != null) {
            knownScopes.put(scope, newElement);
        }
        break;
    case Scope.METHOD_SCOPE:
        if (scope.isLambdaScope()) {
            parentElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
            LambdaExpression expression = (LambdaExpression) scope.originalReferenceContext();
            if (expression.resolvedType != null && expression.resolvedType.isValidBinding()
                    && !(expression.descriptor instanceof ProblemMethodBinding)) { // chain in lambda element only if resolved properly.
                //newElement = new org.eclipse.jdt.internal.core.SourceLambdaExpression((JavaElement) parentElement, expression)
                // .getMethod();

                newElement = LambdaFactory.createLambdaExpression((JavaElement) parentElement, expression)
                        .getMethod();
                knownScopes.put(scope, newElement);
                return newElement;
            }
            return parentElement;
        }
        IType parentType = (IType) createElement(scope.parent, elementPosition, unit, existingElements,
                knownScopes);
        MethodScope methodScope = (MethodScope) scope;
        if (methodScope.isInsideInitializer()) {
            // inside field or initializer, must find proper one
            TypeDeclaration type = methodScope.referenceType();
            int occurenceCount = 1;
            int length = type.fields == null ? 0 : type.fields.length;
            for (int i = 0; i < length; i++) {
                FieldDeclaration field = type.fields[i];
                if (field.declarationSourceStart <= elementPosition
                        && elementPosition <= field.declarationSourceEnd) {
                    switch (field.getKind()) {
                    case AbstractVariableDeclaration.FIELD:
                    case AbstractVariableDeclaration.ENUM_CONSTANT:
                        newElement = parentType.getField(new String(field.name));
                        break;
                    case AbstractVariableDeclaration.INITIALIZER:
                        newElement = parentType.getInitializer(occurenceCount);
                        break;
                    }
                    break;
                } else if (field.getKind() == AbstractVariableDeclaration.INITIALIZER) {
                    occurenceCount++;
                }
            }
        } else {
            // method element
            AbstractMethodDeclaration method = methodScope.referenceMethod();
            newElement = parentType.getMethod(new String(method.selector),
                    Util.typeParameterSignatures(method));
            if (newElement != null) {
                knownScopes.put(scope, newElement);
            }
        }
        break;
    case Scope.BLOCK_SCOPE:
        // standard block, no element per se
        newElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
        break;
    }
    return newElement;
}

From source file:org.eclipse.che.jdt.refactoring.RefactoringManager.java

License:Open Source License

private static RenameSupport createRenameSupport(IJavaElement element, String newName, int flags)
        throws CoreException {
    switch (element.getElementType()) {
    case IJavaElement.PACKAGE_FRAGMENT:
        return RenameSupport.create((IPackageFragment) element, newName, flags);
    case IJavaElement.COMPILATION_UNIT:
        return RenameSupport.create((ICompilationUnit) element, newName, flags);
    case IJavaElement.TYPE:
        return RenameSupport.create((IType) element, newName, flags);
    case IJavaElement.METHOD:
        final IMethod method = (IMethod) element;
        if (method.isConstructor())
            return createRenameSupport(method.getDeclaringType(), newName, flags);
        else//from w w  w  . java 2s  . c  o m
            return RenameSupport.create((IMethod) element, newName, flags);
    case IJavaElement.FIELD:
        return RenameSupport.create((IField) element, newName, flags);
    case IJavaElement.TYPE_PARAMETER:
        return RenameSupport.create((ITypeParameter) element, newName, flags);
    case IJavaElement.LOCAL_VARIABLE:
        return RenameSupport.create((ILocalVariable) element, newName, flags);
    }
    return null;
}