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:de.fu_berlin.inf.jtourbus.actions.InsertJTourBusCommentAction.java

License:Open Source License

public void run(ITextSelection selection, JavaEditor fEditor) {
    try {//from   www  .  ja v  a2s  . c o  m
        IJavaElement element = SelectionConverter.getElementAtOffset(fEditor);
        if (!ActionUtil.isProcessable(fSite.getShell(), element))
            return;
        int type = element != null ? element.getElementType() : -1;
        if (type != IJavaElement.METHOD && type != IJavaElement.TYPE && type != IJavaElement.FIELD) {
            element = SelectionConverter.getTypeAtOffset(fEditor);
            if (element == null) {
                MessageDialog.openInformation(fSite.getShell(), getDialogTitle(),
                        ActionMessages.AddJavaDocStubsAction_not_applicable);
                return;
            }
        }
        IMember[] members = new IMember[] { (IMember) element };
        if (ElementValidator.checkValidateEdit(members, fSite.getShell(), getDialogTitle()))
            run(((IMember) element).getCompilationUnit(), (IMember) element);
    } catch (CoreException e) {
        ExceptionHandler.handle(e, fSite.getShell(), getDialogTitle(),
                ActionMessages.AddJavaDocStubsAction_error_actionFailed);
    }
}

From source file:de.jcup.egradle.eclipse.JavaHelper.java

License:Apache License

/**
 * Returns currently selected method or <code>null</code>
 * @param editor/*  w  w  w . j a va 2s .com*/
 * @return method or <code>null</code>
 * 
 */
public IMethod getCurrentSelectedJavaMethod(ITextEditor editor) {
    if (editor == null) {
        return null;
    }

    IEditorInput editorInput = editor.getEditorInput();
    if (editorInput == null) {
        return null;
    }
    IJavaElement elem = JavaUI.getEditorInputJavaElement(editorInput);
    if (elem instanceof ICompilationUnit) {
        ITextSelection sel = (ITextSelection) editor.getSelectionProvider().getSelection();
        IJavaElement selected;
        try {
            selected = ((ICompilationUnit) elem).getElementAt(sel.getOffset());
        } catch (JavaModelException e) {
            EGradleUtil.log(e);
            return null;
        }
        if (selected == null) {
            return null;
        }
        if (selected.getElementType() == IJavaElement.METHOD) {
            return (IMethod) selected;
        }
    }
    return null;
}

From source file:de.loskutov.bco.ui.actions.BytecodeAction.java

License:Open Source License

protected TypedElement createTypedElement(IJavaElement javaElement, BitSet modes) {
    String name;/*  www  .j  a  v a2s. c om*/
    IClassFile classFile = (IClassFile) javaElement.getAncestor(IJavaElement.CLASS_FILE);
    // existing read-only class files
    if (classFile != null) {
        name = classFile.getPath().toOSString();
        if (!name.endsWith(".class")) { //$NON-NLS-1$
            name += '/' + JdtUtils.getFullBytecodeName(classFile);
        }
    } else {
        // usual eclipse - generated bytecode
        name = JdtUtils.getByteCodePath(javaElement);
    }
    String methodName = null;
    if (javaElement.getElementType() == IJavaElement.METHOD
            || javaElement.getElementType() == IJavaElement.INITIALIZER) {
        methodName = JdtUtils.getMethodSignature(javaElement);
        if (methodName != null) {
            name += ":" + methodName;
        }
    }
    return new TypedElement(name, methodName, TypedElement.TYPE_BYTECODE, javaElement, modes);
}

From source file:de.loskutov.bco.ui.JdtUtils.java

License:Open Source License

public static IJavaElement getMethod(IParent parent, String signature) {
    try {/* w ww.  j  a  va  2 s.  c  om*/
        IJavaElement[] children = parent.getChildren();
        for (int i = 0; i < children.length; i++) {
            IJavaElement javaElement = children[i];
            switch (javaElement.getElementType()) {
            case IJavaElement.INITIALIZER:
                // fall through
            case IJavaElement.METHOD:
                if (signature.equals(getMethodSignature(javaElement))) {
                    return javaElement;
                }
                break;
            default:
                break;
            }
            if (javaElement instanceof IParent) {
                javaElement = getMethod((IParent) javaElement, signature);
                if (javaElement != null) {
                    return javaElement;
                }
            }
        }
    } catch (JavaModelException e) {
        // just ignore it. Mostly caused by class files not on the class path
        // which is not a problem for us, but a big problem for JDT
    }
    return null;
}

From source file:de.loskutov.bco.ui.JdtUtils.java

License:Open Source License

/**
 * @param childEl//w  ww  . j  a va2 s .c  o  m
 * @return method signature, if given java element is either initializer or method,
 * otherwise returns null.
 */
public static String getMethodSignature(IJavaElement childEl) {
    String methodName = null;
    if (childEl.getElementType() == IJavaElement.INITIALIZER) {
        IInitializer ini = (IInitializer) childEl;
        try {
            if (Flags.isStatic(ini.getFlags())) {
                methodName = "<clinit>()V";
            } else {
                methodName = "<init>()";
            }
        } catch (JavaModelException e) {
            // this is compilation problem - don't show the message
            BytecodeOutlinePlugin.log(e, IStatus.WARNING);
        }
    } else if (childEl.getElementType() == IJavaElement.METHOD) {
        IMethod iMethod = (IMethod) childEl;
        try {
            methodName = createMethodSignature(iMethod);
        } catch (JavaModelException e) {
            // this is compilation problem - don't show the message
            BytecodeOutlinePlugin.log(e, IStatus.WARNING);
        }
    }
    return methodName;
}

From source file:de.loskutov.bco.ui.JdtUtils.java

License:Open Source License

/**
 * @param javaElement/*w w w  .jav  a  2 s .c  o  m*/
 * @param topAncestor
 * @param sb
 */
private static String getClassName(IJavaElement javaElement, IJavaElement topAncestor) {
    StringBuffer sb = new StringBuffer();
    if (!javaElement.equals(topAncestor)) {
        int elementType = javaElement.getElementType();
        if (elementType == IJavaElement.FIELD || elementType == IJavaElement.METHOD
                || elementType == IJavaElement.INITIALIZER) {
            // it's field or method
            javaElement = getFirstAncestor(javaElement);
        } else {
            boolean is50OrHigher = is50OrHigher(javaElement);
            if (!is50OrHigher && (isAnonymousType(javaElement) || isLocal(javaElement))) {
                // it's inner type
                sb.append(getElementName(topAncestor));
                sb.append(TYPE_SEPARATOR);
            } else {
                /*
                 * TODO there is an issue with < 1.5 compiler setting and with inner
                 * classes with the same name but defined in different methods in the same
                 * source file. Then compiler needs to generate *different* content for
                 *  A$1$B and A$1$B, which is not possible so therefore compiler generates
                 *  A$1$B and A$2$B. The naming order is the source range order of inner
                 *  classes, so the first inner B class will get A$1$B and the second
                 *  inner B class A$2$B etc.
                 */

                // override top ancestor with immediate ancestor
                topAncestor = getFirstAncestor(javaElement);
                while (topAncestor != null) {
                    sb.insert(0, getElementName(topAncestor) + TYPE_SEPARATOR);
                    topAncestor = getFirstAncestor(topAncestor);
                }
            }
        }
    }
    sb.append(getElementName(javaElement));
    return sb.toString();
}

From source file:de.loskutov.bco.ui.JdtUtils.java

License:Open Source License

/**
 * Check if java element is an interface or abstract method or a method from
 * interface.//from  w ww  .  j  a  va  2s .  co  m
 */
public static boolean isAbstractOrInterface(IJavaElement javaEl) {
    if (javaEl == null) {
        return true;
    }
    boolean abstractOrInterface = false;
    try {
        switch (javaEl.getElementType()) {
        case IJavaElement.CLASS_FILE:
            IClassFile classFile = (IClassFile) javaEl;
            if (isOnClasspath(javaEl)) {
                abstractOrInterface = classFile.isInterface();
            } /*else {
               this is the case for eclipse-generated class files.
               if we do not perform the check in if, then we will have java model
               exception on classFile.isInterface() call.
              }*/
            break;
        case IJavaElement.COMPILATION_UNIT:
            ICompilationUnit cUnit = (ICompilationUnit) javaEl;
            IType type = cUnit.findPrimaryType();
            abstractOrInterface = type != null && type.isInterface();
            break;
        case IJavaElement.TYPE:
            abstractOrInterface = ((IType) javaEl).isInterface();
            break;
        case IJavaElement.METHOD:
            // test for "abstract" flag on method in a class
            abstractOrInterface = Flags.isAbstract(((IMethod) javaEl).getFlags());
            // "abstract" flags could be not exist on interface methods
            if (!abstractOrInterface) {
                IType ancestor = (IType) javaEl.getAncestor(IJavaElement.TYPE);
                abstractOrInterface = ancestor != null && ancestor.isInterface();
            }
            break;
        default:
            IType ancestor1 = (IType) javaEl.getAncestor(IJavaElement.TYPE);
            abstractOrInterface = ancestor1 != null && ancestor1.isInterface();
            break;
        }
    } catch (JavaModelException e) {
        // No point to log it here
        // BytecodeOutlinePlugin.log(e, IStatus.ERROR);
    }
    return abstractOrInterface;
}

From source file:de.loskutov.bco.views.BytecodeOutlineView.java

License:Open Source License

/**
 * @return IJavaElement which fits in the current selection in java editor
 *///from  w w  w. j  a v  a 2 s  . co  m
private IJavaElement getCurrentJavaElement() {
    IJavaElement childEl = null;
    try {
        childEl = JdtUtils.getElementAtOffset(javaInput, currentSelection);
        if (childEl != null) {
            switch (childEl.getElementType()) {
            case IJavaElement.METHOD:
            case IJavaElement.FIELD:
            case IJavaElement.INITIALIZER:
            case IJavaElement.TYPE:
                break;
            case IJavaElement.LOCAL_VARIABLE:
                childEl = childEl.getAncestor(IJavaElement.METHOD);
                break;
            default:
                childEl = null;
                break;
            }
        }
    } catch (JavaModelException e) {
        // the exception is mostly occured if java structure was
        // changed and current element is not more exist in model
        // e.g. on rename/delete/move operation.
        // so it is not an error for user, but info for us
        BytecodeOutlinePlugin.log(e, IStatus.INFO);
        setJavaInput(null);
        lastChildElement = null;
    }
    return childEl;
}

From source file:dynamicrefactoring.util.selection.SelectionInfo.java

License:Open Source License

/**
 * Obtiene el nombre completamente cualificado de los tipos de elemento 
 * descritos por la interfaz <code>IJavaElement</code>.
 * //  ww  w  .j a  v  a2 s .c o m
 * @param type el valor codificado por IJavaElement para el tipo de elemento Java.
 * 
 * @return el nombre completamente cualificado del tipo equivalente.
 */
protected String decodeElementType(int type) {
    switch (type) {
    case IJavaElement.TYPE:
        return TYPE_NAME;
    case IJavaElement.FIELD:
        return FIELD_NAME;
    case IJavaElement.METHOD:
        return METHOD_NAME;
    case IJavaElement.TYPE_PARAMETER:
        return PARAMETER_NAME;
    case FORMAL_ARGUMENT:
        return FORMAL_ARGUMENT_NAME;
    default:
        return ""; //$NON-NLS-1$
    }
}

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

License:Open Source License

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

void rename(String proj, String bid, String file, int start, int end, String name, String handle,
        String newname, boolean keeporig, boolean getters, boolean setters, boolean dohier, boolean qual,
        boolean refs, boolean dosimilar, boolean textocc, boolean doedit, String filespat, IvyXmlWriter xw)
        throws BedrockException {
    FileData fd = file_map.get(file);//w ww .ja v  a 2  s. co  m
    ICompilationUnit icu;

    if (doedit) {
        // icu = fd.getDefaultUnit();
        icu = fd.getEditableUnit(bid);
    } else
        icu = fd.getEditableUnit(bid);

    IJavaElement[] elts;
    try {
        elts = icu.codeSelect(start, end - start);
    } catch (JavaModelException e) {
        throw new BedrockException("Bad location: " + e, e);
    }

    IJavaElement relt = null;
    for (IJavaElement ije : elts) {
        if (handle != null && !handle.equals(ije.getHandleIdentifier()))
            continue;
        if (name != null && !name.equals(ije.getElementName()))
            continue;
        relt = ije;
        break;
    }
    if (relt == null)
        throw new BedrockException("Item to rename not found");

    String id = null;
    switch (relt.getElementType()) {
    case IJavaElement.COMPILATION_UNIT:
        id = IJavaRefactorings.RENAME_COMPILATION_UNIT;
        break;
    case IJavaElement.FIELD:
        IField ifld = (IField) relt;
        try {
            if (ifld.isEnumConstant())
                id = IJavaRefactorings.RENAME_ENUM_CONSTANT;
            else
                id = IJavaRefactorings.RENAME_FIELD;
        } catch (JavaModelException e) {
        }
        break;
    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
    case IJavaElement.PACKAGE_FRAGMENT:
        id = IJavaRefactorings.RENAME_PACKAGE;
        break;
    case IJavaElement.LOCAL_VARIABLE:
        id = IJavaRefactorings.RENAME_LOCAL_VARIABLE;
        break;
    case IJavaElement.TYPE:
        id = IJavaRefactorings.RENAME_TYPE;
        break;
    case IJavaElement.TYPE_PARAMETER:
        id = IJavaRefactorings.RENAME_TYPE_PARAMETER;
        break;
    case IJavaElement.METHOD:
        id = IJavaRefactorings.RENAME_METHOD;
        break;
    case IJavaElement.ANNOTATION:
    case IJavaElement.CLASS_FILE:
    case IJavaElement.IMPORT_CONTAINER:
    case IJavaElement.IMPORT_DECLARATION:
    case IJavaElement.INITIALIZER:
    case IJavaElement.JAVA_MODEL:
    case IJavaElement.JAVA_PROJECT:
    case IJavaElement.PACKAGE_DECLARATION:
        break;
    }
    if (id == null)
        throw new BedrockException("Invalid element type to rename");

    RenameJavaElementDescriptor renamer;

    RefactoringContribution rfc = RefactoringCore.getRefactoringContribution(id);
    if (rfc == null) {
        xw.begin("FAILURE");
        xw.field("TYPE", "SETUP");
        xw.textElement("ID", id);
        xw.end("FAILURE");
        renamer = new RenameJavaElementDescriptor(id);
    } else {
        renamer = (RenameJavaElementDescriptor) rfc.createDescriptor();
    }

    renamer.setJavaElement(relt);
    renamer.setKeepOriginal(keeporig);
    renamer.setNewName(newname);
    if (proj != null)
        renamer.setProject(proj);
    renamer.setRenameGetters(getters);
    renamer.setRenameSetters(setters);
    renamer.setUpdateHierarchy(dohier);
    renamer.setUpdateQualifiedNames(qual);
    renamer.setUpdateReferences(refs);
    renamer.setUpdateSimilarDeclarations(dosimilar);
    renamer.setUpdateTextualOccurrences(textocc);
    if (filespat != null)
        renamer.setFileNamePatterns(filespat);

    RefactoringStatus sts = renamer.validateDescriptor();
    if (!sts.isOK()) {
        xw.begin("FAILURE");
        xw.field("TYPE", "VALIDATE");
        BedrockUtil.outputStatus(sts, xw);
        xw.end("FAILURE");
        return;
    }

    try {
        Refactoring refactor = renamer.createRefactoring(sts);
        if (refactor == null) {
            xw.begin("FAILURE");
            xw.field("TYPE", "CREATE");
            xw.textElement("RENAMER", renamer.toString());
            xw.textElement("REFACTOR", renamer.toString());
            xw.textElement("STATUS", sts.toString());
            xw.end("FAILURE");
            return;
        }

        refactor.setValidationContext(null);

        // this seems to reset files from disk (mutliple times)
        sts = refactor.checkAllConditions(new NullProgressMonitor());
        if (!sts.isOK()) {
            xw.begin("FAILURE");
            xw.field("TYPE", "CHECK");
            BedrockUtil.outputStatus(sts, xw);
            xw.end("FAILURE");
            if (sts.hasFatalError())
                return;
        }
        BedrockPlugin.logD("RENAME: Refactoring checked");

        Change chng = refactor.createChange(new NullProgressMonitor());
        BedrockPlugin.logD("RENAME: Refactoring change created");

        if (doedit && chng != null) {
            chng.perform(new NullProgressMonitor());
        } else if (chng != null) {
            xw.begin("EDITS");
            BedrockUtil.outputChange(chng, xw);
            xw.end("EDITS");
        }
    } catch (CoreException e) {
        throw new BedrockException("Problem creating refactoring: " + e, e);
    }

    BedrockPlugin.logD("RENAME RESULT = " + xw.toString());
}