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

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

Introduction

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

Prototype

int INITIALIZER

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

Click Source Link

Document

Constant representing a stand-alone instance or class initializer.

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.SourceType.java

License:Open Source License

/**
 * @see org.eclipse.jdt.core.IType#isLocal()
 */// w w w.  ja va  2s.  co m
public boolean isLocal() {
    switch (this.parent.getElementType()) {
    case IJavaElement.METHOD:
    case IJavaElement.INITIALIZER:
    case IJavaElement.FIELD:
        return true;
    default:
        return false;
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.util.ASTNodeFinder.java

License:Open Source License

public TypeDeclaration findType(IType typeHandle) {
    IJavaElement parent = typeHandle.getParent();
    final char[] typeName = typeHandle.getElementName().toCharArray();
    final int occurenceCount = ((SourceType) typeHandle).occurrenceCount;
    final boolean findAnonymous = typeName.length == 0;
    class Visitor extends ASTVisitor {
        TypeDeclaration result;//ww  w .j av  a2s  . c o  m
        int count = 0;

        public boolean visit(TypeDeclaration typeDeclaration, BlockScope scope) {
            if (this.result != null)
                return false;
            if ((typeDeclaration.bits & ASTNode.IsAnonymousType) != 0) {
                if (findAnonymous && ++this.count == occurenceCount) {
                    this.result = typeDeclaration;
                }
            } else {
                if (!findAnonymous && CharOperation.equals(typeName, typeDeclaration.name)) {
                    this.result = typeDeclaration;
                }
            }
            return false; // visit only one level
        }
    }
    switch (parent.getElementType()) {
    case IJavaElement.COMPILATION_UNIT:
        TypeDeclaration[] types = this.unit.types;
        if (types != null) {
            for (int i = 0, length = types.length; i < length; i++) {
                TypeDeclaration type = types[i];
                if (CharOperation.equals(typeName, type.name)) {
                    return type;
                }
            }
        }
        break;
    case IJavaElement.TYPE:
        TypeDeclaration parentDecl = findType((IType) parent);
        if (parentDecl == null)
            return null;
        types = parentDecl.memberTypes;
        if (types != null) {
            for (int i = 0, length = types.length; i < length; i++) {
                TypeDeclaration type = types[i];
                if (CharOperation.equals(typeName, type.name)) {
                    return type;
                }
            }
        }
        break;
    case IJavaElement.FIELD:
        FieldDeclaration fieldDecl = findField((IField) parent);
        if (fieldDecl == null)
            return null;
        Visitor visitor = new Visitor();
        fieldDecl.traverse(visitor, null);
        return visitor.result;
    case IJavaElement.INITIALIZER:
        Initializer initializer = findInitializer((IInitializer) parent);
        if (initializer == null)
            return null;
        visitor = new Visitor();
        initializer.traverse(visitor, null);
        return visitor.result;
    case IJavaElement.METHOD:
        AbstractMethodDeclaration methodDecl = findMethod((IMethod) parent);
        if (methodDecl == null)
            return null;
        visitor = new Visitor();
        methodDecl.traverse(visitor, (ClassScope) null);
        return visitor.result;
    }
    return null;
}

From source file:com.codenvy.ide.ext.java.server.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 av  a2s .co m
private 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:
        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:com.codenvy.ide.ext.java.server.javadoc.JavaElementLabelComposer.java

License:Open Source License

/**
 * Appends the label for a type. Considers the T_* flags.
 *
 * @param type the element to render/*from w ww .  j a va  2 s  .  c o m*/
 * @param flags the rendering flags. Flags with names starting with 'T_' are considered.
 */
public void appendTypeLabel(IType type, long flags) {

    if (getFlag(flags, JavaElementLabels.T_FULLY_QUALIFIED)) {
        IPackageFragment pack = type.getPackageFragment();
        if (!pack.isDefaultPackage()) {
            appendPackageFragmentLabel(pack, (flags & QUALIFIER_FLAGS));
            fBuffer.append('.');
        }
    }
    IJavaElement parent = type.getParent();
    if (getFlag(flags, JavaElementLabels.T_FULLY_QUALIFIED | JavaElementLabels.T_CONTAINER_QUALIFIED)) {
        IType declaringType = type.getDeclaringType();
        if (declaringType != null) {
            appendTypeLabel(declaringType, JavaElementLabels.T_CONTAINER_QUALIFIED | (flags & QUALIFIER_FLAGS));
            fBuffer.append('.');
        }
        int parentType = parent.getElementType();
        if (parentType == IJavaElement.METHOD || parentType == IJavaElement.FIELD
                || parentType == IJavaElement.INITIALIZER) { // anonymous or local
            appendElementLabel(parent, 0);
            fBuffer.append('.');
        }
    }

    String typeName;
    boolean isAnonymous = false;
    if (type.isLambda()) {
        typeName = "() -> {...}"; //$NON-NLS-1$
        try {
            String[] superInterfaceSignatures = type.getSuperInterfaceTypeSignatures();
            if (superInterfaceSignatures.length > 0) {
                typeName = typeName + ' ' + getSimpleTypeName(type, superInterfaceSignatures[0]);
            }
        } catch (JavaModelException e) {
            //ignore
        }

    } else {
        typeName = getElementName(type);
        try {
            isAnonymous = type.isAnonymous();
        } catch (JavaModelException e1) {
            // should not happen, but let's play safe:
            isAnonymous = typeName.length() == 0;
        }
        if (isAnonymous) {
            try {
                if (parent instanceof IField && type.isEnum()) {
                    typeName = '{' + JavaElementLabels.ELLIPSIS_STRING + '}';
                } else {
                    String supertypeName;
                    String[] superInterfaceSignatures = type.getSuperInterfaceTypeSignatures();
                    if (superInterfaceSignatures.length > 0) {
                        supertypeName = getSimpleTypeName(type, superInterfaceSignatures[0]);
                    } else {
                        supertypeName = getSimpleTypeName(type, type.getSuperclassTypeSignature());
                    }
                    typeName = MessageFormat.format("new {0}() '{'...}", supertypeName);
                }
            } catch (JavaModelException e) {
                //ignore
                typeName = "new Anonymous";
            }
        }
    }
    fBuffer.append(typeName);

    if (getFlag(flags, JavaElementLabels.T_TYPE_PARAMETERS)) {
        if (getFlag(flags, JavaElementLabels.USE_RESOLVED) && type.isResolved()) {
            BindingKey key = new BindingKey(type.getKey());
            if (key.isParameterizedType()) {
                String[] typeArguments = key.getTypeArguments();
                appendTypeArgumentSignaturesLabel(type, typeArguments, flags);
            } else {
                String[] typeParameters = Signature.getTypeParameters(key.toSignature());
                appendTypeParameterSignaturesLabel(typeParameters, flags);
            }
        } else if (type.exists()) {
            try {
                appendTypeParametersLabels(type.getTypeParameters(), flags);
            } catch (JavaModelException e) {
                // ignore
            }
        }
    }

    // category
    if (getFlag(flags, JavaElementLabels.T_CATEGORY) && type.exists()) {
        try {
            appendCategoryLabel(type, flags);
        } catch (JavaModelException e) {
            // ignore
        }
    }

    // post qualification
    if (getFlag(flags, JavaElementLabels.T_POST_QUALIFIED)) {
        int offset = fBuffer.length();
        fBuffer.append(JavaElementLabels.CONCAT_STRING);
        IType declaringType = type.getDeclaringType();
        if (declaringType == null && type.isBinary() && isAnonymous) {
            // workaround for Bug 87165: [model] IType#getDeclaringType() does not work for anonymous binary type
            String tqn = type.getTypeQualifiedName();
            int lastDollar = tqn.lastIndexOf('$');
            if (lastDollar != 1) {
                String declaringTypeCF = tqn.substring(0, lastDollar) + ".class"; //$NON-NLS-1$
                declaringType = type.getPackageFragment().getClassFile(declaringTypeCF).getType();
                try {
                    ISourceRange typeSourceRange = type.getSourceRange();
                    if (declaringType.exists() && SourceRange.isAvailable(typeSourceRange)) {
                        IJavaElement realParent = declaringType.getTypeRoot()
                                .getElementAt(typeSourceRange.getOffset() - 1);
                        if (realParent != null) {
                            parent = realParent;
                        }
                    }
                } catch (JavaModelException e) {
                    // ignore
                }
            }
        }
        if (declaringType != null) {
            appendTypeLabel(declaringType, JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS));
            int parentType = parent.getElementType();
            if (parentType == IJavaElement.METHOD || parentType == IJavaElement.FIELD
                    || parentType == IJavaElement.INITIALIZER) { // anonymous or local
                fBuffer.append('.');
                appendElementLabel(parent, 0);
            }
        } else {
            appendPackageFragmentLabel(type.getPackageFragment(), flags & QUALIFIER_FLAGS);
        }
        //         if (getFlag(flags, JavaElementLabels.COLORIZE)) {
        //            fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE);
        //         }
    }
}

From source file:com.github.elucash.lambda4jdt.FoldingStructureProvider.java

License:Open Source License

/**
 * Computes the folding structure for a given {@link IJavaElement java element}. Computed
 * projection annotations are//  ww w  .  j a v  a 2 s .  com
 * {@link FoldingStructureProvider.FoldingStructureComputationContext#addProjectionRange(FoldingStructureProvider.JavaProjectionAnnotation, Position)
 * added} to the computation context.
 * <p>
 * Subclasses may extend or replace. The default implementation creates projection annotations for
 * the following elements:
 * <ul>
 * <li>true members (not for top-level types)</li>
 * <li>the javadoc comments of any member</li>
 * <li>header comments (javadoc or multi-line comments appearing before the first type's javadoc
 * or before the package or import declarations).</li>
 * </ul>
 * </p>
 * @param element the java element to compute the folding structure for
 * @param ctx the computation context
 */
protected void computeFoldingStructure(IJavaElement element, FoldingStructureComputationContext ctx) {

    IMethod lambdaMethod = null;
    /*      boolean importContainer = false;*/
    boolean collapse = false;
    boolean collapseCode = true;
    switch (element.getElementType()) {

    case IJavaElement.IMPORT_CONTAINER: {

        IImportContainer importContainer = (IImportContainer) element;
        IRegion projectionRegion = computeImportProjectionRanges(importContainer, ctx);

        if (projectionRegion != null) {
            JavaImportPosition importPosition = new JavaImportPosition(projectionRegion, importContainer);

            ctx.addProjectionRange(
                    new JavaProjectionAnnotation(ctx.collapseImportContainer(), ctx, element, true),
                    importPosition);
        }

        return;
    }
    case IJavaElement.TYPE:
        collapseCode = isInnerType((IType) element) && !isAnonymousEnum((IType) element);
        collapse = ctx.collapseInnerTypes() && collapseCode;

        lambdaMethod = findLambdaMethodIn(element);

        if (lambdaMethod != null) {
            // Let collapse initially by default
            collapse = ctx.initial;
        } else {
            try {
                if (((IType) element).isAnonymous())
                    return;
            } catch (Exception e) {
            }
        }
        break;
    case IJavaElement.METHOD:
    case IJavaElement.FIELD:
    case IJavaElement.INITIALIZER:
        collapse = ctx.collapseMembers();
        collapseCode = false;
        break;
    default:
        return;
    }

    IRegion[] regions = computeProjectionRanges((ISourceReference) element, ctx);

    if (regions.length == 0)
        return;

    // comments
    for (int i = 0; i < regions.length - 1; i++) {
        IRegion normalized = alignRegion(regions[i], ctx);
        if (normalized != null) {
            Position position = createCommentPosition(normalized);
            if (position != null) {
                boolean commentCollapse;
                if (i == 0 && (regions.length > 2 || ctx.hasHeaderComment()) && element == ctx.getFirstType()) {
                    commentCollapse = ctx.collapseHeaderComments();
                } else {
                    commentCollapse = ctx.collapseJavadoc();
                }
                ctx.addProjectionRange(new JavaProjectionAnnotation(commentCollapse, ctx, element, true),
                        position);
            }
        }
    }
    // code
    if (!collapseCode)
        return;

    // IRegion lastRegion = regions[regions.length - 1];

    IRegion codeRegion = regions[regions.length - 1];

    // if (lambdaMethod != null) {
    // normalized = alignRegion(lastRegion, ctx);
    // }

    if (codeRegion != null) {
        Position position = element instanceof IMember
                ? createMemberPosition(codeRegion, (IMember) element, lambdaMethod)
                : createCommentPosition(codeRegion);

        if (position != null)
            ctx.addProjectionRange(new JavaProjectionAnnotation(collapse, ctx, element, false), position);
    }
}

From source file:com.ibm.research.tagging.java.folding.WaypointAwareJavaFoldingStructureProvider.java

License:Open Source License

@Override
protected void computeFoldingStructure(IJavaElement element, FoldingStructureComputationContext ctx) {
    super.computeFoldingStructure(element, ctx);

    switch (element.getElementType()) {
    case IJavaElement.METHOD:
        return;/*www .j  av  a  2  s . co  m*/

    case IJavaElement.IMPORT_CONTAINER:
    case IJavaElement.TYPE:
    case IJavaElement.FIELD:
    case IJavaElement.INITIALIZER:
        break;
    default:
        return;
    }

    IRegion[] waypointRegions = WaypointDefinitionExtractor.getWaypointRegions(getDocument(),
            (ISourceReference) element);

    for (IRegion region : waypointRegions) {

        JavaProjectionAnnotation annotation = new JavaProjectionAnnotation(true, element, false);
        IRegion normalized = alignRegion(region, ctx);

        /* A null here indicates that we are attempting to fold a single line region */
        if (normalized != null) {
            Position position = createCommentPosition(normalized);
            ctx.addProjectionRange(annotation, position);
        }
    }

}

From source file:com.microsoft.javapkgsrv.JavaElementLabelComposer.java

License:Open Source License

/**
 * Appends the label for a Java element with the flags as defined by this class.
 *
 * @param element the element to render/*ww  w.ja v a 2  s. c o  m*/
 * @param flags the rendering flags.
 */
public void appendElementLabel(IJavaElement element, long flags) {
    int type = element.getElementType();
    IPackageFragmentRoot root = null;

    if (type != IJavaElement.JAVA_MODEL && type != IJavaElement.JAVA_PROJECT
            && type != IJavaElement.PACKAGE_FRAGMENT_ROOT)
        root = (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
    if (root != null && getFlag(flags, PREPEND_ROOT_PATH)) {
        appendPackageFragmentRootLabel(root, ROOT_QUALIFIED);
        fBuffer.append(CONCAT_STRING);
    }

    switch (type) {
    case IJavaElement.METHOD:
        appendMethodLabel((IMethod) element, flags);
        break;
    case IJavaElement.FIELD:
        appendFieldLabel((IField) element, flags);
        break;
    case IJavaElement.LOCAL_VARIABLE:
        appendLocalVariableLabel((ILocalVariable) element, flags);
        break;
    case IJavaElement.TYPE_PARAMETER:
        appendTypeParameterLabel((ITypeParameter) element, flags);
        break;
    case IJavaElement.INITIALIZER:
        appendInitializerLabel((IInitializer) element, flags);
        break;
    case IJavaElement.TYPE:
        appendTypeLabel((IType) element, flags);
        break;
    case IJavaElement.CLASS_FILE:
        appendClassFileLabel((IClassFile) element, flags);
        break;
    case IJavaElement.COMPILATION_UNIT:
        appendCompilationUnitLabel((ICompilationUnit) element, flags);
        break;
    case IJavaElement.PACKAGE_FRAGMENT:
        appendPackageFragmentLabel((IPackageFragment) element, flags);
        break;
    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
        appendPackageFragmentRootLabel((IPackageFragmentRoot) element, flags);
        break;
    case IJavaElement.IMPORT_CONTAINER:
    case IJavaElement.IMPORT_DECLARATION:
    case IJavaElement.PACKAGE_DECLARATION:
        appendDeclarationLabel(element, flags);
        break;
    case IJavaElement.JAVA_PROJECT:
    case IJavaElement.JAVA_MODEL:
        fBuffer.append(element.getElementName());
        break;
    default:
        fBuffer.append(element.getElementName());
    }

    if (root != null && getFlag(flags, APPEND_ROOT_PATH)) {
        int offset = fBuffer.length();
        fBuffer.append(CONCAT_STRING);
        appendPackageFragmentRootLabel(root, ROOT_QUALIFIED);
    }
}

From source file:com.microsoft.javapkgsrv.JavaElementLabelComposer.java

License:Open Source License

/**
 * Appends the label for a type. Considers the T_* flags.
 *
 * @param type the element to render/*from  w  ww  .j  a v a 2  s.c o m*/
 * @param flags the rendering flags. Flags with names starting with 'T_' are considered.
 */
public void appendTypeLabel(IType type, long flags) {

    if (getFlag(flags, T_FULLY_QUALIFIED)) {
        IPackageFragment pack = type.getPackageFragment();
        if (!pack.isDefaultPackage()) {
            appendPackageFragmentLabel(pack, (flags & QUALIFIER_FLAGS));
            fBuffer.append('.');
        }
    }
    IJavaElement parent = type.getParent();
    if (getFlag(flags, T_FULLY_QUALIFIED | T_CONTAINER_QUALIFIED)) {
        IType declaringType = type.getDeclaringType();
        if (declaringType != null) {
            appendTypeLabel(declaringType, T_CONTAINER_QUALIFIED | (flags & QUALIFIER_FLAGS));
            fBuffer.append('.');
        }
        int parentType = parent.getElementType();
        if (parentType == IJavaElement.METHOD || parentType == IJavaElement.FIELD
                || parentType == IJavaElement.INITIALIZER) { // anonymous or local
            appendElementLabel(parent, 0);
            fBuffer.append('.');
        }
    }

    String typeName;
    boolean isAnonymous = false;
    if (type.isLambda()) {
        typeName = "() -> {...}"; //$NON-NLS-1$
        try {
            String[] superInterfaceSignatures = type.getSuperInterfaceTypeSignatures();
            if (superInterfaceSignatures.length > 0) {
                typeName = typeName + ' ' + getSimpleTypeName(type, superInterfaceSignatures[0]);
            }
        } catch (JavaModelException e) {
            //ignore
        }

    } else {
        typeName = getElementName(type);
        try {
            isAnonymous = type.isAnonymous();
        } catch (JavaModelException e1) {
            // should not happen, but let's play safe:
            isAnonymous = typeName.length() == 0;
        }
        if (isAnonymous) {
            try {
                if (parent instanceof IField && type.isEnum()) {
                    typeName = '{' + ELLIPSIS_STRING + '}';
                } else {
                    String supertypeName;
                    String[] superInterfaceSignatures = type.getSuperInterfaceTypeSignatures();
                    if (superInterfaceSignatures.length > 0) {
                        supertypeName = getSimpleTypeName(type, superInterfaceSignatures[0]);
                    } else {
                        supertypeName = getSimpleTypeName(type, type.getSuperclassTypeSignature());
                    }
                    typeName = "new " + supertypeName + "() {...}";
                }
            } catch (JavaModelException e) {
                //ignore
                typeName = "new Anonymous";
            }
        }
    }
    fBuffer.append(typeName);

    if (getFlag(flags, T_TYPE_PARAMETERS)) {
        if (getFlag(flags, USE_RESOLVED) && type.isResolved()) {
            BindingKey key = new BindingKey(type.getKey());
            if (key.isParameterizedType()) {
                String[] typeArguments = key.getTypeArguments();
                appendTypeArgumentSignaturesLabel(type, typeArguments, flags);
            } else {
                String[] typeParameters = Signature.getTypeParameters(key.toSignature());
                appendTypeParameterSignaturesLabel(typeParameters, flags);
            }
        } else if (type.exists()) {
            try {
                appendTypeParametersLabels(type.getTypeParameters(), flags);
            } catch (JavaModelException e) {
                // ignore
            }
        }
    }

    // category
    if (getFlag(flags, T_CATEGORY) && type.exists()) {
        try {
            appendCategoryLabel(type, flags);
        } catch (JavaModelException e) {
            // ignore
        }
    }

    // post qualification
    if (getFlag(flags, T_POST_QUALIFIED)) {
        int offset = fBuffer.length();
        fBuffer.append(CONCAT_STRING);
        IType declaringType = type.getDeclaringType();
        if (declaringType == null && type.isBinary() && isAnonymous) {
            // workaround for Bug 87165: [model] IType#getDeclaringType() does not work for anonymous binary type
            String tqn = type.getTypeQualifiedName();
            int lastDollar = tqn.lastIndexOf('$');
            if (lastDollar != 1) {
                String declaringTypeCF = tqn.substring(0, lastDollar) + ".class"; //$NON-NLS-1$
                declaringType = type.getPackageFragment().getClassFile(declaringTypeCF).getType();
                try {
                    ISourceRange typeSourceRange = type.getSourceRange();
                    if (declaringType.exists() && SourceRange.isAvailable(typeSourceRange)) {
                        IJavaElement realParent = declaringType.getTypeRoot()
                                .getElementAt(typeSourceRange.getOffset() - 1);
                        if (realParent != null) {
                            parent = realParent;
                        }
                    }
                } catch (JavaModelException e) {
                    // ignore
                }
            }
        }
        if (declaringType != null) {
            appendTypeLabel(declaringType, T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS));
            int parentType = parent.getElementType();
            if (parentType == IJavaElement.METHOD || parentType == IJavaElement.FIELD
                    || parentType == IJavaElement.INITIALIZER) { // anonymous or local
                fBuffer.append('.');
                appendElementLabel(parent, 0);
            }
        } else {
            appendPackageFragmentLabel(type.getPackageFragment(), flags & QUALIFIER_FLAGS);
        }
    }
}

From source file:com.mountainminds.eclemma.internal.core.analysis.TypeTraverser.java

License:Open Source License

private void processType(ITypeVisitor visitor, BinaryTypeName btn, IType type, IProgressMonitor monitor)
        throws JavaModelException {
    final String binaryname = btn.toString();
    monitor.subTask(binaryname);//  w w  w  . j  a va 2 s. c  o m
    visitor.visit(type, binaryname);
    for (final IJavaElement child : type.getChildren()) {
        if (monitor.isCanceled()) {
            break;
        }
        switch (child.getElementType()) {
        case IJavaElement.TYPE:
            IType nestedtype = (IType) child;
            processType(visitor, btn.nest(nestedtype), nestedtype, monitor);
            break;
        case IJavaElement.METHOD:
        case IJavaElement.INITIALIZER:
        case IJavaElement.FIELD:
            processAnonymousInnerTypes(visitor, btn, (IMember) child, monitor);
            break;
        }
    }
}

From source file:com.redhat.ceylon.eclipse.code.explorer.JavaElementImageProvider.java

License:Open Source License

/**
 * Returns an image descriptor for a java element. This is the base image, no overlays.
 * @param element the element/*from   w  w w  . j  a  v  a 2s.c om*/
 * @param renderFlags the image flags
 * @return returns the image descriptor
 */
public ImageDescriptor getBaseImageDescriptor(IJavaElement element, int renderFlags) {

    try {
        switch (element.getElementType()) {
        case IJavaElement.INITIALIZER:
            return JavaPluginImages.DESC_MISC_PRIVATE; // 23479
        case IJavaElement.METHOD: {
            IMethod method = (IMethod) element;
            IType declType = method.getDeclaringType();
            int flags = method.getFlags();
            if (declType.isEnum() && isDefaultFlag(flags) && method.isConstructor())
                return JavaPluginImages.DESC_MISC_PRIVATE;
            return getMethodImageDescriptor(JavaModelUtil.isInterfaceOrAnnotation(declType), flags);
        }
        case IJavaElement.FIELD: {
            IMember member = (IMember) element;
            IType declType = member.getDeclaringType();
            return getFieldImageDescriptor(JavaModelUtil.isInterfaceOrAnnotation(declType), member.getFlags());
        }
        case IJavaElement.LOCAL_VARIABLE:
            return JavaPluginImages.DESC_OBJS_LOCAL_VARIABLE;

        case IJavaElement.PACKAGE_DECLARATION:
            return JavaPluginImages.DESC_OBJS_PACKDECL;

        case IJavaElement.IMPORT_DECLARATION:
            return JavaPluginImages.DESC_OBJS_IMPDECL;

        case IJavaElement.IMPORT_CONTAINER:
            return JavaPluginImages.DESC_OBJS_IMPCONT;

        case IJavaElement.TYPE: {
            IType type = (IType) element;

            IType declType = type.getDeclaringType();
            boolean isInner = declType != null;
            boolean isInInterfaceOrAnnotation = isInner && JavaModelUtil.isInterfaceOrAnnotation(declType);
            return getTypeImageDescriptor(isInner, isInInterfaceOrAnnotation, type.getFlags(),
                    useLightIcons(renderFlags));
        }

        case IJavaElement.PACKAGE_FRAGMENT_ROOT: {
            IPackageFragmentRoot root = (IPackageFragmentRoot) element;
            IPath attach = root.getSourceAttachmentPath();
            if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
                if (root.isArchive()) {
                    if (root.isExternal()) {
                        if (attach == null) {
                            return JavaPluginImages.DESC_OBJS_EXTJAR;
                        } else {
                            return JavaPluginImages.DESC_OBJS_EXTJAR_WSRC;
                        }
                    } else {
                        if (attach == null) {
                            return JavaPluginImages.DESC_OBJS_JAR;
                        } else {
                            return JavaPluginImages.DESC_OBJS_JAR_WSRC;
                        }
                    }
                } else {
                    if (attach == null) {
                        return JavaPluginImages.DESC_OBJS_CLASSFOLDER;
                    } else {
                        return JavaPluginImages.DESC_OBJS_CLASSFOLDER_WSRC;
                    }
                }
            } else {
                return JavaPluginImages.DESC_OBJS_PACKFRAG_ROOT;
            }
        }

        case IJavaElement.PACKAGE_FRAGMENT:
            return getPackageFragmentIcon(element);

        case IJavaElement.COMPILATION_UNIT:
            return JavaPluginImages.DESC_OBJS_CUNIT;

        case IJavaElement.CLASS_FILE:
            /* this is too expensive for large packages
            try {
               IClassFile cfile= (IClassFile)element;
               if (cfile.isClass())
             return JavaPluginImages.IMG_OBJS_CFILECLASS;
               return JavaPluginImages.IMG_OBJS_CFILEINT;
            } catch(JavaModelException e) {
               // fall through;
            }*/
            return JavaPluginImages.DESC_OBJS_CFILE;

        case IJavaElement.JAVA_PROJECT:
            IJavaProject jp = (IJavaProject) element;
            if (jp.getProject().isOpen()) {
                IProject project = jp.getProject();
                IWorkbenchAdapter adapter = (IWorkbenchAdapter) project.getAdapter(IWorkbenchAdapter.class);
                if (adapter != null) {
                    ImageDescriptor result = adapter.getImageDescriptor(project);
                    if (result != null)
                        return result;
                }
                return DESC_OBJ_PROJECT;
            }
            return DESC_OBJ_PROJECT_CLOSED;

        case IJavaElement.JAVA_MODEL:
            return JavaPluginImages.DESC_OBJS_JAVA_MODEL;

        case IJavaElement.TYPE_PARAMETER:
            return JavaPluginImages.DESC_OBJS_TYPEVARIABLE;

        case IJavaElement.ANNOTATION:
            return JavaPluginImages.DESC_OBJS_ANNOTATION;

        default:
            // ignore. Must be a new, yet unknown Java element
            // give an advanced IWorkbenchAdapter the chance
            IWorkbenchAdapter wbAdapter = (IWorkbenchAdapter) element.getAdapter(IWorkbenchAdapter.class);
            if (wbAdapter != null && !(wbAdapter instanceof JavaWorkbenchAdapter)) { // avoid recursion
                ImageDescriptor imageDescriptor = wbAdapter.getImageDescriptor(element);
                if (imageDescriptor != null) {
                    return imageDescriptor;
                }
            }
            return JavaPluginImages.DESC_OBJS_GHOST;
        }

    } catch (JavaModelException e) {
        if (e.isDoesNotExist())
            return JavaPluginImages.DESC_OBJS_UNKNOWN;
        JavaPlugin.log(e);
        return JavaPluginImages.DESC_OBJS_GHOST;
    }
}