Example usage for org.eclipse.jdt.core IField getKey

List of usage examples for org.eclipse.jdt.core IField getKey

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IField getKey.

Prototype

String getKey();

Source Link

Document

Returns the binding key for this field only if the given field is #isResolved() resolved .

Usage

From source file:at.bestsolution.fxide.jdt.text.viewersupport.JavaElementLabelComposer.java

License:Open Source License

/**
 * Appends the style label for a field. Considers the F_* flags.
 *
 * @param field the element to render/* w w  w .  j a va2  s . co  m*/
 * @param flags the rendering flags. Flags with names starting with 'F_' are considered.
 */
public void appendFieldLabel(IField field, long flags) {
    try {

        if (getFlag(flags, JavaElementLabels.F_PRE_TYPE_SIGNATURE) && field.exists()
                && !Flags.isEnum(field.getFlags())) {
            if (getFlag(flags, JavaElementLabels.USE_RESOLVED) && field.isResolved()) {
                appendTypeSignatureLabel(field, new BindingKey(field.getKey()).toSignature(), flags);
            } else {
                appendTypeSignatureLabel(field, field.getTypeSignature(), flags);
            }
            fBuffer.append(' ');
        }

        // qualification
        if (getFlag(flags, JavaElementLabels.F_FULLY_QUALIFIED)) {
            appendTypeLabel(field.getDeclaringType(),
                    JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS));
            fBuffer.append('.');
        }
        fBuffer.append(getElementName(field));

        if (getFlag(flags, JavaElementLabels.F_APP_TYPE_SIGNATURE) && field.exists()
                && !Flags.isEnum(field.getFlags())) {
            int offset = fBuffer.length();
            fBuffer.append(JavaElementLabels.DECL_STRING);
            if (getFlag(flags, JavaElementLabels.USE_RESOLVED) && field.isResolved()) {
                appendTypeSignatureLabel(field, new BindingKey(field.getKey()).toSignature(), flags);
            } else {
                appendTypeSignatureLabel(field, field.getTypeSignature(), flags);
            }
            //            if (getFlag(flags, JavaElementLabels.COLORIZE)) {
            //               fBuffer.setStyle(offset, fBuffer.length() - offset, DECORATIONS_STYLE);
            //            }
        }

        // category
        if (getFlag(flags, JavaElementLabels.F_CATEGORY) && field.exists())
            appendCategoryLabel(field, flags);

        // post qualification
        if (getFlag(flags, JavaElementLabels.F_POST_QUALIFIED)) {
            int offset = fBuffer.length();
            fBuffer.append(JavaElementLabels.CONCAT_STRING);
            appendTypeLabel(field.getDeclaringType(),
                    JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS));
            //            if (getFlag(flags, JavaElementLabels.COLORIZE)) {
            //               fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE);
            //            }
        }

    } catch (JavaModelException e) {
        // TODO
        e.printStackTrace();
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.ClassFileMatchLocator.java

License:Open Source License

/**
 * Locate declaration in the current class file. This class file is always in a jar.
 *//*from ww  w.  j a  v a 2s.  co  m*/
public void locateMatches(MatchLocator locator, ClassFile classFile, IBinaryType info) throws CoreException {
    SearchPattern pattern = locator.pattern;

    // check annotations references
    matchAnnotations(pattern, locator, classFile, info);

    // check class definition
    BinaryType binaryType = (BinaryType) classFile.getType();
    if (matchBinary(pattern, info, null)) {
        binaryType = new ResolvedBinaryType((JavaElement) binaryType.getParent(), binaryType.getElementName(),
                binaryType.getKey());
        locator.reportBinaryMemberDeclaration(null, binaryType, null, info, SearchMatch.A_ACCURATE);
        return;
    }

    // Define arrays to store methods/fields from binary type if necessary
    IBinaryMethod[] binaryMethods = info.getMethods();
    int bMethodsLength = binaryMethods == null ? 0 : binaryMethods.length;
    IBinaryMethod[] unresolvedMethods = null;
    char[][] binaryMethodSignatures = null;
    boolean hasUnresolvedMethods = false;

    // Get fields from binary type info
    IBinaryField[] binaryFields = info.getFields();
    int bFieldsLength = binaryFields == null ? 0 : binaryFields.length;
    IBinaryField[] unresolvedFields = null;
    boolean hasUnresolvedFields = false;

    // Report as many accurate matches as possible
    int accuracy = SearchMatch.A_ACCURATE;
    boolean mustResolve = pattern.mustResolve;
    if (mustResolve) {
        BinaryTypeBinding binding = locator.cacheBinaryType(binaryType, info);
        if (binding != null) {
            // filter out element not in hierarchy scope
            if (!locator.typeInHierarchy(binding))
                return;

            // Search matches on resolved methods
            MethodBinding[] availableMethods = binding.availableMethods();
            int aMethodsLength = availableMethods == null ? 0 : availableMethods.length;
            hasUnresolvedMethods = bMethodsLength != aMethodsLength;
            for (int i = 0; i < aMethodsLength; i++) {
                MethodBinding method = availableMethods[i];
                char[] methodSignature = method.genericSignature();
                if (methodSignature == null)
                    methodSignature = method.signature();

                // Report the match if possible
                int level = locator.patternLocator.resolveLevel(method);
                if (level != PatternLocator.IMPOSSIBLE_MATCH) {
                    IMethod methodHandle = binaryType.getMethod(
                            new String(method.isConstructor()
                                    ? binding.compoundName[binding.compoundName.length - 1]
                                    : method.selector),
                            CharOperation.toStrings(
                                    Signature.getParameterTypes(convertClassFileFormat(methodSignature))));
                    accuracy = level == PatternLocator.ACCURATE_MATCH ? SearchMatch.A_ACCURATE
                            : SearchMatch.A_INACCURATE;
                    locator.reportBinaryMemberDeclaration(null, methodHandle, method, info, accuracy);
                }

                // Remove method from unresolved list
                if (hasUnresolvedMethods) {
                    if (binaryMethodSignatures == null) { // Store binary method signatures to avoid multiple computation
                        binaryMethodSignatures = new char[bMethodsLength][];
                        for (int j = 0; j < bMethodsLength; j++) {
                            IBinaryMethod binaryMethod = binaryMethods[j];
                            char[] signature = binaryMethod.getGenericSignature();
                            if (signature == null)
                                signature = binaryMethod.getMethodDescriptor();
                            binaryMethodSignatures[j] = signature;
                        }
                    }
                    for (int j = 0; j < bMethodsLength; j++) {
                        if (CharOperation.equals(binaryMethods[j].getSelector(), method.selector)
                                && CharOperation.equals(binaryMethodSignatures[j], methodSignature)) {
                            if (unresolvedMethods == null) {
                                System.arraycopy(binaryMethods, 0,
                                        unresolvedMethods = new IBinaryMethod[bMethodsLength], 0,
                                        bMethodsLength);
                            }
                            unresolvedMethods[j] = null;
                            break;
                        }
                    }
                }
            }

            // Search matches on resolved fields
            FieldBinding[] availableFields = binding.availableFields();
            int aFieldsLength = availableFields == null ? 0 : availableFields.length;
            hasUnresolvedFields = bFieldsLength != aFieldsLength;
            for (int i = 0; i < aFieldsLength; i++) {
                FieldBinding field = availableFields[i];

                // Report the match if possible
                int level = locator.patternLocator.resolveLevel(field);
                if (level != PatternLocator.IMPOSSIBLE_MATCH) {
                    IField fieldHandle = binaryType.getField(new String(field.name));
                    accuracy = level == PatternLocator.ACCURATE_MATCH ? SearchMatch.A_ACCURATE
                            : SearchMatch.A_INACCURATE;
                    locator.reportBinaryMemberDeclaration(null, fieldHandle, field, info, accuracy);
                }

                // Remove the field from unresolved list
                if (hasUnresolvedFields) {
                    for (int j = 0; j < bFieldsLength; j++) {
                        if (CharOperation.equals(binaryFields[j].getName(), field.name)) {
                            if (unresolvedFields == null) {
                                System.arraycopy(binaryFields, 0,
                                        unresolvedFields = new IBinaryField[bFieldsLength], 0, bFieldsLength);
                            }
                            unresolvedFields[j] = null;
                            break;
                        }
                    }
                }
            }

            // If all methods/fields were accurate then returns now
            if (!hasUnresolvedMethods && !hasUnresolvedFields) {
                return;
            }
        }
        accuracy = SearchMatch.A_INACCURATE;
    }

    // Report inaccurate methods
    if (mustResolve)
        binaryMethods = unresolvedMethods;
    bMethodsLength = binaryMethods == null ? 0 : binaryMethods.length;
    for (int i = 0; i < bMethodsLength; i++) {
        IBinaryMethod method = binaryMethods[i];
        if (method == null)
            continue; // impossible match or already reported as accurate
        if (matchBinary(pattern, method, info)) {
            char[] name;
            if (method.isConstructor()) {
                // https://bugs.eclipse.org/bugs/show_bug.cgi?id=329727
                // We don't need the enclosing type name for the constructor name
                name = info.getSourceName();
            } else {
                name = method.getSelector();
            }
            String selector = new String(name);
            char[] methodSignature = binaryMethodSignatures == null ? null : binaryMethodSignatures[i];
            if (methodSignature == null) {
                methodSignature = method.getGenericSignature();
                if (methodSignature == null)
                    methodSignature = method.getMethodDescriptor();
            }
            String[] parameterTypes = CharOperation
                    .toStrings(Signature.getParameterTypes(convertClassFileFormat(methodSignature)));
            IMethod methodHandle = binaryType.getMethod(selector, parameterTypes);
            methodHandle = new ResolvedBinaryMethod(binaryType, selector, parameterTypes,
                    methodHandle.getKey());
            locator.reportBinaryMemberDeclaration(null, methodHandle, null, info, accuracy);
        }
    }

    // Report inaccurate fields
    if (mustResolve)
        binaryFields = unresolvedFields;
    bFieldsLength = binaryFields == null ? 0 : binaryFields.length;
    for (int i = 0; i < bFieldsLength; i++) {
        IBinaryField field = binaryFields[i];
        if (field == null)
            continue; // impossible match or already reported as accurate
        if (matchBinary(pattern, field, info)) {
            String fieldName = new String(field.getName());
            IField fieldHandle = binaryType.getField(fieldName);
            fieldHandle = new ResolvedBinaryField(binaryType, fieldName, fieldHandle.getKey());
            locator.reportBinaryMemberDeclaration(null, fieldHandle, null, info, accuracy);
        }
    }
}

From source file:com.codenvy.ide.ext.java.server.javadoc.JavaElementLabelComposer.java

License:Open Source License

/**
 * Appends the style label for a field. Considers the F_* flags.
 *
 * @param field the element to render//  w  w w .  ja v  a 2 s .com
 * @param flags the rendering flags. Flags with names starting with 'F_' are considered.
 */
public void appendFieldLabel(IField field, long flags) {
    try {

        if (getFlag(flags, JavaElementLabels.F_PRE_TYPE_SIGNATURE) && field.exists()
                && !Flags.isEnum(field.getFlags())) {
            if (getFlag(flags, JavaElementLabels.USE_RESOLVED) && field.isResolved()) {
                appendTypeSignatureLabel(field, new BindingKey(field.getKey()).toSignature(), flags);
            } else {
                appendTypeSignatureLabel(field, field.getTypeSignature(), flags);
            }
            fBuffer.append(' ');
        }

        // qualification
        if (getFlag(flags, JavaElementLabels.F_FULLY_QUALIFIED)) {
            appendTypeLabel(field.getDeclaringType(),
                    JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS));
            fBuffer.append('.');
        }
        fBuffer.append(getElementName(field));

        if (getFlag(flags, JavaElementLabels.F_APP_TYPE_SIGNATURE) && field.exists()
                && !Flags.isEnum(field.getFlags())) {
            int offset = fBuffer.length();
            fBuffer.append(JavaElementLabels.DECL_STRING);
            if (getFlag(flags, JavaElementLabels.USE_RESOLVED) && field.isResolved()) {
                appendTypeSignatureLabel(field, new BindingKey(field.getKey()).toSignature(), flags);
            } else {
                appendTypeSignatureLabel(field, field.getTypeSignature(), flags);
            }
            if (getFlag(flags, JavaElementLabels.COLORIZE)) {
                //               fBuffer.setStyle(offset, fBuffer.length() - offset, DECORATIONS_STYLE);
            }
        }

        // category
        if (getFlag(flags, JavaElementLabels.F_CATEGORY) && field.exists())
            appendCategoryLabel(field, flags);

        // post qualification
        if (getFlag(flags, JavaElementLabels.F_POST_QUALIFIED)) {
            int offset = fBuffer.length();
            fBuffer.append(JavaElementLabels.CONCAT_STRING);
            appendTypeLabel(field.getDeclaringType(),
                    JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS));
            if (getFlag(flags, JavaElementLabels.COLORIZE)) {
                //               fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE);
            }
        }

    } catch (JavaModelException e) {
        LOG.error(e.getMessage(), e); // NotExistsException will not reach this point
    }
}

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

License:Open Source License

/**
 * Appends the style label for a field. Considers the F_* flags.
 *
 * @param field the element to render/*from   w w  w. ja va  2s.  c o  m*/
 * @param flags the rendering flags. Flags with names starting with 'F_' are considered.
 */
public void appendFieldLabel(IField field, long flags) {
    try {

        if (getFlag(flags, F_PRE_TYPE_SIGNATURE) && field.exists() && !Flags.isEnum(field.getFlags())) {
            if (getFlag(flags, USE_RESOLVED) && field.isResolved()) {
                appendTypeSignatureLabel(field, new BindingKey(field.getKey()).toSignature(), flags);
            } else {
                appendTypeSignatureLabel(field, field.getTypeSignature(), flags);
            }
            fBuffer.append(' ');
        }

        // qualification
        if (getFlag(flags, F_FULLY_QUALIFIED)) {
            appendTypeLabel(field.getDeclaringType(), T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS));
            fBuffer.append('.');
        }
        fBuffer.append(getElementName(field));

        if (getFlag(flags, F_APP_TYPE_SIGNATURE) && field.exists() && !Flags.isEnum(field.getFlags())) {
            int offset = fBuffer.length();
            fBuffer.append(DECL_STRING);
            if (getFlag(flags, USE_RESOLVED) && field.isResolved()) {
                appendTypeSignatureLabel(field, new BindingKey(field.getKey()).toSignature(), flags);
            } else {
                appendTypeSignatureLabel(field, field.getTypeSignature(), flags);
            }
        }

        // category
        if (getFlag(flags, F_CATEGORY) && field.exists())
            appendCategoryLabel(field, flags);

        // post qualification
        if (getFlag(flags, F_POST_QUALIFIED)) {
            int offset = fBuffer.length();
            fBuffer.append(CONCAT_STRING);
            appendTypeLabel(field.getDeclaringType(), T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS));
        }

    } catch (JavaModelException e) {
        e.printStackTrace();
    }
}

From source file:edu.brown.cs.bubbles.bedrock.BedrockJava.java

License:Open Source License

/********************************************************************************/

void getFullyQualifiedName(String proj, String file, int start, int end, IvyXmlWriter xw)
        throws BedrockException {
    String name = null;/*w w  w. j  a  va2  s  . co  m*/
    String key = null;
    String sgn = null;
    String hdl = null;
    ICompilationUnit icu = our_plugin.getProjectManager().getCompilationUnit(proj, file);
    if (icu == null)
        throw new BedrockException("Compilation unit not found for " + file);
    icu = getCompilationElement(icu);

    try {
        IJavaElement[] elts = icu.codeSelect(start, end - start);
        for (int i = 0; i < elts.length && name == null; ++i) {
            switch (elts[i].getElementType()) {
            case IJavaElement.JAVA_PROJECT:
            case IJavaElement.JAVA_MODEL:
            case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            case IJavaElement.CLASS_FILE:
            case IJavaElement.PACKAGE_FRAGMENT:
            case IJavaElement.IMPORT_CONTAINER:
            case IJavaElement.IMPORT_DECLARATION:
            case IJavaElement.TYPE_PARAMETER:
            case IJavaElement.COMPILATION_UNIT:
            default:
                break;
            case IJavaElement.TYPE:
                IType typ = (IType) elts[i];
                name = typ.getFullyQualifiedName();
                key = typ.getKey();
                break;
            case IJavaElement.FIELD:
                IField fld = ((IField) elts[i]);
                name = fld.getDeclaringType().getFullyQualifiedName() + "." + fld.getElementName();
                key = fld.getKey();
                sgn = fld.getTypeSignature();
                break;
            case IJavaElement.METHOD:
                IMethod mthd = ((IMethod) elts[i]);
                name = mthd.getDeclaringType().getFullyQualifiedName() + "." + mthd.getElementName();
                key = mthd.getKey();
                sgn = mthd.getSignature();
                // TODO: might want to add signture here as well
                break;
            case IJavaElement.INITIALIZER:
                IInitializer init = ((IInitializer) elts[i]);
                name = init.getDeclaringType().getFullyQualifiedName() + ".<clinit>";
                break;
            case IJavaElement.PACKAGE_DECLARATION:
                name = ((IPackageDeclaration) elts[i]).getElementName();
                break;
            case IJavaElement.LOCAL_VARIABLE:
                ILocalVariable lcl = (ILocalVariable) elts[i];
                name = lcl.getHandleIdentifier();
                sgn = lcl.getTypeSignature();
                break;
            }
            hdl = elts[i].getHandleIdentifier();
        }
    } catch (CoreException e) {
        throw new BedrockException("Problem getting name", e);
    }

    if (name == null) {
        return;
        // throw new BedrockException("No identifier at location");
    }

    xw.begin("FULLYQUALIFIEDNAME");
    xw.field("NAME", name);
    if (key != null)
        xw.field("KEY", key);
    if (sgn != null)
        xw.field("TYPE", sgn);
    if (hdl != null)
        xw.field("HANDLE", hdl);
    xw.end();
}

From source file:edu.brown.cs.bubbles.bedrock.BedrockUtil.java

License:Open Source License

private static void outputNameDetails(IField fld, IvyXmlWriter xw) throws JavaModelException {
    String tnm = "Field";

    if (fld.isEnumConstant())
        tnm = "EnumConstant";

    outputSymbol(fld, tnm, fld.getElementName(), fld.getKey(), xw);
}

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

License:Open Source License

/**
 * Locate declaration in the current class file. This class file is always in a jar.
 *///from w w  w  .j  a  va2  s .  co  m
public void locateMatches(MatchLocator locator, ClassFile classFile, IBinaryType info) throws CoreException {
    SearchPattern pattern = locator.pattern;

    // check annotations references
    matchAnnotations(pattern, locator, classFile, info);

    // check class definition
    BinaryType binaryType = (BinaryType) classFile.getType();
    if (matchBinary(pattern, info, null)) {
        binaryType = new ResolvedBinaryType((JavaElement) binaryType.getParent(), binaryType.getElementName(),
                binaryType.getKey());
        locator.reportBinaryMemberDeclaration(null, binaryType, null, info, SearchMatch.A_ACCURATE);
        return;
    }

    // Define arrays to store methods/fields from binary type if necessary
    IBinaryMethod[] binaryMethods = info.getMethods();
    int bMethodsLength = binaryMethods == null ? 0 : binaryMethods.length;
    IBinaryMethod[] unresolvedMethods = null;
    char[][] binaryMethodSignatures = null;
    boolean hasUnresolvedMethods = false;

    // Get fields from binary type info
    IBinaryField[] binaryFields = info.getFields();
    int bFieldsLength = binaryFields == null ? 0 : binaryFields.length;
    IBinaryField[] unresolvedFields = null;
    boolean hasUnresolvedFields = false;

    // Report as many accurate matches as possible
    int accuracy = SearchMatch.A_ACCURATE;
    boolean mustResolve = pattern.mustResolve;
    if (mustResolve) {
        BinaryTypeBinding binding = locator.cacheBinaryType(binaryType, info);
        if (binding != null) {
            // filter out element not in hierarchy scope
            if (!locator.typeInHierarchy(binding))
                return;

            // Search matches on resolved methods
            MethodBinding[] availableMethods = binding.availableMethods();
            int aMethodsLength = availableMethods == null ? 0 : availableMethods.length;
            hasUnresolvedMethods = bMethodsLength != aMethodsLength;
            for (int i = 0; i < aMethodsLength; i++) {
                MethodBinding method = availableMethods[i];
                char[] methodSignature = method.genericSignature();
                if (methodSignature == null)
                    methodSignature = method.signature();

                // Report the match if possible
                int level = locator.patternLocator.resolveLevel(method);
                if (level != org.eclipse.jdt.internal.core.search.matching.PatternLocator.IMPOSSIBLE_MATCH) {
                    IMethod methodHandle = binaryType.getMethod(
                            new String(method.isConstructor()
                                    ? binding.compoundName[binding.compoundName.length - 1]
                                    : method.selector),
                            CharOperation.toStrings(
                                    Signature.getParameterTypes(convertClassFileFormat(methodSignature))));
                    accuracy = level == org.eclipse.jdt.internal.core.search.matching.PatternLocator.ACCURATE_MATCH
                            ? SearchMatch.A_ACCURATE
                            : SearchMatch.A_INACCURATE;
                    locator.reportBinaryMemberDeclaration(null, methodHandle, method, info, accuracy);
                }

                // Remove method from unresolved list
                if (hasUnresolvedMethods) {
                    if (binaryMethodSignatures == null) { // Store binary method signatures to avoid multiple computation
                        binaryMethodSignatures = new char[bMethodsLength][];
                        for (int j = 0; j < bMethodsLength; j++) {
                            IBinaryMethod binaryMethod = binaryMethods[j];
                            char[] signature = binaryMethod.getGenericSignature();
                            if (signature == null)
                                signature = binaryMethod.getMethodDescriptor();
                            binaryMethodSignatures[j] = signature;
                        }
                    }
                    for (int j = 0; j < bMethodsLength; j++) {
                        if (CharOperation.equals(binaryMethods[j].getSelector(), method.selector)
                                && CharOperation.equals(binaryMethodSignatures[j], methodSignature)) {
                            if (unresolvedMethods == null) {
                                System.arraycopy(binaryMethods, 0,
                                        unresolvedMethods = new IBinaryMethod[bMethodsLength], 0,
                                        bMethodsLength);
                            }
                            unresolvedMethods[j] = null;
                            break;
                        }
                    }
                }
            }

            // Search matches on resolved fields
            FieldBinding[] availableFields = binding.availableFields();
            int aFieldsLength = availableFields == null ? 0 : availableFields.length;
            hasUnresolvedFields = bFieldsLength != aFieldsLength;
            for (int i = 0; i < aFieldsLength; i++) {
                FieldBinding field = availableFields[i];

                // Report the match if possible
                int level = locator.patternLocator.resolveLevel(field);
                if (level != org.eclipse.jdt.internal.core.search.matching.PatternLocator.IMPOSSIBLE_MATCH) {
                    IField fieldHandle = binaryType.getField(new String(field.name));
                    accuracy = level == PatternLocator.ACCURATE_MATCH ? SearchMatch.A_ACCURATE
                            : SearchMatch.A_INACCURATE;
                    locator.reportBinaryMemberDeclaration(null, fieldHandle, field, info, accuracy);
                }

                // Remove the field from unresolved list
                if (hasUnresolvedFields) {
                    for (int j = 0; j < bFieldsLength; j++) {
                        if (CharOperation.equals(binaryFields[j].getName(), field.name)) {
                            if (unresolvedFields == null) {
                                System.arraycopy(binaryFields, 0,
                                        unresolvedFields = new IBinaryField[bFieldsLength], 0, bFieldsLength);
                            }
                            unresolvedFields[j] = null;
                            break;
                        }
                    }
                }
            }

            // If all methods/fields were accurate then returns now
            if (!hasUnresolvedMethods && !hasUnresolvedFields) {
                return;
            }
        }
        accuracy = SearchMatch.A_INACCURATE;
    }

    // Report inaccurate methods
    if (mustResolve)
        binaryMethods = unresolvedMethods;
    bMethodsLength = binaryMethods == null ? 0 : binaryMethods.length;
    for (int i = 0; i < bMethodsLength; i++) {
        IBinaryMethod method = binaryMethods[i];
        if (method == null)
            continue; // impossible match or already reported as accurate
        if (matchBinary(pattern, method, info)) {
            char[] name;
            if (method.isConstructor()) {
                // https://bugs.eclipse.org/bugs/show_bug.cgi?id=329727
                // We don't need the enclosing type name for the constructor name
                name = info.getSourceName();
            } else {
                name = method.getSelector();
            }
            String selector = new String(name);
            char[] methodSignature = binaryMethodSignatures == null ? null : binaryMethodSignatures[i];
            if (methodSignature == null) {
                methodSignature = method.getGenericSignature();
                if (methodSignature == null)
                    methodSignature = method.getMethodDescriptor();
            }
            String[] parameterTypes = CharOperation
                    .toStrings(Signature.getParameterTypes(convertClassFileFormat(methodSignature)));
            IMethod methodHandle = binaryType.getMethod(selector, parameterTypes);
            methodHandle = new ResolvedBinaryMethod(binaryType, selector, parameterTypes,
                    methodHandle.getKey());
            locator.reportBinaryMemberDeclaration(null, methodHandle, null, info, accuracy);
        }
    }

    // Report inaccurate fields
    if (mustResolve)
        binaryFields = unresolvedFields;
    bFieldsLength = binaryFields == null ? 0 : binaryFields.length;
    for (int i = 0; i < bFieldsLength; i++) {
        IBinaryField field = binaryFields[i];
        if (field == null)
            continue; // impossible match or already reported as accurate
        if (matchBinary(pattern, field, info)) {
            String fieldName = new String(field.getName());
            IField fieldHandle = binaryType.getField(fieldName);
            fieldHandle = new ResolvedBinaryField(binaryType, fieldName, fieldHandle.getKey());
            locator.reportBinaryMemberDeclaration(null, fieldHandle, null, info, accuracy);
        }
    }
}

From source file:org.eclipse.e4.demo.simpleide.jdt.internal.editor.viewer.JavaElementLabelComposer.java

License:Open Source License

/**
 * Appends the style label for a field. Considers the F_* flags.
 * /*w w  w .  j  av a2 s.  com*/
 * @param field
 *            the element to render
 * @param flags
 *            the rendering flags. Flags with names starting with 'F_' are
 *            considered.
 */
public void appendFieldLabel(IField field, long flags) {
    try {

        if (getFlag(flags, JavaElementLabels.F_PRE_TYPE_SIGNATURE) && field.exists()
                && !Flags.isEnum(field.getFlags())) {
            if (getFlag(flags, JavaElementLabels.USE_RESOLVED) && field.isResolved()) {
                appendTypeSignatureLabel(field, new BindingKey(field.getKey()).toSignature(), flags);
            } else {
                appendTypeSignatureLabel(field, field.getTypeSignature(), flags);
            }
            fBuffer.append(' ');
        }

        // qualification
        if (getFlag(flags, JavaElementLabels.F_FULLY_QUALIFIED)) {
            appendTypeLabel(field.getDeclaringType(),
                    JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS));
            fBuffer.append('.');
        }
        fBuffer.append(getElementName(field));

        if (getFlag(flags, JavaElementLabels.F_APP_TYPE_SIGNATURE) && field.exists()
                && !Flags.isEnum(field.getFlags())) {
            int offset = fBuffer.length();
            fBuffer.append(JavaElementLabels.DECL_STRING);
            if (getFlag(flags, JavaElementLabels.USE_RESOLVED) && field.isResolved()) {
                appendTypeSignatureLabel(field, new BindingKey(field.getKey()).toSignature(), flags);
            } else {
                appendTypeSignatureLabel(field, field.getTypeSignature(), flags);
            }
            if (getFlag(flags, JavaElementLabels.COLORIZE)) {
                fBuffer.setStyle(offset, fBuffer.length() - offset, DECORATIONS_STYLE);
            }
        }

        // category
        if (getFlag(flags, JavaElementLabels.F_CATEGORY) && field.exists())
            appendCategoryLabel(field, flags);

        // post qualification
        if (getFlag(flags, JavaElementLabels.F_POST_QUALIFIED)) {
            int offset = fBuffer.length();
            fBuffer.append(JavaElementLabels.CONCAT_STRING);
            appendTypeLabel(field.getDeclaringType(),
                    JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS));
            if (getFlag(flags, JavaElementLabels.COLORIZE)) {
                fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE);
            }
        }

    } catch (JavaModelException e) {
        logger.error(e);
    }
}

From source file:org.jboss.tools.ws.jaxrs.core.jdt.JdtUtils.java

License:Open Source License

/**
 * Finds the declaring {@link ASTNode} for the given {@link IMember}, using
 * the {@link NodeFinder} if the member was not resolved.
 * //from  ww  w. j a v a  2 s . co  m
 * @param member
 *            the member to find
 * @param ast
 *            the Compilation Unit
 * @return the associated declaring node
 * @throws JavaModelException
 */
private static ASTNode findDeclaringNode(final IMember member, final CompilationUnit ast)
        throws JavaModelException {
    switch (member.getElementType()) {
    case IJavaElement.TYPE:
        final IType type = (IType) member;
        if (type.isResolved()) {
            final ASTNode typeNode = ast.findDeclaringNode(type.getKey());
            // return if match found
            if (typeNode != null) {
                return typeNode;
            }
        }
        break;
    case IJavaElement.METHOD:
        final IMethod method = (IMethod) member;
        if (method.isResolved()) {
            final ASTNode methodNode = ast.findDeclaringNode(method.getKey());
            // return if match found
            if (methodNode != null) {
                return methodNode;
            }
        }
        break;
    case IJavaElement.FIELD:
        final IField field = (IField) member;
        if (field.isResolved()) {
            // in the case of a Field, the
            // CompilationUnit#findDeclaringNode(String key) method returns
            // a VariableDeclarationFragment in a FieldDeclaration
            final ASTNode variableDeclarationFragment = ast.findDeclaringNode(field.getKey());
            if (variableDeclarationFragment != null) {
                final ASTNode fieldNode = variableDeclarationFragment.getParent();
                if (fieldNode != null) {
                    // return if match found
                    return fieldNode;
                }
            }
        }
        break;
    default:
    }
    // fallback approach if everything above failed.
    final NodeFinder finder = new NodeFinder(ast, member.getSourceRange().getOffset(),
            member.getSourceRange().getLength());
    return finder.getCoveredNode();
}

From source file:org.springframework.tooling.jdt.ls.commons.java.JavaData.java

License:Open Source License

private FieldData createFieldData(IType type, IField field) {
    FieldData data = new FieldData();
    fillMemberData(field, data);//from   w  w  w.  j  av a2s  .c  om
    data.setBindingKey(field.getKey());
    ImmutableList.Builder<AnnotationData> annotationsBuilder = ImmutableList.builder();
    try {
        for (IAnnotation annotation : field.getAnnotations()) {
            annotationsBuilder.add(createAnnotationData(type, annotation));
        }
        data.setType(createFromSignature(type, field.getTypeSignature()));
        data.setEnumConstant(field.isEnumConstant());
    } catch (JavaModelException e) {
        logger.log(e);
    }
    data.setAnnotations(annotationsBuilder.build());
    return data;
}