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

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

Introduction

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

Prototype

int METHOD

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

Click Source Link

Document

Constant representing a method or constructor.

Usage

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

License:Open Source License

public void consumeTypeVariable(char[] position, char[] typeVariableName) {
    if (this.element == null)
        return;// ww  w.  jav a 2s.  c o  m
    switch (this.element.getElementType()) {
    case IJavaElement.TYPE:
        this.element = ((IType) this.element).getTypeParameter(new String(typeVariableName));
        break;
    case IJavaElement.METHOD:
        this.element = ((IMethod) this.element).getTypeParameter(new String(typeVariableName));
        break;
    }
}

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//ww w .  j a v a2  s  . c  om
 * @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.drgarbage.asm.render.impl.OutlineElementMethod.java

License:Apache License

/**
 * Constructs a method outline object./*  w w  w. ja v  a2  s . c  o m*/
 * @param parent
 */
public OutlineElementMethod(IType parent, IClassFileDocument document, int methodIndex) {
    super(com.drgarbage.asm.Opcodes.ASM4);
    this.classFileDocument = document;
    this.methodIndex = methodIndex;
    setElementType(IJavaElement.METHOD);
    setDeclaringType(parent);
}

From source file:com.drgarbage.bytecodevisualizer.editors.BytecodeEditor.java

License:Apache License

/**
 * Selects the given line and revaluate visible position.
 * @param bytecodeDocumentLine the line number of the bytecode document
 * @param elementName /*from w  w  w  . ja  v  a2 s. c  o m*/
 * @param elementType the element type one of {@link IJavaElement IJavaElement.CLASS_FILE},
 *       {@link IJavaElement IJavaElement.FIELD} or {@link IJavaElement IJavaElement.METHOD}.
 * @see IJavaElement
 */
public void selectBytecodeLineAndReveal(int bytecodeDocumentLine, String elementName, int elementType) {
    IDocument document = byteCodeDocumentProvider.getBytecodeDocument(getBytecodeEditorInput());
    try {
        /* get line information */
        IRegion region = document.getLineInformation(bytecodeDocumentLine);
        int lineStartOffset = region.getOffset();
        int lineLenght = region.getLength();
        String lineString = document.get(lineStartOffset, lineLenght);

        if (elementName == null) {
            super.selectAndReveal(lineStartOffset, 0);
        }

        int elementIndex, elementLength;
        switch (elementType) {
        case IJavaElement.CLASS_FILE:
            elementIndex = lineString.indexOf(" " + elementName + " {") + 1;
            break;
        case IJavaElement.FIELD:
            elementIndex = lineString.indexOf(" " + elementName + ";") + 1;
            break;
        case IJavaElement.METHOD:
            elementIndex = lineString.indexOf(" " + elementName + "(") + 1;
            break;
        default:
            elementIndex = 0;
            elementLength = 0;
        }

        /* name not found */
        if (elementIndex == 0) {
            elementLength = 0;
        } else {
            elementLength = elementName.length();
        }

        super.selectAndReveal(lineStartOffset + elementIndex, elementLength);

    } catch (BadLocationException e) {
        /*nothing to do */}
}

From source file:com.drgarbage.bytecodevisualizer.editors.BytecodeEditor.java

License:Apache License

/**
 * Selects the line for the given element:
 * - ClassFileSection//from  w ww  . j  a v  a  2s  . co m
 * - MethodSection
 * - Variable
 * 
 * @param element
 * @param line
 * @param markLine true if the line has to be marked
 */
public void setSelection(IJavaElement element, int line, boolean markLine) {

    if (element == null) {
        return;
    }

    switch (element.getElementType()) {
    case IJavaElement.FIELD: /* select field */
        String fieldName = element.getElementName();
        IFieldSection fieldSection = byteCodeDocumentProvider.getClassFileDocument()
                .findFieldSection(fieldName);
        if (fieldSection != null) {
            selectBytecodeLineAndReveal(fieldSection.getBytecodeDocumentLine(), fieldName, IJavaElement.FIELD);
        }
        break;
    case IJavaElement.METHOD: /* select method */
        IMethod method = (IMethod) element;
        try {
            String methodName;
            if (method.isConstructor()) {
                methodName = "<init>";
            } else {
                methodName = method.getElementName();
            }

            String methodSignature = ClassFileDocumentsUtils.resolveMethodSignature(method);
            IMethodSection methodSection = byteCodeDocumentProvider.getClassFileDocument()
                    .findMethodSection(methodName, methodSignature);

            if (methodSection != null) {
                if (line == IClassFileEditor.INVALID_LINE || line == IClassFileEditor.METHOD_DECLARATION_LINE) {

                    if (method.isConstructor()) {
                        methodName = method.getElementName();
                    }
                    selectBytecodeLineAndReveal(methodSection.getFirstLine(), methodName, IJavaElement.METHOD);
                } else {
                    selectLineAndRevaluate2(methodSection.getBytecodeLine(line) - 1, markLine);
                }
                break;
            }

        } catch (JavaModelException e) {
            BytecodeVisualizerPlugin
                    .log(new Status(IStatus.ERROR, BytecodeVisualizerPlugin.PLUGIN_ID, e.getMessage(), e));
        }

    default:
        /* select class */
        IClassFileDocument doc = byteCodeDocumentProvider.getClassFileDocument();
        if (doc != null) {
            int docLine = doc.getClassSignatureDocumentLine();
            selectBytecodeLineAndReveal(docLine, element.getElementName(), IJavaElement.CLASS_FILE);
        }
    }

    /* call cursor hook */
    doHandleCursorPositionChanged();
}

From source file:com.drgarbage.bytecodevisualizer.editors.BytecodeMarkerAnnotationModel.java

License:Apache License

protected Position createPositionFromMarker(IMarker marker) {

    if (classFileDocument == null) {
        return null;
    }//from  w  w  w .jav a 2  s.co  m

    String markerType = null;
    try {
        markerType = marker.getType();
    } catch (CoreException e1) {
        BytecodeVisualizerPlugin.log(
                new Status(IStatus.ERROR, CoreConstants.BYTECODE_VISUALIZER_PLUGIN_ID, e1.getMessage(), e1));
    }
    int l = ByteCodeConstants.INVALID_LINE;

    if (markerType != null) {
        if (markerType.equals("org.eclipse.jdt.debug.javaMethodBreakpointMarker")) {
            int line = MarkerUtilities.getLineNumber(marker);
            String methodName = marker.getAttribute("org.eclipse.jdt.debug.core.methodName", "NULL");
            String methodSignature = marker.getAttribute("org.eclipse.jdt.debug.core.methodSignature", "NULL");

            IMethodSection m = classFileDocument.findMethodSection(line);
            if (m == null) {
                m = classFileDocument.findMethodSection(methodName, methodSignature);
            }
            if (m != null) {
                l = m.getFirstLine();
            }
        } else if (markerType.equals("org.eclipse.jdt.debug.javaClassPrepareBreakpointMarker")) {
            l = classFileDocument.getClassSignatureDocumentLine();
        } else if (markerType.equals("org.eclipse.jdt.debug.javaWatchpointMarker")) {
            String fieldName = marker.getAttribute("org.eclipse.jdt.debug.core.fieldName", "NULL");

            IFieldSection f = classFileDocument.findFieldSection(fieldName);
            l = f.getBytecodeDocumentLine();
        } else if (markerType.equals("org.eclipse.jdt.debug.javaLineBreakpointMarker")) {
            int line = MarkerUtilities.getLineNumber(marker);

            int start = marker.getAttribute("org.eclipse.jdt.debug.ui.member_start",
                    ByteCodeConstants.INVALID_LINE);

            try {
                IJavaElement element = fClassFile.getElementAt(start);
                if (element != null && element.getElementType() == IJavaElement.METHOD) {
                    IMethod method = (IMethod) element;

                    String methodName;
                    if (method.isConstructor()) {
                        methodName = "<init>";
                    } else {
                        methodName = method.getElementName();
                    }

                    String methodSignature = ClassFileDocumentsUtils.resolveMethodSignature(method);
                    IMethodSection methodSection = classFileDocument.findMethodSection(methodName,
                            methodSignature);

                    if (methodSection != null) {
                        l = methodSection.getBytecodeLine(line) - 1;
                    } else {
                        return null;
                    }

                } else {
                    return null;
                }

            } catch (JavaModelException e) {
                BytecodeVisualizerPlugin.log(new Status(IStatus.ERROR,
                        CoreConstants.BYTECODE_VISUALIZER_PLUGIN_ID, e.getMessage(), e));
            }
        }
    }

    int start;
    try {
        start = fDocument.getLineOffset(l);
    } catch (BadLocationException e) {
        start = 0;
    }
    return new Position(start, 0);
}

From source file:com.drgarbage.bytecodevisualizer.editors.BytecodeVisualizerUtils.java

License:Apache License

public static int verifyLine(ISourceViewer sourceViewer, IJavaElement element, int caret) {
    if (element == null) {
        return IClassFileEditor.INVALID_LINE;
    }//from  w w  w.  java2s . c om

    if (element == null) {
        return IClassFileEditor.INVALID_LINE;
    }

    IDocument document = sourceViewer.getDocument();
    if (document == null) {
        return IClassFileEditor.INVALID_LINE;
    }

    int line = IClassFileEditor.INVALID_LINE;
    try {
        /* get current line */
        line = document.getLineOfOffset(caret);

        /* check if the method declaration is selected */
        if (element.getElementType() == IJavaElement.METHOD) {
            IMethod method = (IMethod) element;
            int startOffset = method.getSourceRange().getOffset();
            int startLine = document.getLineOfOffset(startOffset);
            if (startLine == line) {
                line = IClassFileEditor.METHOD_DECLARATION_LINE;
            } else {
                line++;
            }
        }

    } catch (BadLocationException e) {

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

        e.printStackTrace();
        BytecodeVisualizerPlugin.log(e);
    }

    return line;
}

From source file:com.drgarbage.bytecodevisualizer.editors.ToggleBytecodeBreakpointAdapter.java

License:Apache License

public void toggleBreakpoints(IWorkbenchPart part, ISelection selection) throws CoreException {

    ISelection sel = translateToMembers(part, selection);
    if (sel instanceof IStructuredSelection) {
        IStructuredSelection structuredSelection = (IStructuredSelection) sel;
        if (!structuredSelection.isEmpty()) {
            IMember member = (IMember) structuredSelection.getFirstElement();
            int mtype = member.getElementType();
            if (mtype == IJavaElement.FIELD) {
                toggleWatchpoints(part, sel);
            } else if (mtype == IJavaElement.METHOD) {
                toggleMethodBreakpoints(part, sel);
            } else if (member.getElementType() == IJavaElement.TYPE) {
                toggleClassBreakpoints(part, sel);
            } else {
                BytecodeVisualizerPlugin
                        .log(new IllegalStateException("Uncovered element type " + member.getElementType()));
                //               /*
                //                * fall back to old behavior, always create a line
                //                * breakpoint
                //                */
                //               /* should never occur */
                //               toggleLineBreakpoints(part, selection, true);
            }//ww w  .  jav  a 2  s  .com
        }

    }
}

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

License:Open Source License

static IMethod findLambdaMethodIn(IJavaElement element) {
    try {/*from  w ww  .j  av a  2  s  .c  o m*/
        if (element.getElementType() == IJavaElement.TYPE) {
            IType t = (IType) element;
            if (t.isAnonymous()) {
                IJavaElement[] c = t.getChildren();
                if (c.length == 1 && c[0].getElementType() == IJavaElement.METHOD) {
                    IMethod m = (IMethod) c[0];
                    return m;
                    // IAnnotation[] annotations = m.getAnnotations();
                    // if (annotations != null) {
                    // for (IAnnotation annotation : annotations) {
                    // if (annotation.getElementName().endsWith("LambdaFolded"))
                    // return m;
                    // }
                    // }
                    // }
                }
            }
        }
    } catch (Exception e) {
    }
    return null;
}

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.  jav a 2  s  . c  o  m*/
 * {@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);
    }
}