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:org.eclipse.jst.jsf.common.ui.internal.utils.JavaModelUtil.java

License:Open Source License

/**
 * Evaluates if a member (possible from another package) is visible from
 * elements in a package.//w ww .  ja  v a  2  s.c  o m
 * 
 * @param member
 *            The member to test the visibility for
 * @param pack
 *            The package in focus
 * @return true if visible
 * @throws JavaModelException 
 */
public static boolean isVisible(IMember member, IPackageFragment pack) throws JavaModelException {

    int type = member.getElementType();
    if (type == IJavaElement.INITIALIZER
            || (type == IJavaElement.METHOD && member.getElementName().startsWith("<"))) { //$NON-NLS-1$
        //$NON-NLS-1$
        return false;
    }

    int otherflags = member.getFlags();
    IType declaringType = member.getDeclaringType();
    if (Flags.isPublic(otherflags) || (declaringType != null && declaringType.isInterface())) {
        return true;
    } else if (Flags.isPrivate(otherflags)) {
        return false;
    }

    IPackageFragment otherpack = (IPackageFragment) findParentOfKind(member, IJavaElement.PACKAGE_FRAGMENT);
    return (pack != null && otherpack != null && isSamePackage(pack, otherpack));
}

From source file:org.eclipse.jst.jsf.common.ui.internal.utils.JavaModelUtil.java

License:Open Source License

/**
 * Evaluates if a member in the focus' element hierarchy is visible from
 * elements in a package.//from   w  ww .java 2s.  co  m
 * 
 * @param member
 *            The member to test the visibility for
 * @param pack
 *            The package of the focus element focus
 * @return true if is visible in hiearchy
 * @throws JavaModelException 
 */
public static boolean isVisibleInHierarchy(IMember member, IPackageFragment pack) throws JavaModelException {
    int type = member.getElementType();
    if (type == IJavaElement.INITIALIZER
            || (type == IJavaElement.METHOD && member.getElementName().startsWith("<"))) { //$NON-NLS-1$
        //$NON-NLS-1$
        return false;
    }

    int otherflags = member.getFlags();

    IType declaringType = member.getDeclaringType();
    if (Flags.isPublic(otherflags) || Flags.isProtected(otherflags)
            || (declaringType != null && declaringType.isInterface())) {
        return true;
    } else if (Flags.isPrivate(otherflags)) {
        return false;
    }

    IPackageFragment otherpack = (IPackageFragment) findParentOfKind(member, IJavaElement.PACKAGE_FRAGMENT);
    return (pack != null && pack.equals(otherpack));
}

From source file:org.eclipse.jst.jsp.ui.internal.hyperlink.JSPJavaHyperlinkDetector.java

License:Open Source License

private IHyperlink createHyperlink(IJavaElement element, IRegion region, IDocument document) {
    IHyperlink link = null;//w  w  w  .j  a v  a  2  s . c om
    if (region != null) {
        // open local variable in the JSP file...
        boolean isInTranslationCU = false;
        if (element instanceof ISourceReference) {
            IFile file = null;
            int jspOffset = 0;

            // try to locate the file in the workspace
            ITextFileBuffer textFileBuffer = FileBuffers.getTextFileBufferManager().getTextFileBuffer(document);
            if (textFileBuffer != null && textFileBuffer.getLocation() != null) {
                file = getFile(textFileBuffer.getLocation().toString());
            }

            // get Java range and translate to JSP range
            try {
                ISourceRange range = null;
                IJSPTranslation jspTranslation = getJSPTranslation(document);
                if (jspTranslation != null) {
                    // link to local variable definitions
                    if (element instanceof ILocalVariable) {
                        range = ((ILocalVariable) element).getNameRange();
                        Object cu = ((ILocalVariable) element).getAncestor(IJavaElement.COMPILATION_UNIT);
                        if (cu != null && cu.equals(jspTranslation.getCompilationUnit()))
                            isInTranslationCU = true;
                    }
                    // linking to fields of the same compilation unit
                    else if (element.getElementType() == IJavaElement.FIELD) {
                        Object cu = ((IField) element).getCompilationUnit();
                        if (cu != null && cu.equals(jspTranslation.getCompilationUnit())) {
                            range = ((ISourceReference) element).getSourceRange();
                            isInTranslationCU = true;
                        }
                    }
                    // linking to methods of the same compilation unit
                    else if (element.getElementType() == IJavaElement.METHOD) {
                        Object cu = ((IMethod) element).getCompilationUnit();
                        if (cu != null && cu.equals(jspTranslation.getCompilationUnit())) {
                            range = ((ISourceReference) element).getSourceRange();
                            isInTranslationCU = true;
                        }
                    }
                }

                if (jspTranslation != null && range != null && file != null) {
                    jspOffset = jspTranslation.getJspOffset(range.getOffset());
                    if (jspOffset >= 0) {
                        link = new WorkspaceFileHyperlink(region, file,
                                new Region(jspOffset, range.getLength()));
                    }
                }
            } catch (JavaModelException jme) {
                Logger.log(Logger.WARNING_DEBUG, jme.getMessage(), jme);
            }
        }
        if (link == null && !isInTranslationCU) { // Don't try to open the translation CU
            link = new JSPJavaHyperlink(region, element);
        }
    }
    return link;
}

From source file:org.eclipse.jst.jsp.ui.internal.java.refactoring.JSPRenameElementActionDelegate.java

License:Open Source License

public void run(IAction action) {
    IJavaElement element = getSelectedElement();
    if (element != null) {
        RenameSupport renameSupport = null;
        try {//w  ww  .j av a  2  s  .  co  m
            switch (element.getElementType()) {
            case IJavaElement.TYPE:
                renameSupport = RenameSupport.create((IType) element, element.getElementName(),
                        RenameSupport.UPDATE_REFERENCES);
                break;
            case IJavaElement.METHOD:
                renameSupport = RenameSupport.create((IMethod) element, element.getElementName(),
                        RenameSupport.UPDATE_REFERENCES);
                break;
            case IJavaElement.PACKAGE_FRAGMENT:
                renameSupport = RenameSupport.create((IPackageFragment) element, element.getElementName(),
                        RenameSupport.UPDATE_REFERENCES);
                break;
            }
            if (renameSupport != null) {
                renameSupport.openDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
                PlatformStatusLineUtil.clearStatusLine();
            }
        } catch (CoreException e) {
            Logger.logException(e);
        }
    } else {
        PlatformStatusLineUtil.displayErrorMessage(JSPUIMessages.JSPRenameElementAction_0); //$NON-NLS-1$
        PlatformStatusLineUtil.addOneTimeClearListener();
    }
}

From source file:org.eclipse.jst.jsp.ui.internal.java.refactoring.RenameElementHandler.java

License:Open Source License

public Object execute(ExecutionEvent event) throws ExecutionException {
    fEditor = HandlerUtil.getActiveEditor(event);

    IJavaElement element = getSelectedElement();
    if (element != null) {
        RenameSupport renameSupport = null;
        try {/*w  ww.ja v  a2s . c om*/
            switch (element.getElementType()) {
            case IJavaElement.TYPE:
                renameSupport = RenameSupport.create((IType) element, element.getElementName(),
                        RenameSupport.UPDATE_REFERENCES);
                break;
            case IJavaElement.METHOD:
                renameSupport = RenameSupport.create((IMethod) element, element.getElementName(),
                        RenameSupport.UPDATE_REFERENCES);
                break;
            case IJavaElement.PACKAGE_FRAGMENT:
                renameSupport = RenameSupport.create((IPackageFragment) element, element.getElementName(),
                        RenameSupport.UPDATE_REFERENCES);
                break;
            case IJavaElement.FIELD:
                renameSupport = RenameSupport.create((IField) element, element.getElementName(),
                        RenameSupport.UPDATE_REFERENCES);
                break;
            }
            if (renameSupport != null) {
                renameSupport.openDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
                PlatformStatusLineUtil.clearStatusLine();
            }
        } catch (CoreException e) {
            Logger.logException(e);
        }
    } else {
        PlatformStatusLineUtil.displayErrorMessage(JSPUIMessages.JSPRenameElementAction_0); //$NON-NLS-1$
        PlatformStatusLineUtil.addOneTimeClearListener();
    }

    return null;
}

From source file:org.eclipse.jst.jsp.ui.tests.contentassist.JSPTranslationTest.java

License:Open Source License

private void verifyTranslationHasNoSessionVariables(IFile file) throws JavaModelException {
    IDOMModel model = null;//from  w  ww  .ja va2  s .c o m
    try {
        model = (IDOMModel) getStructuredModelForRead(file);
        setupAdapterFactory(model);

        JSPTranslationAdapter adapter = (JSPTranslationAdapter) model.getDocument()
                .getAdapterFor(IJSPTranslation.class);
        ICompilationUnit cu = adapter.getJSPTranslation().getCompilationUnit();
        cu.makeConsistent(new NullProgressMonitor());
        IType[] types = cu.getAllTypes();
        for (int i = 0; i < types.length; i++) {
            IJavaElement[] members = types[i].getChildren();
            for (int k = 0; k < members.length; k++) {
                // check fields for name "session"
                if (members[k].getElementType() == IJavaElement.FIELD) {
                    assertFalse("field named \"session\" exists",
                            members[k].getElementName().equals(JSP11Namespace.ATTR_NAME_SESSION));
                }
                /*
                 * check "public void
                 * _jspService(javax.servlet.http.HttpServletRequest
                 * request, javax.servlet.http.HttpServletResponse
                 * response)" for local variables named "session"
                 */
                else if (members[k].getElementType() == IJavaElement.METHOD
                        && members[k].getElementName().startsWith("_jspService")) {
                    ICompilationUnit compilationUnit = ((IMethod) members[k]).getCompilationUnit();
                    compilationUnit.makeConsistent(new NullProgressMonitor());
                    ASTParser parser = ASTParser.newParser(AST.JLS3);
                    parser.setSource(cu);
                    ASTNode node = parser.createAST(null);
                    node.accept(new ASTVisitor() {
                        public boolean visit(VariableDeclarationStatement node) {
                            Iterator fragments = node.fragments().iterator();
                            while (fragments.hasNext()) {
                                VariableDeclarationFragment fragment = (VariableDeclarationFragment) fragments
                                        .next();
                                if (fragment.getName().getFullyQualifiedName()
                                        .equals(JSP11Namespace.ATTR_NAME_SESSION)) {
                                    String typeName = ((SimpleType) node.getType()).getName()
                                            .getFullyQualifiedName();
                                    assertFalse(
                                            "local variable of type \"javax.servlet.http.HttpSession\" and named \"session\" exists",
                                            typeName.equals("javax.servlet.http.HttpSession"));
                                }
                            }
                            return super.visit(node);
                        }
                    });
                }
            }
        }
    } finally {
        if (model != null)
            model.releaseFromRead();
    }
}

From source file:org.eclipse.linuxtools.changelog.parsers.java.JavaParser.java

License:Open Source License

/**
 * @see IParserChangeLogContrib#parseCurrentFunction(IEditorPart)
 *///ww w.j  ava  2 s.c o m
public String parseCurrentFunction(IEditorInput input, int offset) throws CoreException {

    String currentElementName;
    int elementType;

    // Get the working copy and connect to input.
    IWorkingCopyManager manager = JavaUI.getWorkingCopyManager();
    manager.connect(input);

    // Retrieve the Java Element in question.
    // The following internal access is done because the getWorkingCopy() method
    // for the WorkingCopyManager returns null for StorageEditorInput, however,
    // there is a working copy available through the ICompilationUnitDocumentProvider.
    //      ICompilationUnit workingCopy = manager.getWorkingCopy(input);
    ICompilationUnitDocumentProvider x = (ICompilationUnitDocumentProvider) JavaUI.getDocumentProvider();
    // Retrieve the Java Element in question.
    ICompilationUnit workingCopy = x.getWorkingCopy(input);

    if (workingCopy == null)
        return "";

    IJavaElement method = workingCopy.getElementAt(offset);

    manager.disconnect(input);

    // no element selected
    if (method == null)
        return "";

    // Get the current element name, to test it.
    currentElementName = method.getElementName();

    // Element doesn't have a name. Can go no further.
    if (currentElementName == null)
        return "";

    // Get the Element Type to test.
    elementType = method.getElementType();

    switch (elementType) {
    case IJavaElement.METHOD:
    case IJavaElement.FIELD:
        break;
    case IJavaElement.COMPILATION_UNIT:
        return "";
    case IJavaElement.INITIALIZER:
        return STATIC_INITIALIZER_NAME;

    // So it's not a method, field, type, or static initializer. Where are we?
    default:
        IJavaElement tmpMethodType;
        if (((tmpMethodType = method.getAncestor(IJavaElement.METHOD)) == null)
                && ((tmpMethodType = method.getAncestor(IJavaElement.TYPE)) == null)) {
            return "";
        } else {
            // In a class, but not in a method. Return class name instead.
            method = tmpMethodType;
            currentElementName = method.getElementName();
        }
    }

    // Build all ancestor classes.
    // Append all ancestor class names to string

    IJavaElement tmpParent = method.getParent();
    boolean firstLoop = true;

    while (tmpParent != null) {
        IJavaElement tmpParentClass = tmpParent.getAncestor(IJavaElement.TYPE);
        if (tmpParentClass != null) {
            String tmpParentClassName = tmpParentClass.getElementName();
            if (tmpParentClassName == null)
                return "";
            currentElementName = tmpParentClassName + "." + currentElementName;
        } else {
            // cut root class name
            int rootClassPos = currentElementName.indexOf(".");
            if (rootClassPos >= 0)
                currentElementName = currentElementName.substring(rootClassPos + 1);
            if (firstLoop)
                return "";
            else
                return currentElementName;
        }
        tmpParent = tmpParentClass.getParent();
        firstLoop = false;

    }

    return "";
}

From source file:org.eclipse.linuxtools.internal.changelog.parsers.java.JavaParser.java

License:Open Source License

@Override
public String parseCurrentFunction(IEditorInput input, int offset) throws CoreException {

    String currentElementName;//from w  w  w .j a  v a  2  s. co  m
    int elementType;

    // Get the working copy and connect to input.
    IWorkingCopyManager manager = JavaUI.getWorkingCopyManager();
    manager.connect(input);

    // Retrieve the Java Element in question.
    // The following internal access is done because the getWorkingCopy()
    // method
    // for the WorkingCopyManager returns null for StorageEditorInput,
    // however,
    // there is a working copy available through the
    // ICompilationUnitDocumentProvider.
    ICompilationUnitDocumentProvider x = (ICompilationUnitDocumentProvider) JavaUI.getDocumentProvider();
    // Retrieve the Java Element in question.
    ICompilationUnit workingCopy = x.getWorkingCopy(input);

    if (workingCopy == null) {
        return "";
    }

    IJavaElement method = workingCopy.getElementAt(offset);

    manager.disconnect(input);

    // no element selected
    if (method == null) {
        return "";
    }

    // Get the current element name, to test it.
    currentElementName = method.getElementName();

    // Element doesn't have a name. Can go no further.
    if (currentElementName == null) {
        return "";
    }

    // Get the Element Type to test.
    elementType = method.getElementType();

    switch (elementType) {
    case IJavaElement.METHOD:
    case IJavaElement.FIELD:
        break;
    case IJavaElement.COMPILATION_UNIT:
        return "";
    case IJavaElement.INITIALIZER:
        return STATIC_INITIALIZER_NAME;

    // So it's not a method, field, type, or static initializer. Where
    // are we?
    default:
        IJavaElement tmpMethodType;
        if (((tmpMethodType = method.getAncestor(IJavaElement.METHOD)) == null)
                && ((tmpMethodType = method.getAncestor(IJavaElement.TYPE)) == null)) {
            return "";
        } else {
            // In a class, but not in a method. Return class name instead.
            method = tmpMethodType;
            currentElementName = method.getElementName();
        }
    }

    // Build all ancestor classes.
    // Append all ancestor class names to string

    IJavaElement tmpParent = method.getParent();
    boolean firstLoop = true;

    while (tmpParent != null) {
        IJavaElement tmpParentClass = tmpParent.getAncestor(IJavaElement.TYPE);
        if (tmpParentClass != null) {
            String tmpParentClassName = tmpParentClass.getElementName();
            if (tmpParentClassName == null) {
                return "";
            }
            currentElementName = tmpParentClassName + "." + currentElementName;
        } else {
            // cut root class name
            int rootClassPos = currentElementName.indexOf('.');
            if (rootClassPos >= 0) {
                currentElementName = currentElementName.substring(rootClassPos + 1);
            }
            if (firstLoop) {
                return "";
            } else {
                return currentElementName;
            }
        }
        tmpParent = tmpParentClass.getParent();
        firstLoop = false;

    }

    return "";
}

From source file:org.eclipse.modisco.java.discoverer.internal.io.library.ClassFileParser.java

License:Open Source License

/**
 * Finds the type variable associated with the currently visited Java
 * element (type or method) or with it's parent.
 * /*from w  w w.j  av  a  2 s .c o  m*/
 * @param name
 *            the name of the type variable
 * @return the {@code ITypeParameter}
 */
public ITypeParameter findTypeParameter(final String name) {
    IMember member = this.currentlyVisitedJavaElement;
    ITypeParameter typeParameter = null;
    while (member != null) {
        if (member.getElementType() == IJavaElement.METHOD) {
            typeParameter = ((IMethod) member).getTypeParameter(name);
        } else {
            typeParameter = ((IType) member).getTypeParameter(name);
        }
        if (typeParameter.exists()) {
            break;
        }
        member = member.getDeclaringType();
    }
    return typeParameter;
}

From source file:org.eclipse.modisco.java.discoverer.internal.io.library.ClassFileParser.java

License:Open Source License

/**
 * Complete the MoDisco modifier with the informations of the flags.
 * /*from  w w  w  . jav a 2  s. co m*/
 * @param flags
 *            the flags
 * @param modiscoModifier
 *            the MoDisco Modifier
 * @see Flags
 */
private static void manageModifier(final Modifier modiscoModifier, final int flags,
        final IJavaElement element) {
    int kind = element.getElementType();
    // static is applicable on types, methods, fields, and initializers.
    if (!modiscoModifier.isStatic()) {
        if (kind == IJavaElement.TYPE || kind == IJavaElement.METHOD || kind == IJavaElement.FIELD) {
            modiscoModifier.setStatic(Flags.isStatic(flags));
        }
    }
    // native is applicable to methods
    if (!modiscoModifier.isNative()) {
        if (kind == IJavaElement.METHOD) {
            modiscoModifier.setNative(Flags.isNative(flags));
        }
    }
    // strictfp is applicable to types and methods
    if (!modiscoModifier.isStrictfp()) {
        if (kind == IJavaElement.TYPE || kind == IJavaElement.METHOD) {
            modiscoModifier.setStrictfp(Flags.isStrictfp(flags));
        }
    }
    // synchronized is applicable only to methods
    if (!modiscoModifier.isSynchronized()) {
        if (kind == IJavaElement.METHOD) {
            modiscoModifier.setSynchronized(Flags.isSynchronized(flags));
        }
    }
    // transient is applicable only to fields
    if (!modiscoModifier.isTransient()) {
        if (kind == IJavaElement.FIELD) {
            modiscoModifier.setTransient(Flags.isTransient(flags));
        }
    }
    // volatile is applicable only to fields
    if (!modiscoModifier.isVolatile()) {
        if (kind == IJavaElement.FIELD) {
            modiscoModifier.setVolatile(Flags.isVolatile(flags));
        }
    }

    // visibility modifiers are applicable to types, methods, constructors,
    // and fields.
    if (kind == IJavaElement.TYPE || kind == IJavaElement.METHOD || kind == IJavaElement.FIELD) {
        if (Flags.isPrivate(flags)) {
            modiscoModifier.setVisibility(VisibilityKind.PRIVATE);
        } else if (Flags.isProtected(flags)) {
            modiscoModifier.setVisibility(VisibilityKind.PROTECTED);
        } else if (Flags.isPublic(flags)) {
            modiscoModifier.setVisibility(VisibilityKind.PUBLIC);
        }
    }

    // abstract is applicable to types and methods
    // final is applicable to types, methods and variables
    if (kind == IJavaElement.TYPE || kind == IJavaElement.METHOD) {
        if (Flags.isAbstract(flags)) {
            modiscoModifier.setInheritance(InheritanceKind.ABSTRACT);
        } else if (Flags.isFinal(flags)) {
            modiscoModifier.setInheritance(InheritanceKind.FINAL);
        }
    }
}