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

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

Introduction

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

Prototype

String getElementName();

Source Link

Document

Returns the name of this element.

Usage

From source file:org.codehaus.groovy.eclipse.editor.outline.GroovyScriptOutlineExtender.java

License:Apache License

@Override
public IJavaElement[] refreshChildren() {
    ModuleNode node = (ModuleNode) getNode();
    ClassNode scriptClassDummy;//from ww w  . j a  va2  s .  co  m
    String scriptName;
    if (node != null) {
        scriptClassDummy = node.getScriptClassDummy();
        if (scriptClassDummy == null) {
            if (node.getClasses().size() > 0) {
                scriptClassDummy = node.getClasses().get(0);
            }
        }
        if (scriptClassDummy == null) {
            scriptName = "Problem";
        } else {
            scriptName = scriptClassDummy.getNameWithoutPackage();
        }
    } else {
        scriptName = null;
        scriptClassDummy = null;
    }
    if (node == null || node.encounteredUnrecoverableError() || scriptClassDummy == null) {
        // we have no idea what the structure is.
        // Let the user know
        return new IJavaElement[] {
                new OType(getUnit(), node, scriptName + GroovyScriptOutlineExtender.NO_STRUCTURE_FOUND) };
    }

    // otherwise, add all children directly except for the script class
    try {
        IJavaElement[] children = getUnit().getChildren();
        final IType scriptType;
        final List<IJavaElement> fakeChildren = new ArrayList<IJavaElement>();
        IType candidate = null;
        for (IJavaElement elt : children) {
            if (elt.getElementName().equals(scriptName)) {
                candidate = (IType) elt;
            } else if (elt instanceof IJavaElement) {
                fakeChildren.add((IJavaElement) elt);
            }
        }
        scriptType = candidate;

        if (scriptType != null) {
            // do not add the script type directly. Rather, add all of the
            // children
            // Additionally, do not add the run or main methods
            IJavaElement[] scriptChildren = scriptType.getChildren();
            for (IJavaElement scriptElt : scriptChildren) {
                if (scriptElt instanceof IMember) {
                    if (isRunMethod(scriptElt) || isMainMethod(scriptElt) || isConstructor(scriptElt)) {
                        continue;
                    }
                    fakeChildren.add((IMember) scriptElt);
                }
            }

            // next add all of the variable declarations
            BlockStatement block = node.getStatementBlock();
            ClassCodeVisitorSupport visitor = new ClassCodeVisitorSupport() {
                @Override
                protected SourceUnit getSourceUnit() {
                    return null;
                }

                @Override
                public void visitClosureExpression(ClosureExpression expression) {
                }

                @Override
                public void visitDeclarationExpression(DeclarationExpression expression) {
                    fakeChildren.add(new GroovyScriptVariable((JavaElement) scriptType, expression));
                    super.visitDeclarationExpression(expression);
                }
            };
            visitor.visitBlockStatement(block);
        }

        // finally, sort all the elements by source location
        IJavaElement[] fakeChildrenArr = fakeChildren.toArray(new IJavaElement[fakeChildren.size()]);
        sort(fakeChildrenArr);
        return fakeChildrenArr;

    } catch (JavaModelException e) {
        GroovyCore.logException("Encountered exception when calculating children", e);
        return new IJavaElement[] {
                new OType(getUnit(), node, scriptName + " -- Encountered exception.  See log.") };
    }
}

From source file:org.codehaus.groovy.eclipse.editor.outline.GroovyScriptOutlineExtender.java

License:Apache License

private boolean isRunMethod(IJavaElement scriptElt) {
    if (scriptElt.getElementType() != IJavaElement.METHOD) {
        return false;
    }/*w w  w .ja v  a 2 s  .  c om*/
    if (!scriptElt.getElementName().equals("run")) {
        return false;
    }
    String[] parammeterTypes = ((IMethod) scriptElt).getParameterTypes();
    return parammeterTypes == null || parammeterTypes.length == 0;
}

From source file:org.codehaus.groovy.eclipse.refactoring.core.convert.ConvertToClosureRefactoring.java

License:Apache License

private IMethod findMethod(GroovyCompilationUnit unit, int offset) {
    if (unit.isOnBuildPath()) {
        return null;
    }/*from w  w w .j  av  a  2s  .  c  o m*/
    try {
        IJavaElement maybeMethod = unit.getElementAt(offset);

        if (!(maybeMethod instanceof IMethod)) {
            return null;
        }

        ISourceRange nameRange = ((IMethod) maybeMethod).getNameRange();
        if (nameRange.getLength() == maybeMethod.getElementName().length()) {
            return ((IMethod) maybeMethod);
        }

        // For quoted method names, the name range will include the quotes,
        // but
        // the name itself will not include them
        // check the text to see if the name start is at a quote
        char[] contents = unit.getContents();
        if (contents.length > nameRange.getOffset() && contents[nameRange.getOffset()] == '"') {
            return ((IMethod) maybeMethod);
        }
    } catch (JavaModelException e) {
        GroovyCore.logException("Error finding enclosing method for refactoring", e);
    }
    return null;
}

From source file:org.codehaus.groovy.eclipse.refactoring.core.extract.InferParameterAndReturnTypesRequestor.java

License:Apache License

/**
 * @param enclosingElement//from w  ww  .  j  av a2 s .  co m
 * @return true iff enclosingElement's source location contains the source
 *         location of {@link #selectedText}
 */
private boolean interestingElement(IJavaElement enclosingElement) {
    // the clinit is always interesting since the clinit contains static
    // initializers
    if (enclosingElement.getElementName().equals("<clinit>")) {
        return true;
    }

    if (enclosingElement instanceof ISourceReference) {
        try {
            ISourceRange range = ((ISourceReference) enclosingElement).getSourceRange();
            return range.getOffset() <= selectedText.getOffset()
                    && range.getOffset() + range.getLength() >= selectedText.getEnd();
        } catch (JavaModelException e) {
            Util.log(e);
        }
    }
    return false;
}

From source file:org.codehaus.groovy.eclipse.refactoring.core.rename.renameLocal.LocalVariableNameCheckerRequestor.java

License:Apache License

/**
 * @param enclosingElement/*from  ww  w . j a  va 2  s . c  om*/
 * @return true iff enclosingElement's source location contains the source
 *         location of {@link #variable}
 */
private boolean interestingElement(IJavaElement enclosingElement) {
    // the clinit is always interesting since the clinit contains static
    // initializers
    if (enclosingElement.getElementName().equals("<clinit>")) {
        return true;
    }

    if (start >= 0 && end >= 0 && enclosingElement instanceof ISourceReference) {
        try {
            ISourceRange range = ((ISourceReference) enclosingElement).getSourceRange();
            return range.getOffset() <= start && range.getOffset() + range.getLength() >= end;
        } catch (JavaModelException e) {
            Util.log(e);
        }
    }
    return false;
}

From source file:org.codehaus.groovy.eclipse.refactoring.test.rename.RenameTypeTests.java

License:Open Source License

private void checkMappers(Refactoring refactoring, IType type, String newCUName,
        IJavaElement[] someClassMembers) {
    RenameTypeProcessor rtp = (RenameTypeProcessor) ((RenameRefactoring) refactoring).getProcessor();

    ICompilationUnit newUnit = (ICompilationUnit) rtp.getRefactoredJavaElement(type.getCompilationUnit());
    assertTrue(newUnit.exists());//  w  w w. ja v  a  2 s . c  om
    assertTrue(newUnit.getElementName().equals(newCUName));

    IFile newFile = (IFile) rtp.getRefactoredResource(type.getResource());
    assertTrue(newFile.exists());
    assertTrue(newFile.getName().equals(newCUName));

    if ((type.getParent().getElementType() == IJavaElement.COMPILATION_UNIT)
            && type.getCompilationUnit().getElementName().equals(type.getElementName() + ".groovy")) {
        assertFalse(type.getCompilationUnit().exists());
        assertFalse(type.getResource().exists());
    }

    IPackageFragment oldPackage = (IPackageFragment) type.getCompilationUnit().getParent();
    IPackageFragment newPackage = (IPackageFragment) rtp.getRefactoredJavaElement(oldPackage);
    assertEquals(oldPackage, newPackage);

    for (int i = 0; i < someClassMembers.length; i++) {
        IMember member = (IMember) someClassMembers[i];
        IJavaElement refactoredMember = rtp.getRefactoredJavaElement(member);
        if (member instanceof IMethod && member.getElementName().equals(type.getElementName()))
            continue; // constructor
        assertTrue(refactoredMember.exists());
        assertEquals(member.getElementName(), refactoredMember.getElementName());
        assertFalse(refactoredMember.equals(member));
    }
}

From source file:org.codehaus.groovy.eclipse.refactoring.test.TestRenameParticipantShared.java

License:Open Source License

public boolean initialize(Object element) {
    fgInstance = this;
    fElements.add(element);/*from   w w w .j  av a  2 s . c om*/
    fArguments.add(getArguments());
    if (element instanceof IJavaElement)
        fHandles.add(((IJavaElement) element).getHandleIdentifier());
    else
        fHandles.add(((IResource) element).getFullPath().toString());

    IJavaElementMapper updating = (IJavaElementMapper) getProcessor().getAdapter(IJavaElementMapper.class);
    if ((updating != null) && getArguments() instanceof RenameTypeArguments) {
        RenameTypeArguments arguments = (RenameTypeArguments) getArguments();
        if (arguments.getUpdateSimilarDeclarations()) {
            IJavaElement[] elements = arguments.getSimilarDeclarations();
            for (int i = 0; i < elements.length; i++) {
                IJavaElement updated = updating.getRefactoredJavaElement(elements[i]);
                if (updated != null) {
                    fSimilarToHandle.put(elements[i].getHandleIdentifier(), getKey(updated));
                    fSimilarToNewName.put(elements[i].getHandleIdentifier(), updated.getElementName());
                }
            }
        }
    }

    return true;
}

From source file:org.codehaus.groovy.eclipse.test.ui.OutlineExtenderTests.java

License:Apache License

/**
 * check that the element is the appropriate field
 * @param element/* www . j av a2 s  .c om*/
 * @param name
 * @param signature
 * @throws JavaModelException
 */
private void assertIsField(IJavaElement element, String name, String typeSignature) throws JavaModelException {
    assertTrue("Element" + element.getElementName() + " in not an instanceof OMethod",
            element instanceof OField);

    OField field = (OField) element;
    assertTrue("Field name is '" + field.getElementName() + "' instead of '" + name + "'",
            name.equals(field.getElementName()));
    assertTrue("Field type signature is '" + field.getTypeSignature() + "' instead of '" + name + "'",
            typeSignature.equals(field.getTypeSignature()));
}

From source file:org.codehaus.groovy.eclipse.test.ui.OutlineExtenderTests.java

License:Apache License

/**
 * check that the element is the appropriate method
 * @param element//from  w ww. j a  v a  2 s .c om
 * @param name
 * @param returnType
 * @throws JavaModelException
 */
private void assertIsMethod(IJavaElement element, String name, String returnType) throws JavaModelException {
    assertTrue("Element " + element.getElementName() + " in not an instanceof OMethod",
            element instanceof OMethod);

    OMethod method = (OMethod) element;
    assertTrue("Method name is '" + method.getElementName() + "' instead of '" + name + "'",
            name.equals(method.getElementName()));
    if (returnType != null) {
        assertTrue("Method return type is '" + method.getReturnTypeName() + "' instead of '" + returnType + "'",
                returnType.equals(method.getReturnTypeName()));
    }
}

From source file:org.codehaus.groovy.eclipse.test.ui.OutlineExtenderTests.java

License:Apache License

/**
 * check that the element is the appropriate type
 * @param element//from w w  w .  j a va  2 s .  c o  m
 * @param name
 * @param returnType
 * @throws JavaModelException
 */
private void assertIsType(IJavaElement element, String name) throws JavaModelException {
    assertTrue("Element " + element.getElementName() + " in not an instanceof OType", element instanceof OType);

    OType type = (OType) element;
    assertTrue("Type name is '" + type.getElementName() + "' instead of '" + name + "'",
            name.equals(type.getElementName()));
}