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

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

Introduction

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

Prototype

IJavaElement getAncestor(int ancestorType);

Source Link

Document

Returns this Java element or the first ancestor of this element that has the given type.

Usage

From source file:at.bestsolution.efxclipse.tooling.jdt.ui.internal.FXBeanJavaCompletionProposalComputer.java

License:Open Source License

@Override
public List<ICompletionProposal> computeCompletionProposals(ContentAssistInvocationContext context,
        IProgressMonitor monitor) {//from  w  ww. j a v a2 s.  c om
    if (context instanceof JavaContentAssistInvocationContext && false) {
        JavaContentAssistInvocationContext javaContext = (JavaContentAssistInvocationContext) context;
        CompletionContext completionContext = javaContext.getCoreContext();
        IJavaElement enclosingElement = null;
        if (completionContext.isExtended()) {
            enclosingElement = completionContext.getEnclosingElement();
        } else {
            try {
                enclosingElement = javaContext.getCompilationUnit()
                        .getElementAt(context.getInvocationOffset() + 1);
            } catch (JavaModelException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        List<ICompletionProposal> l = new ArrayList<ICompletionProposal>();

        if (enclosingElement != null) {
            IType type = (IType) enclosingElement.getAncestor(IJavaElement.TYPE);
            if (type == null) {
                return l;
            }

            try {
                IField[] fields = type.getFields();
                IMethod[] methods = type.getMethods();
                int offset = context.getInvocationOffset() - 3;
                if (offset > 0) {
                    String prefix = context.getDocument().get(offset, 3);
                    IType propType = type.getJavaProject().findType("javafx.beans.property.Property");
                    IType writableType = type.getJavaProject().findType("javafx.beans.value.WritableValue");

                    // Primitives
                    IType booleanType = type.getJavaProject()
                            .findType("javafx.beans.property.ReadOnlyBooleanProperty");
                    IType doubleType = type.getJavaProject()
                            .findType("javafx.beans.property.ReadOnlyDoubleProperty");
                    IType floatType = type.getJavaProject()
                            .findType("javafx.beans.property.ReadOnlyFloatProperty");
                    IType intType = type.getJavaProject()
                            .findType("javafx.beans.property.ReadOnlyIntegerProperty");
                    IType longType = type.getJavaProject()
                            .findType("javafx.beans.property.ReadOnlyLongProperty");
                    IType stringType = type.getJavaProject()
                            .findType("javafx.beans.property.ReadOnlyStringProperty");

                    for (int i = 0; i < fields.length; i++) {
                        IField curr = fields[i];
                        if (!Flags.isEnum(curr.getFlags())) {
                            IType fieldType = toType(type, curr.getTypeSignature());
                            if (fieldType != null) {
                                if (assignable(fieldType, propType)) {
                                    if ("set".equals(prefix)) {
                                        if (assignable(fieldType, writableType)) {
                                            String setterName = NamingConventions.suggestSetterName(
                                                    type.getJavaProject(), curr.getElementName(),
                                                    curr.getFlags(), false, null);
                                            if (!hasMethod(methods, setterName)) {
                                                StyledString s = new StyledString(setterName
                                                        + "(" + toValue(fieldType, booleanType, doubleType,
                                                                floatType, intType, longType, stringType)
                                                        + ") : void");
                                                s.append(" - Setter for '" + curr.getElementName() + "'",
                                                        StyledString.QUALIFIER_STYLER);
                                                l.add(new CompletionProposalImpl(setterName, s));
                                            }
                                        }
                                    } else if (Character.isWhitespace(prefix.charAt(0))
                                            && prefix.endsWith("is")) {
                                        if (assignable(fieldType, booleanType)) {
                                            String getterName = NamingConventions.suggestGetterName(
                                                    type.getJavaProject(), curr.getElementName(),
                                                    curr.getFlags(), false, null);
                                            getterName = "is" + getterName.substring(3);
                                            if (!hasMethod(methods, getterName)) {
                                                StyledString s = new StyledString(getterName + "() : boolean");
                                                s.append(" - Getter for '" + curr.getElementName() + "'",
                                                        StyledString.QUALIFIER_STYLER);
                                                l.add(new CompletionProposalImpl(getterName, s));
                                            }
                                        }
                                    } else if ("get".equals(prefix)) {
                                        if (!assignable(fieldType, booleanType)) {
                                            String getterName = NamingConventions.suggestGetterName(
                                                    type.getJavaProject(), curr.getElementName(),
                                                    curr.getFlags(), false, null);
                                            if (!hasMethod(methods, getterName)) {
                                                StyledString s = new StyledString(getterName + "() : "
                                                        + toValue(fieldType, booleanType, doubleType, floatType,
                                                                intType, longType, stringType));
                                                s.append(" - Getter for '" + curr.getElementName() + "'",
                                                        StyledString.QUALIFIER_STYLER);
                                                l.add(new CompletionProposalImpl(getterName, s));
                                            }
                                        }
                                    } else if (Character.isWhitespace(prefix.charAt(2))) {
                                        String propertyName = curr.getElementName() + "Property";
                                        if (!hasMethod(methods, propertyName)) {
                                            StyledString s = new StyledString(propertyName);
                                            l.add(new CompletionProposalImpl(propertyName, s));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

            } catch (JavaModelException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (BadLocationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return l;
    }
    return Collections.emptyList();
}

From source file:at.bestsolution.efxclipse.tooling.ui.wizards.AbstractNewJDTElementWizard.java

License:Open Source License

protected IPackageFragmentRoot getFragmentRoot(IJavaElement elem) {
    IPackageFragmentRoot initRoot = null;
    if (elem != null) {
        initRoot = (IPackageFragmentRoot) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
        try {/* w w  w.j a v  a  2s.co m*/
            if (initRoot == null || initRoot.getKind() != IPackageFragmentRoot.K_SOURCE) {
                IJavaProject jproject = elem.getJavaProject();
                if (jproject != null) {
                    initRoot = null;
                    if (jproject.exists()) {
                        IPackageFragmentRoot[] roots = jproject.getPackageFragmentRoots();
                        for (int i = 0; i < roots.length; i++) {
                            if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) {
                                initRoot = roots[i];
                                break;
                            }
                        }
                    }
                    if (initRoot == null) {
                        initRoot = jproject.getPackageFragmentRoot(jproject.getResource());
                    }
                }
            }
        } catch (JavaModelException e) {
            // TODO
            e.printStackTrace();
        }
    }
    return initRoot;
}

From source file:at.bestsolution.fxide.jdt.corext.util.JavaModelUtil.java

License:Open Source License

/**
 * Returns the package fragment root of <code>IJavaElement</code>. If the given
 * element is already a package fragment root, the element itself is returned.
 * @param element the element//from ww  w.  jav  a  2s.  c  o m
 * @return the package fragment root of the element or <code>null</code>
 */
public static IPackageFragmentRoot getPackageFragmentRoot(IJavaElement element) {
    return (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
}

From source file:bndtools.internal.pkgselection.SearchUtils.java

License:Open Source License

/**
 * @param match/*ww  w.  j  av  a  2 s.  com*/
 * @return the enclosing {@link ICompilationUnit} of the given match, or null iff none
 */
public static ICompilationUnit getCompilationUnit(SearchMatch match) {
    IJavaElement enclosingElement = getEnclosingJavaElement(match);
    if (enclosingElement != null) {
        if (enclosingElement instanceof ICompilationUnit)
            return (ICompilationUnit) enclosingElement;
        ICompilationUnit cu = (ICompilationUnit) enclosingElement.getAncestor(IJavaElement.COMPILATION_UNIT);
        if (cu != null)
            return cu;
    }

    IJavaElement jElement = JavaCore.create(match.getResource());
    if (jElement != null && jElement.exists() && jElement.getElementType() == IJavaElement.COMPILATION_UNIT)
        return (ICompilationUnit) jElement;
    return null;
}

From source file:bndtools.internal.testcaseselection.JavaSearchScopeTestCaseLister.java

License:Open Source License

@Override
public String[] getTestCases(boolean includeNonSource, ITestCaseFilter filter) throws TestCaseListException {
    final List<IJavaElement> testCaseList = new LinkedList<IJavaElement>();

    search(Arrays.asList("junit.framework.TestCase", "junit.framework.TestSuite"), testCaseList); //$NON-NLS-1$ //$NON-NLS-2$

    // Remove non-source and excludes
    Set<String> testCaseNames = new LinkedHashSet<String>();
    for (Iterator<IJavaElement> iter = testCaseList.iterator(); iter.hasNext();) {
        boolean omit = false;
        IJavaElement element = iter.next();
        try {//w ww .  j  a v a  2  s  .c  o m

            IType type = (IType) element.getAncestor(IJavaElement.TYPE);
            if (Flags.isAbstract(type.getFlags())) {
                omit = true;
            }

            if (!includeNonSource) {
                IPackageFragment pkgFragment = (IPackageFragment) element
                        .getAncestor(IJavaElement.PACKAGE_FRAGMENT);
                if (pkgFragment.getCompilationUnits().length == 0) {
                    omit = true;
                }
            }

        } catch (JavaModelException e) {
            throw new TestCaseListException(e);
        }
        String className = getClassName(element);
        if (filter != null && !filter.select(className)) {
            omit = true;
        }
        if (!omit) {
            testCaseNames.add(className);
        }
    }

    return testCaseNames.toArray(new String[0]);
}

From source file:bndtools.internal.testcaseselection.JavaSearchScopeTestCaseLister.java

License:Open Source License

private static String getClassName(IJavaElement element) {

    IPackageFragment pkgFragment = (IPackageFragment) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
    String packageName = pkgFragment.getElementName();
    if (packageName.length() > 0) {
        return packageName + '.' + element.getElementName();
    }/* w  w w .  j av  a 2  s  .c om*/
    return element.getElementName();
}

From source file:br.com.objectos.way.ide.IFileWrapperPojo.java

License:Apache License

@Override
public Optional<ICompilationUnit> getICompilationUnit() {
    IJavaElement element = JavaCore.create(file);
    ICompilationUnit compilationUnit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
    return Optional.of(compilationUnit);
}

From source file:byke.DependencyAnalysis.java

License:Open Source License

private IJavaElement enclosingPackageOf(IJavaElement element) throws InvalidElement {
    IPackageFragment result = (IPackageFragment) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
    assertValid(result);/*from ww  w  .  ja va  2 s.  c  om*/
    return result;
}

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

License:Open Source License

/**
 * @see org.eclipse.jdt.core.IWorkingCopy#getOriginal(org.eclipse.jdt.core.IJavaElement)
 * @deprecated//from w  w w .  j  a  v  a2 s .c  om
 */
public IJavaElement getOriginal(IJavaElement workingCopyElement) {
    // backward compatibility
    if (!isWorkingCopy())
        return null;
    CompilationUnit cu = (CompilationUnit) workingCopyElement.getAncestor(COMPILATION_UNIT);
    if (cu == null || !this.owner.equals(cu.owner)) {
        return null;
    }

    return workingCopyElement.getPrimaryElement();
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.HierarchyScope.java

License:Open Source License

public boolean encloses(IJavaElement element, IProgressMonitor progressMonitor) {
    if (this.hierarchy == null) {
        if (this.includeFocusType && this.focusType.equals(element.getAncestor(IJavaElement.TYPE))) {
            return true;
        } else {/*from  w w w . j  a  v  a2  s. c o  m*/
            if (this.needsRefresh) {
                try {
                    initialize(progressMonitor);
                } catch (JavaModelException e) {
                    return false;
                }
            } else {
                // the scope is used only to find enclosing projects and jars
                // clients is responsible for filtering out elements not in the hierarchy (see SearchEngine)
                return true;
            }
        }
    }
    if (this.needsRefresh) {
        try {
            refresh(progressMonitor);
        } catch (JavaModelException e) {
            return false;
        }
    }
    IType type = null;
    if (element instanceof IType) {
        type = (IType) element;
    } else if (element instanceof IMember) {
        type = ((IMember) element).getDeclaringType();
    }
    if (type != null) {
        if (this.focusType.equals(type))
            return this.includeFocusType;
        // potentially allow travelling in:
        if (enclosesType(type, this.allowMemberAndEnclosingTypes)) {
            return true;
        }
        if (this.allowMemberAndEnclosingTypes) {
            // travel out: queried type is enclosed in this scope if its (indirect) declaring type is:
            IType enclosing = type.getDeclaringType();
            while (enclosing != null) {
                // don't allow travelling in again:
                if (enclosesType(enclosing, false)) {
                    return true;
                }
                enclosing = enclosing.getDeclaringType();
            }
        }
    }
    return false;
}