Example usage for org.eclipse.jdt.core.dom Expression getRoot

List of usage examples for org.eclipse.jdt.core.dom Expression getRoot

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom Expression getRoot.

Prototype

public final ASTNode getRoot() 

Source Link

Document

Returns the root node at or above this node; returns this node if it is a root.

Usage

From source file:com.intel.ide.eclipse.mpt.ast.UnresolvedElementsSubProcessor.java

License:Open Source License

private static String getExpressionBaseName(Expression expr) {
    IBinding argBinding = Bindings.resolveExpressionBinding(expr, true);
    if (argBinding instanceof IVariableBinding) {
        IJavaProject project = null;/*from www.  j a v a 2  s.c o m*/
        ASTNode root = expr.getRoot();
        if (root instanceof CompilationUnit) {
            ITypeRoot typeRoot = ((CompilationUnit) root).getTypeRoot();
            if (typeRoot != null)
                project = typeRoot.getJavaProject();
        }
        return StubUtility.getBaseName((IVariableBinding) argBinding, project);
    }
    if (expr instanceof SimpleName)
        return ((SimpleName) expr).getIdentifier();
    return null;
}

From source file:org.eclipse.wb.internal.core.utils.ast.AstNodeUtils.java

License:Open Source License

/**
 * For {@link AnonymousClassDeclaration} we can not get full name using only {@link ITypeBinding},
 * so we use AST to find necessary <code>$1</code>, <code>$2</code>, etc suffixes.
 * /*w  w w . j  a  va2  s.  co m*/
 * @return the fully qualified name with anonymous suffix.
 */
private static String getFullyQualifiedName_appendAnonymous(Expression expression, final boolean runtime,
        String fullyQualifiedName) {
    final AnonymousClassDeclaration acd = ((ClassInstanceCreation) expression).getAnonymousClassDeclaration();
    final String[] suffix = new String[1];
    expression.getRoot().accept(new ASTVisitor() {
        private final int[] m_counts = new int[32];
        private int m_level = 0;

        @Override
        public boolean visit(AnonymousClassDeclaration node) {
            m_counts[m_level]++;
            if (node == acd) {
                StringBuilder sb = new StringBuilder();
                String separator = runtime ? "$" : ".";
                for (int i = 0; i <= m_level; i++) {
                    sb.append(separator);
                    sb.append(m_counts[i]);
                }
                suffix[0] = sb.toString();
            }
            m_counts[++m_level] = 0;
            return suffix[0] == null;
        }

        @Override
        public void endVisit(AnonymousClassDeclaration node) {
            m_level--;
        }
    });
    fullyQualifiedName = StringUtils.stripEnd(fullyQualifiedName, "$.");
    fullyQualifiedName += suffix[0];
    return fullyQualifiedName;
}