Example usage for org.eclipse.jdt.core.dom Name resolveBinding

List of usage examples for org.eclipse.jdt.core.dom Name resolveBinding

Introduction

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

Prototype

public final IBinding resolveBinding() 

Source Link

Document

Resolves and returns the binding for the entity referred to by this name.

Usage

From source file:ca.mcgill.cs.swevo.ppa.inference.QNameInferenceStrategy.java

License:Open Source License

/**
 * This method is necessary because resolveTypeBinding for a qualified name may result in
 * checking the declared class of a field instead of looking at the type of the previous field.
 * /*from  w ww.j  av a2 s .  c o  m*/
 * @param right
 * @return
 */
private ITypeBinding getRightBinding(Name right) {
    IBinding binding = right.resolveBinding();
    ITypeBinding typeBinding = null;
    if (binding != null && binding instanceof IVariableBinding) {
        IVariableBinding varBinding = (IVariableBinding) binding;
        typeBinding = varBinding.getType();
    } else {
        typeBinding = right.resolveTypeBinding();
    }

    return typeBinding;
}

From source file:ca.mcgill.cs.swevo.ppa.ui.actions.PPAOnNameAction.java

License:Open Source License

public void run(IAction action) {
    if (javaEditor != null) {
        ICompilationUnit icu = (ICompilationUnit) SelectionConverter.getInput(javaEditor);
        IDocument document = JavaUI.getDocumentProvider().getDocument(javaEditor.getEditorInput());
        JavaTextSelection javaSelection = new JavaTextSelection(icu, document, selection.getOffset(),
                selection.getLength());/*from  w  ww .ja v  a2s.com*/
        ASTNode[] nodes = javaSelection.resolveSelectedNodes();

        if (nodes != null && nodes.length == 1) {
            ASTNode node = nodes[0];
            if (node instanceof Name) {

                // 1- Get the PPA CU:
                CompilationUnit cu = PPAUtil.getCU((IFile) icu.getResource(), new PPAOptions());
                if (cu == null) {
                    return;
                }
                PPANameVisitor nameVisitor = new PPANameVisitor();
                cu.accept(nameVisitor);

                // 2- Get the corresponding name in the PPA CU
                Name ppaNode = nameVisitor.getName((Name) node);
                if (ppaNode != null) {
                    String bindingText = PPABindingsUtil.getBindingText(ppaNode.resolveBinding());
                    MessageDialog.openInformation(javaEditor.getEditorSite().getShell(),
                            "Binding for " + ppaNode.getFullyQualifiedName(), bindingText);
                }

                // 3- Clean-up
                PPAUtil.cleanUp(cu);
            }
        }
    }
}

From source file:ca.mcgill.cs.swevo.ppa.ui.NameBindingVisitor.java

License:Open Source License

@Override
public void postVisit(ASTNode node) {
    super.postVisit(node);

    if (node instanceof Expression) {
        Expression exp = (Expression) node;

        IBinding binding = null;/* ww  w.ja  v  a  2 s.c o  m*/
        if (exp instanceof Name) {
            Name name = (Name) exp;
            binding = name.resolveBinding();
        } else if (exp instanceof MethodInvocation) {
            MethodInvocation mi = (MethodInvocation) exp;
            binding = mi.resolveMethodBinding();
        } else if (exp instanceof ClassInstanceCreation) {
            ClassInstanceCreation cic = (ClassInstanceCreation) exp;
            binding = cic.resolveConstructorBinding();
        } else {
            return;
        }

        printer.println("Node: " + node.toString());
        ITypeBinding tBinding = exp.resolveTypeBinding();
        if (tBinding != null) {
            printer.println("  Type Binding: " + tBinding.getQualifiedName());
            printer.println("  isAnnotation?: " + tBinding.isAnnotation());
        }

        if (binding != null) {
            printer.println("  " + PPABindingsUtil.getBindingText(binding));
        }
        printer.flush();
    }
    monitor.worked(1);
}

From source file:com.google.devtools.j2cpp.translate.Rewriter.java

License:Open Source License

/**
 * Rewrites System.out and System.err println calls as NSLog calls.
 *
 * @return true if the node was rewritten
 *//*from w ww.  j  a  v  a 2  s.co  m*/
// TODO(user): remove when there is iOS console support.
@SuppressWarnings("unchecked")
private boolean rewriteSystemOut(MethodInvocation node) {
    Expression expression = node.getExpression();
    if (expression instanceof Name) {
        Name expr = (Name) node.getExpression();
        IBinding binding = expr.resolveBinding();
        if (binding instanceof IVariableBinding) {
            IVariableBinding varBinding = (IVariableBinding) binding;
            ITypeBinding type = varBinding.getDeclaringClass();
            if (type == null) {
                return false;
            }
            String clsName = type.getQualifiedName();
            String varName = varBinding.getName();
            if (clsName.equals("java.lang.System") && (varName.equals("out") || varName.equals("err"))) {
                // Change System.out.* or System.err.* to NSLog
                AST ast = node.getAST();
                MethodInvocation newInvocation = ast.newMethodInvocation();
                IMethodBinding methodBinding = new IOSMethodBinding("NSLog", Types.getMethodBinding(node),
                        null);
                Types.addBinding(newInvocation, methodBinding);
                Types.addFunction(methodBinding);
                newInvocation.setName(ast.newSimpleName("NSLog"));
                Types.addBinding(newInvocation.getName(), methodBinding);
                newInvocation.setExpression(null);

                // Insert NSLog format argument
                List<Expression> args = node.arguments();
                if (args.size() == 1) {
                    Expression arg = args.get(0);
                    arg.accept(this);
                    String format = getFormatArgument(arg);
                    StringLiteral literal = ast.newStringLiteral();
                    literal.setLiteralValue(format);
                    Types.addBinding(literal, ast.resolveWellKnownType("java.lang.String"));
                    newInvocation.arguments().add(literal);

                    // JDT won't let nodes be re-parented, so copy and map.
                    ASTNode newArg = NodeCopier.copySubtree(ast, arg);
                    if (arg instanceof MethodInvocation) {
                        IMethodBinding argBinding = ((MethodInvocation) arg).resolveMethodBinding();
                        if (!argBinding.getReturnType().isPrimitive()) {
                            IOSMethodBinding newBinding = new IOSMethodBinding("format", argBinding,
                                    Types.getNSString());
                            Types.addMappedInvocation((MethodInvocation) newArg, newBinding);
                        }
                    }
                    newInvocation.arguments().add(newArg);
                } else if (args.size() > 1 && node.getName().getIdentifier().equals("printf")) {
                    newInvocation.arguments().addAll(NodeCopier.copySubtrees(ast, args));
                } else if (args.size() == 0) {
                    // NSLog requires a format string.
                    StringLiteral literal = ast.newStringLiteral();
                    literal.setLiteralValue("");
                    Types.addBinding(literal, ast.resolveWellKnownType("java.lang.String"));
                    newInvocation.arguments().add(literal);
                }

                // Replace old invocation with new.
                ASTNode parent = node.getParent();
                if (parent instanceof ExpressionStatement) {
                    ExpressionStatement stmt = (ExpressionStatement) parent;
                    stmt.setExpression(newInvocation);
                } else {
                    throw new AssertionError("unknown parent type: " + parent.getClass().getSimpleName());
                }
                return true;
            }
        }
    }
    return false;
}

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

License:Open Source License

public static void addNewTypeProposals(ICompilationUnit cu, Name refNode, int kind, int relevance,
        Map<String, LinkedCorrectionProposal> proposals) throws CoreException {
    Name node = refNode;//  w ww.  j ava2s.c  o  m
    //      do {
    String typeName = ASTNodes.getSimpleNameIdentifier(node);
    Name qualifier = null;
    // only propose to create types for qualifiers when the name starts with upper case
    boolean isPossibleName = isLikelyTypeName(typeName) || node == refNode;
    if (isPossibleName) {
        IPackageFragment enclosingPackage = null;
        IType enclosingType = null;
        if (node.isSimpleName()) {
            enclosingPackage = (IPackageFragment) cu.getParent();
            return;
            // don't suggest member type, user can select it in wizard
        } else {
            Name qualifierName = ((QualifiedName) node).getQualifier();
            IBinding binding = qualifierName.resolveBinding();
            if (binding != null && binding.isRecovered()) {
                binding = null;
            }
            if (binding instanceof ITypeBinding) {
                enclosingType = (IType) binding.getJavaElement();
            } else if (binding instanceof IPackageBinding) {
                qualifier = qualifierName;
                enclosingPackage = (IPackageFragment) binding.getJavaElement();
            } else {
                IJavaElement[] res = cu.codeSelect(qualifierName.getStartPosition(), qualifierName.getLength());
                if (res != null && res.length > 0 && res[0] instanceof IType) {
                    enclosingType = (IType) res[0];
                } else {
                    qualifier = qualifierName;
                    enclosingPackage = JavaModelUtil.getPackageFragmentRoot(cu)
                            .getPackageFragment(ASTResolving.getFullName(qualifierName));
                }
            }
        }
        int rel = relevance;
        if (enclosingPackage != null && isLikelyPackageName(enclosingPackage.getElementName())) {
            rel += 3;
        }

        if (enclosingPackage != null
                && !enclosingPackage.getCompilationUnit(typeName + JavaModelUtil.DEFAULT_CU_SUFFIX).exists()
                || enclosingType != null && !enclosingType.isReadOnly()
                        && !enclosingType.getType(typeName).exists()) { // new member type
            IJavaElement enclosing = enclosingPackage != null ? (IJavaElement) enclosingPackage : enclosingType;

            String name = node.getFullyQualifiedName();

            if ((kind & SimilarElementsRequestor.CLASSES) != 0) {
                proposals.put(name, new NewCUProposal(cu, node, NewCUProposal.K_CLASS, enclosing, rel + 3));
            } else if ((kind & SimilarElementsRequestor.INTERFACES) != 0) {
                proposals.put(name, new NewCUProposal(cu, node, NewCUProposal.K_INTERFACE, enclosing, rel + 2));
            } else if ((kind & SimilarElementsRequestor.ENUMS) != 0) {
                proposals.put(name, new NewCUProposal(cu, node, NewCUProposal.K_ENUM, enclosing, rel));
            }
        }
    }
    node = qualifier;
    //      } while (node != null);
}

From source file:de.ovgu.cide.export.physical.ahead.FieldMethodVisibilityLifter.java

License:Open Source License

private boolean visitName(Name node) {
    IBinding binding = node.resolveBinding();
    if (binding == null)
        return true;

    ASTNode declaration = compUnit.findDeclaringNode(binding);
    if (declaration instanceof MethodDeclaration) {
        makeProtected((MethodDeclaration) declaration);
    } else if (declaration instanceof VariableDeclarationFragment) {
        VariableDeclarationFragment variableFragment = (VariableDeclarationFragment) declaration;

        if (variableFragment.getParent() instanceof FieldDeclaration) {
            FieldDeclaration fieldDecl = (FieldDeclaration) variableFragment.getParent();
            makeProtected(fieldDecl);/* w  w  w  .ja v a2 s  .  com*/
        }
    }

    return false;
}

From source file:de.ovgu.cide.typing.jdt.JDTCheckGenerator.java

License:Open Source License

@Override
protected void visitName(Name node) {
    IBinding binding = node.resolveBinding();

    if (binding instanceof IVariableBinding && !((IVariableBinding) binding).isField()) {
        if (knownVariableDeclarations.get(binding) != null)
            checks.add(new LocalVariableReferenceCheck(file, jdtTypingProvider, bridge(node),
                    knownVariableDeclarations.get(binding), node.toString()));
    }//  ww  w  .  j a  va 2 s.  c o  m
    super.visitName(node);
}

From source file:de.ovgu.cide.typing.jdt.JDTCheckGenerator.java

License:Open Source License

@Override
protected void visitName(Name node) {
    IBinding binding = node.resolveBinding();

    if (binding != null && binding instanceof IVariableBinding && ((IVariableBinding) binding).isField()) {
        handleFieldChecks(node, (IVariableBinding) binding);
    }/*  w  w  w. ja v a  2  s.  c om*/

    super.visitName(node);
}

From source file:edu.brown.cs.bubbles.bedrock.BedrockElider.java

License:Open Source License

private String getMethodType(Name n) {
    IMethodBinding mb = (IMethodBinding) n.resolveBinding();

    if (mb == null)
        return "U";

    String typ = "";

    if (mb.isDeprecated())
        typ = "D";

    if (Modifier.isAbstract(mb.getModifiers()))
        typ = "A";
    else if (Modifier.isStatic(mb.getModifiers()))
        typ = "S";

    return typ;/*from  www . ja va  2 s  .com*/
}

From source file:edu.brown.cs.bubbles.bedrock.BedrockElider.java

License:Open Source License

private String getVariableType(Name n) {
    IVariableBinding vb = (IVariableBinding) n.resolveBinding();

    if (vb == null)
        return "U";

    String typ = "";

    if (Modifier.isStatic(vb.getModifiers())) {
        if (Modifier.isFinal(vb.getModifiers()))
            typ = "C";
        else/*www. j a  v  a 2  s . com*/
            typ = "S";
    }

    return typ;
}