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.codeassist.tests.ExtendedCompletionContextTests.java

License:Apache License

public void testExtendedContextWithBoxing() throws Exception {
    String contents = "int x\n" + "Integer y\n" + "boolean a\n" + "Boolean b\n" + "z";
    GroovyExtendedCompletionContext context = getExtendedCoreContext(create(contents),
            contents.lastIndexOf('z') + 1);
    IJavaElement enclosing = context.getEnclosingElement();
    assertEquals("run", enclosing.getElementName());
    assertExtendedContextElements(context, "Ljava.lang.Integer;", "y", "x");
    assertExtendedContextElements(context, "I", "y", "x");
    assertExtendedContextElements(context, "Ljava.lang.Boolean;", "a", "b");
    assertExtendedContextElements(context, "Z", "a", "b");
}

From source file:org.codehaus.groovy.eclipse.codeassist.tests.ExtendedCompletionContextTests.java

License:Apache License

public void testExtendedContextWithBoxingAndArrays() throws Exception {
    String contents = "int x\n" + "Integer y\n" + "boolean a\n" + "Boolean b\n" + "int[] x1\n"
            + "Integer[] y1\n" + "boolean[] a1\n" + "Boolean[] b1\n" + "int[][] x2\n" + "Integer[][] y2\n"
            + "boolean[][] a2\n" + "Boolean[][] b2\n" + "z";
    GroovyExtendedCompletionContext context = getExtendedCoreContext(create(contents),
            contents.lastIndexOf('z') + 1);
    IJavaElement enclosing = context.getEnclosingElement();
    assertEquals("run", enclosing.getElementName());
    assertExtendedContextElements(context, "Ljava.lang.Integer;", "y", "x");
    assertExtendedContextElements(context, "I", "y", "x");
    assertExtendedContextElements(context, "Ljava.lang.Boolean;", "a", "b");
    assertExtendedContextElements(context, "Z", "a", "b");
    assertExtendedContextElements(context, "[Ljava.lang.Integer;", "y1", "x1");
    assertExtendedContextElements(context, "[I", "y1", "x1");
    assertExtendedContextElements(context, "[Ljava.lang.Boolean;", "a1", "b1");
    assertExtendedContextElements(context, "[Z", "a1", "b1");
    assertExtendedContextElements(context, "[[Ljava.lang.Integer;", "y2", "x2");
    assertExtendedContextElements(context, "[[I", "y2", "x2");
    assertExtendedContextElements(context, "[[Ljava.lang.Boolean;", "a2", "b2");
    assertExtendedContextElements(context, "[[Z", "a2", "b2");
}

From source file:org.codehaus.groovy.eclipse.codebrowsing.requestor.CodeSelectRequestor.java

License:Apache License

/**
 * @param enclosingElement/*from www . j a  v  a2 s  .  c o  m*/
 * @return true iff enclosingElement's source location contains the source location of {@link #nodeToLookFor} 
 */
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() <= nodeToLookFor.getStart()
                    && range.getOffset() + range.getLength() >= nodeToLookFor.getEnd();
        } catch (JavaModelException e) {
            Util.log(e);
        }
    }
    return false;
}

From source file:org.codehaus.groovy.eclipse.codebrowsing.requestor.CodeSelectRequestor.java

License:Apache License

/**
 * @param result//from  w w w .  jav a  2  s. c  om
 * @param type
 * @throws JavaModelException
 */
private IJavaElement findRequestedElement(TypeLookupResult result, IType type) throws JavaModelException {
    IJavaElement maybeRequested = null;
    ASTNode declaration = result.declaration;
    if (declaration instanceof ClassNode) {
        maybeRequested = type;
    } else if (type.getTypeRoot() != null) {
        if (declaration.getEnd() > 0) {
            // GRECLIPSE-1233 can't use getEltAt because of default parameters.
            // instead, just iterate through children.  Method variants 
            // are always after the original method
            IJavaElement[] children = type.getChildren();
            int start = declaration.getStart();
            int end = declaration.getEnd();
            String name;
            if (declaration instanceof MethodNode) {
                name = ((MethodNode) declaration).getName();
            } else if (declaration instanceof FieldNode) {
                name = ((FieldNode) declaration).getName();
            } else if (declaration instanceof PropertyNode) {
                name = ((PropertyNode) declaration).getName();
            } else {
                name = declaration.getText();
            }
            for (IJavaElement child : children) {
                ISourceRange range = ((ISourceReference) child).getSourceRange();
                if (range.getOffset() <= start && range.getOffset() + range.getLength() >= end
                        && child.getElementName().equals(name)) {
                    maybeRequested = child;
                    break;
                } else if (start + end < range.getOffset()) {
                    // since children are listed incrementally no need to go further
                    break;
                }
            }
        }
        if (maybeRequested == null) {
            // try something else because source location not set right
            String name = null;
            int preferredParamNumber = -1;
            if (declaration instanceof MethodNode) {
                name = ((MethodNode) declaration).getName();
                Parameter[] parameters = ((MethodNode) declaration).getParameters();
                preferredParamNumber = parameters == null ? 0 : parameters.length;
            } else if (declaration instanceof PropertyNode) {
                name = ((PropertyNode) declaration).getName();
            } else if (declaration instanceof FieldNode) {
                name = ((FieldNode) declaration).getName();
            }
            if (name != null) {
                maybeRequested = findElement(type, name, preferredParamNumber);
            }
            if (maybeRequested == null) {
                // still couldn't find anything
                maybeRequested = type;
            }
        }

    }
    return maybeRequested;
}

From source file:org.codehaus.groovy.eclipse.codebrowsing.requestor.CodeSelectRequestor.java

License:Apache License

/**
 * Converts the maybeRequested element into a resolved element by creating a unique key for it.
 *///from w ww  . ja v  a 2s  .  c  om
private IJavaElement resolveRequestedElement(TypeLookupResult result, IJavaElement maybeRequested) {
    AnnotatedNode declaration = (AnnotatedNode) result.declaration;
    if (declaration instanceof PropertyNode && maybeRequested instanceof IMethod) {
        // the field associated with this property does not exist, use the method instead
        String getterName = maybeRequested.getElementName();
        MethodNode maybeDeclaration = (MethodNode) declaration.getDeclaringClass().getMethods(getterName)
                .get(0);
        declaration = maybeDeclaration == null ? declaration : maybeDeclaration;
    }

    String uniqueKey = createUniqueKey(declaration, result.type, result.declaringType, maybeRequested);
    IJavaElement candidate;

    // Create the Groovy Resolved Element, which is like a resolved element, but contains extraDoc, as
    // well as the inferred declaration (which may not be the same as the actual declaration)
    switch (maybeRequested.getElementType()) {
    case IJavaElement.FIELD:
        if (maybeRequested.isReadOnly()) {
            candidate = new GroovyResolvedBinaryField((JavaElement) maybeRequested.getParent(),
                    maybeRequested.getElementName(), uniqueKey, result.extraDoc, result.declaration);
        } else {
            candidate = new GroovyResolvedSourceField((JavaElement) maybeRequested.getParent(),
                    maybeRequested.getElementName(), uniqueKey, result.extraDoc, result.declaration);
        }
        break;
    case IJavaElement.METHOD:
        if (maybeRequested.isReadOnly()) {
            candidate = new GroovyResolvedBinaryMethod((JavaElement) maybeRequested.getParent(),
                    maybeRequested.getElementName(), ((IMethod) maybeRequested).getParameterTypes(), uniqueKey,
                    result.extraDoc, result.declaration);
        } else {
            candidate = new GroovyResolvedSourceMethod((JavaElement) maybeRequested.getParent(),
                    maybeRequested.getElementName(), ((IMethod) maybeRequested).getParameterTypes(), uniqueKey,
                    result.extraDoc, result.declaration);
        }
        break;
    case IJavaElement.TYPE:
        if (maybeRequested.isReadOnly()) {
            candidate = new GroovyResolvedBinaryType((JavaElement) maybeRequested.getParent(),
                    maybeRequested.getElementName(), uniqueKey, result.extraDoc, result.declaration);
        } else {
            candidate = new GroovyResolvedSourceType((JavaElement) maybeRequested.getParent(),
                    maybeRequested.getElementName(), uniqueKey, result.extraDoc, result.declaration);
        }
        break;
    default:
        candidate = maybeRequested;
    }
    requestedElement = candidate;
    return requestedElement;
}

From source file:org.codehaus.groovy.eclipse.core.search.SyntheticAccessorSearchRequestor.java

License:Apache License

private IMethod findSyntheticMember(IJavaElement element, String prefix) throws JavaModelException {
    if (element.getElementType() != IJavaElement.FIELD) {
        return null;
    }/*from w  w w . ja  va  2  s.  c  o  m*/
    IType parent = (IType) element.getParent();
    String[] sigs;
    String[] names;
    if (prefix.equals("set")) {
        sigs = new String[] { ((IField) element).getTypeSignature() };
        names = new String[] { element.getElementName() };
    } else {
        sigs = new String[0];
        names = new String[0];
    }
    MethodWrapper method = new MethodWrapper(
            parent.getMethod(convertName(prefix, element.getElementName()), sigs), names);
    // only return if method doesn't exist since otherwise, this method would not be synthetic
    return method.reallyExists() ? null : method;
}

From source file:org.codehaus.groovy.eclipse.core.search.SyntheticAccessorSearchRequestor.java

License:Apache License

private IField findSyntheticProperty(IJavaElement element) throws JavaModelException {
    if (element.getElementType() != IJavaElement.METHOD) {
        return null;
    }//from w  ww. j a  va  2  s.  co m
    String name = element.getElementName();
    if (name.length() <= 2) {
        return null;
    }
    int prefixLength;
    if (name.startsWith("is")) {
        prefixLength = 2;
    } else {
        if (name.length() == 3) {
            return null;
        }
        prefixLength = 3;
    }

    String fieldName = "" + Character.toLowerCase(name.charAt(prefixLength)) + name.substring(prefixLength + 1);
    IType parent = (IType) element.getParent();
    IField field = parent.getField(fieldName);
    // only return if field doesn't exist since otherwise, this method would
    // not be synthetic
    return field.exists() && Flags.isProtected(field.getFlags()) ? null : field;
}

From source file:org.codehaus.groovy.eclipse.core.search.SyntheticMemberSearchTests.java

License:Apache License

private IJavaElement findSearchTarget(String name) throws JavaModelException {
    for (IJavaElement child : gType.getChildren()) {
        if (child.getElementName().equals(name)) {
            return child;
        }//from   w  w  w  .  j  a v  a 2  s . c o m
    }
    fail("child not found: " + name);
    return null;
}

From source file:org.codehaus.groovy.eclipse.dsl.inferencing.suggestions.SuggestionsRequestor.java

License:Apache License

protected boolean interestingElement(IJavaElement enclosingElement) {
    // the clinit is always interesting since the clinit contains static
    // initializers
    if (enclosingElement.getElementName().equals("<clinit>")) {
        return true;
    }//from  w w w .  j a va2 s  .  c  om

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

From source file:org.codehaus.groovy.eclipse.dsl.tests.BuiltInDSLInferencingTests.java

License:Open Source License

public void testSanity() throws Exception {
    IJavaProject javaProject = JavaCore.create(project);
    assertTrue("Should have DSL support classpath container",
            GroovyRuntime.hasClasspathContainer(javaProject, GroovyDSLCoreActivator.CLASSPATH_CONTAINER_ID));

    IClasspathContainer container = JavaCore
            .getClasspathContainer(GroovyDSLCoreActivator.CLASSPATH_CONTAINER_ID, javaProject);
    IClasspathEntry[] cpes = container.getClasspathEntries();
    assertEquals("Wrong number of classpath entries found: " + Arrays.toString(cpes), 2, cpes.length);

    IClasspathEntry pluginEntry = null;//from   www  .  j  av a  2 s  . c o  m
    IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
    for (IClasspathEntry entry : entries) {
        if (entry.getPath().toString().contains("plugin_dsld")) {
            pluginEntry = entry;
        }
    }

    IPackageFragmentRoot root = null;
    List<String> elements = new ArrayList<String>();
    for (IJavaElement elt : javaProject.getChildren()) {
        elements.add(elt.getElementName());
        if (elt.getElementName().contains("plugin_dsld")) {
            root = (IPackageFragmentRoot) elt;
        }
    }

    List<String> possibleFrags = new ArrayList<String>();
    for (IPackageFragment frag : javaProject.getPackageFragments()) {
        if (frag.getElementName().equals("dsld")) {
            possibleFrags.add(frag.toString());
            possibleFrags.add("  [");
            for (IJavaElement child : frag.getChildren()) {
                possibleFrags.add("    " + child.getElementName());
            }
            possibleFrags.add("  ]");
        }
    }

    assertNotNull("Did not find the Plugin DSLD classpath entry.  Exsting resolved roots:\n"
            + printList(elements) + "\nOther DSLD fragments:\n" + printList(possibleFrags), pluginEntry);
    assertNotNull("Plugin DSLD classpath entry should exist.  Exsting resolved roots:\n" + printList(elements)
            + "\nOther DSLD fragments:\n" + printList(possibleFrags), root);
    assertTrue("Plugin DSLD classpath entry should exist", root.exists());

    ExternalPackageFragmentRoot ext = (ExternalPackageFragmentRoot) root;
    ext.resource().refreshLocal(IResource.DEPTH_INFINITE, null);
    root.close();
    root.open(null);

    IPackageFragment frag = root.getPackageFragment("dsld");
    assertTrue("DSLD package fragment should exist", frag.exists());
}