Example usage for org.eclipse.jdt.core.dom IMethodBinding getJavaElement

List of usage examples for org.eclipse.jdt.core.dom IMethodBinding getJavaElement

Introduction

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

Prototype

public IJavaElement getJavaElement();

Source Link

Document

Returns the Java element that corresponds to this binding.

Usage

From source file:ca.uvic.chisel.diver.sequencediagrams.sc.java.model.ASTUTils.java

License:Open Source License

public static MethodDeclaration findDeclarationFor(MethodInvocation invocation) {
    IMethodBinding binding = invocation.resolveMethodBinding();
    if (binding != null) {
        IMethod method = (IMethod) binding.getJavaElement();
        IType declaringType = method.getDeclaringType();
        ASTNode typeNode = getASTFor(declaringType);
        if (typeNode != null) {
            return findMethodDeclaration(typeNode, method);
        }//from w w  w  .  jav  a2 s .c  o m
    }
    return null;
}

From source file:ca.uvic.chisel.diver.sequencediagrams.sc.java.model.JavaActivation.java

License:Open Source License

@Override
public List<JavaMessage> getMessages() {
    if (this.messages == null) {
        this.messages = new ArrayList<JavaMessage>();
        if (getAST() == null) {
            return messages;
        }//from w w  w .j a va 2 s .  c o m
        MessageFinder finder = new MessageFinder();
        getAST().accept(finder);
        for (ASTNode node : finder.messages) {
            IMethodBinding binding = null;
            switch (node.getNodeType()) {
            case ASTNode.METHOD_INVOCATION:
                binding = ((MethodInvocation) node).resolveMethodBinding();
                break;
            case ASTNode.SUPER_METHOD_INVOCATION:
                binding = ((SuperMethodInvocation) node).resolveMethodBinding();
                break;
            case ASTNode.CONSTRUCTOR_INVOCATION:
                binding = ((ConstructorInvocation) node).resolveConstructorBinding();
                break;
            case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
                binding = ((SuperConstructorInvocation) node).resolveConstructorBinding();
                break;
            case ASTNode.CLASS_INSTANCE_CREATION:
                binding = ((ClassInstanceCreation) node).resolveConstructorBinding();
                break;
            }
            if (binding != null) {
                IMethod target = (IMethod) binding.getJavaElement();
                if (target != null) {
                    JavaActivation targetActivation = new JavaActivation(tree, target);
                    JavaMessage message = new JavaMessage(tree, node, this, targetActivation);
                    //add catches
                    targetActivation.setCallingMessage(message);
                    message.setTries(finder.invocationTries.get(node));
                    messages.add(message);
                }
            } else if (node instanceof ThrowStatement) {
                ThrowStatement statement = (ThrowStatement) node;
                List<IType> throwns = finder.thrownTypes.get(statement);
                for (IType thrown : throwns) {
                    JavaMessage parentMessage = getCallingMessage();
                    IJavaActivation root = this;
                    JavaMessage message = null;
                    while (parentMessage != null) {
                        root = parentMessage.getSource();
                        if (parentMessage.catches(thrown)) {
                            break;
                        }
                        parentMessage = root.getCallingMessage();
                    }
                    if (root != null && root != this) {
                        message = new JavaMessage(tree, statement, this, root);
                        message.setType(thrown);
                        message.setException(true);
                        messages.add(message);
                    }
                }
            } else if (node instanceof ReturnStatement) {
                if (getCallingMessage() != null) {
                    JavaMessage message = new JavaMessage(tree, node, this, getCallingMessage().getSource());
                    IJavaProject project = getJavaElement().getJavaProject();
                    if (project != null) {
                        String returnType;
                        try {
                            returnType = ((IMethod) getJavaElement()).getReturnType();
                            if (returnType != null) {
                                message.setType(returnType);
                            }
                        } catch (JavaModelException e) {
                        }

                    }
                    messages.add(message);

                }
            }
        }
    }
    return messages;
}

From source file:ca.uvic.chisel.diver.sequencediagrams.sc.java.model.JavaMessage.java

License:Open Source License

public IJavaElement getJavaElement() {
    if (element == null) {
        IMethodBinding binding = null;
        if (getAST() instanceof MethodInvocation) {
            binding = ((MethodInvocation) getAST()).resolveMethodBinding();
        } else if (getAST() instanceof SuperMethodInvocation) {
            binding = ((SuperMethodInvocation) getAST()).resolveMethodBinding();
        } else if (getAST() instanceof ConstructorInvocation) {
            binding = ((ConstructorInvocation) getAST()).resolveConstructorBinding();
        } else if (getAST() instanceof SuperConstructorInvocation) {
            binding = ((SuperConstructorInvocation) getAST()).resolveConstructorBinding();
        } else if (getAST() instanceof ClassInstanceCreation) {
            binding = ((ClassInstanceCreation) getAST()).resolveConstructorBinding();
        } else if (getAST() instanceof ReturnStatement) {
            return type;
        } else if (getAST() instanceof ThrowStatement) {
            return type;
        } else if (getAST() instanceof MethodDeclaration) {
            return target.getJavaElement();
        }// ww w  .j a va2  s. c  om
        if (binding != null) {
            element = binding.getJavaElement();
        } else {
            element = type;
        }
    }
    return element;
}

From source file:ca.uvic.chisel.javasketch.internal.ast.ASTMessageFinder.java

License:Open Source License

@Override
public boolean visit(MethodInvocation node) {
    if (!(message instanceof ICall))
        return false;
    if (containsMessage(node)) {
        ICall call = (ICall) message;/*from  w w w  .j  a  v a2s  .c  o  m*/

        IMethodBinding binding = node.resolveMethodBinding();
        if (binding != null) {
            binding = binding.getMethodDeclaration();
            if (binding != null) {
                IJavaElement element = binding.getJavaElement();
                if (element instanceof IMethod) {
                    try {
                        IMethod jm = (IMethod) element;
                        //get the target method.
                        ITraceClassMethod am = call.getTarget().getActivation().getMethod();
                        String types[] = Signature.getParameterTypes(am.getSignature());
                        IMethod testMethod = jm.getDeclaringType().getMethod(am.getName(), types);
                        if (jm.isSimilar(testMethod)) {
                            this.node = node;
                            try {
                                if (document.getLineOfOffset(node.getStartPosition()) != (call.codeLine() - 1))
                                    //look for a better match.
                                    return true;
                            } catch (BadLocationException e) {
                            }
                            return false;
                        }
                    } catch (NullPointerException e) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    return false;
}

From source file:ca.uvic.chisel.javasketch.internal.ast.ASTMessageFinder.java

License:Open Source License

public boolean visit(SuperMethodInvocation node) {
    if (!(message instanceof ICall))
        return false;
    if (containsMessage(node)) {
        ICall call = (ICall) message;//from  w w  w  .j  a  v  a 2s  .c o  m

        IMethodBinding binding = node.resolveMethodBinding();
        if (binding != null) {
            binding = binding.getMethodDeclaration();
            if (binding != null) {
                IJavaElement element = binding.getJavaElement();
                if (element instanceof IMethod) {
                    try {
                        IMethod jm = (IMethod) element;
                        //get the target method.
                        ITraceClassMethod am = call.getTarget().getActivation().getMethod();
                        String types[] = Signature.getParameterTypes(am.getSignature());
                        IMethod testMethod = jm.getDeclaringType().getMethod(am.getName(), types);
                        if (jm.isSimilar(testMethod)) {
                            this.node = node;
                            try {
                                if (document.getLineOfOffset(node.getStartPosition()) != (call.codeLine() - 1))
                                    //look for a better match.
                                    return true;
                            } catch (BadLocationException e) {
                            }
                            return false;
                        }
                    } catch (NullPointerException e) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    return false;
}

From source file:ca.uvic.chisel.javasketch.internal.ast.ASTMessageFinder.java

License:Open Source License

public boolean visit(SuperConstructorInvocation node) {
    if (!(message instanceof ICall))
        return false;
    if (containsMessage(node)) {
        ICall call = (ICall) message;// w  ww.  j  av a2s.  c o  m
        IMethodBinding binding = node.resolveConstructorBinding();
        if (binding != null) {
            binding = binding.getMethodDeclaration();
            if (binding != null) {
                IJavaElement element = binding.getJavaElement();
                if (element instanceof IMethod) {
                    try {
                        IMethod jm = (IMethod) element;
                        //get the target method.
                        ITraceClassMethod am = call.getTarget().getActivation().getMethod();
                        if (JavaSearchUtils.getFullyQualifiedName(jm.getDeclaringType(), true)
                                .equals(am.getTraceClass().getName())) {
                            String types[] = Signature.getParameterTypes(am.getSignature());
                            IMethod testMethod = jm.getDeclaringType().getMethod(am.getName(), types);
                            if (jm.isSimilar(testMethod)) {
                                this.node = node;
                                try {
                                    if (document
                                            .getLineOfOffset(node.getStartPosition()) != (call.codeLine() - 1))
                                        //look for a better match.
                                        return true;
                                } catch (BadLocationException e) {
                                }
                                return false;
                            }
                        }
                    } catch (NullPointerException e) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    return false;
}

From source file:ca.uvic.chisel.javasketch.internal.ast.ASTMessageFinder.java

License:Open Source License

@Override
public boolean visit(ConstructorInvocation node) {
    if (!(message instanceof ICall))
        return false;
    if (containsMessage(node)) {
        ICall call = (ICall) message;/*from  w  w  w . j av a  2  s. c  o m*/
        IMethodBinding binding = node.resolveConstructorBinding();
        if (binding != null) {
            binding = binding.getMethodDeclaration();
            if (binding != null) {
                IJavaElement element = binding.getJavaElement();
                if (element instanceof IMethod) {
                    try {
                        IMethod jm = (IMethod) element;
                        //get the target method.
                        ITraceClassMethod am = call.getTarget().getActivation().getMethod();
                        if (JavaSearchUtils.getFullyQualifiedName(jm.getDeclaringType(), true)
                                .equals(am.getTraceClass().getName())) {
                            String types[] = Signature.getParameterTypes(am.getSignature());
                            IMethod testMethod = jm.getDeclaringType().getMethod(am.getName(), types);
                            if (jm.isSimilar(testMethod)) {
                                this.node = node;
                                try {
                                    if (document
                                            .getLineOfOffset(node.getStartPosition()) != (call.codeLine() - 1))
                                        //look for a better match.
                                        return true;
                                } catch (BadLocationException e) {
                                }
                                return false;
                            }
                        }
                    } catch (NullPointerException e) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    return false;
}

From source file:ca.uvic.chisel.javasketch.internal.ast.ASTMessageFinder.java

License:Open Source License

public boolean visit(ClassInstanceCreation node) {
    if (!(message instanceof ICall))
        return false;
    if (containsMessage(node)) {
        ICall call = (ICall) message;//from ww  w.jav  a2 s.  c  o  m
        IMethodBinding binding = node.resolveConstructorBinding();
        if (binding != null) {
            binding = binding.getMethodDeclaration();
            if (binding != null) {
                IJavaElement element = binding.getJavaElement();
                if (element instanceof IMethod) {
                    try {
                        IMethod jm = (IMethod) element;
                        //get the target method.
                        ITraceClassMethod am = call.getTarget().getActivation().getMethod();
                        if (JavaSearchUtils.getFullyQualifiedName(jm.getDeclaringType(), true)
                                .equals(am.getTraceClass().getName())) {
                            String types[] = Signature.getParameterTypes(am.getSignature());
                            IMethod testMethod = jm.getDeclaringType().getMethod(am.getName(), types);
                            if (jm.isSimilar(testMethod)) {
                                this.node = node;
                                try {
                                    if (document
                                            .getLineOfOffset(node.getStartPosition()) != (call.codeLine() - 1))
                                        //look for a better match.
                                        return true;
                                } catch (BadLocationException e) {
                                }
                                return false;
                            }
                        }
                    } catch (NullPointerException e) {
                        return true;
                    }
                } else {
                    //try to match just on the class name
                    ITypeBinding typeBinding = binding.getDeclaringClass();
                    IJavaElement je = typeBinding.getJavaElement();
                    if (je instanceof IType) {
                        IType type = (IType) je;
                        try {
                            ITraceClassMethod am = call.getTarget().getActivation().getMethod();
                            if (JavaSearchUtils.getFullyQualifiedName(type, true)
                                    .equals(am.getTraceClass().getName())) {
                                this.node = node;
                                try {
                                    if (document
                                            .getLineOfOffset(node.getStartPosition()) != (call.codeLine() - 1))
                                        //look for a better match.
                                        return true;
                                } catch (BadLocationException e) {
                                }
                                return false;
                            }
                        } catch (NullPointerException e) {
                            return true;
                        }
                    }
                }
            }
        }
        return true;
    }

    return false;
}

From source file:com.google.gwt.eclipse.core.wizards.rpc.NewAsyncRemoteServiceInterfaceCreationWizardPage.java

License:Open Source License

public static List<IMethodBinding> computeSyncMethodsThatNeedAsyncVersions(ITypeBinding syncTypeBinding,
        ITypeBinding asyncTypeBinding) {
    // Compute all overridable methods on the sync interface
    List<IMethodBinding> overridableSyncMethods = computeOverridableMethodsForInterface(syncTypeBinding);

    // Remove sync methods that would override existing methods in the
    // async hierarchy
    List<IMethodBinding> remainingMethods = new ArrayList<IMethodBinding>();
    for (IMethodBinding overridableSyncMethod : overridableSyncMethods) {
        IMethod syncMethod = (IMethod) overridableSyncMethod.getJavaElement();
        String[] asyncParameterTypes = RemoteServiceUtilities.computeAsyncParameterTypes(overridableSyncMethod);
        if (Bindings.findMethodInHierarchy(asyncTypeBinding, syncMethod.getElementName(),
                asyncParameterTypes) != null) {
            // Don't add method that appear in the type hierarchy
            continue;
        }//from   w  ww  . j  a  v  a2 s  .  c o  m

        remainingMethods.add(overridableSyncMethod);
    }

    return remainingMethods;
}

From source file:com.google.gwt.eclipse.core.wizards.rpc.NewAsyncRemoteServiceInterfaceCreationWizardPage.java

License:Open Source License

public static String createMethodContents(IType newType, ImportManagerAdapter imports,
        IMethodBinding overridableSyncMethod, boolean addComments) throws CoreException, JavaModelException {
    StringBuilder sb = new StringBuilder();

    IMethod syncMethod = (IMethod) overridableSyncMethod.getJavaElement();

    if (addComments) {
        String lineDelimiter = "\n"; // OK, since content is formatted afterwards

        // Don't go through CodeGeneration type since it can't deal with delegates
        String comment = StubUtility.getMethodComment(newType.getCompilationUnit(),
                newType.getFullyQualifiedName(), syncMethod.getElementName(), NO_STRINGS, NO_STRINGS,
                Signature.SIG_VOID, NO_STRINGS, syncMethod, true, lineDelimiter);
        if (comment != null) {
            sb.append(comment);/*  ww  w. j a va2s.  c  o m*/
            sb.append(lineDelimiter);
        }
    }

    // Expand the type parameters
    ITypeParameter[] typeParameters = syncMethod.getTypeParameters();
    ITypeBinding[] typeParameterBindings = overridableSyncMethod.getTypeParameters();
    if (typeParameters.length > 0) {
        sb.append("<");
        for (int i = 0; i < typeParameters.length; ++i) {
            sb.append(typeParameters[i].getElementName());
            ITypeBinding typeParameterBinding = typeParameterBindings[i];
            ITypeBinding[] typeBounds = typeParameterBinding.getTypeBounds();
            if (typeBounds.length > 0) {
                sb.append(" extends ");
                for (int j = 0; j < typeBounds.length; ++j) {
                    if (j != 0) {
                        sb.append(" & ");
                    }
                    expandTypeBinding(typeBounds[j], sb, imports);
                }
            }
        }
        sb.append(">");
    }

    // Default to a void return type for the async method
    sb.append("void ");

    // Expand the method name
    sb.append(overridableSyncMethod.getName());

    // Expand the arguments
    sb.append("(");
    String[] parameterNames = syncMethod.getParameterNames();
    ITypeBinding[] parameterTypes = overridableSyncMethod.getParameterTypes();
    for (int i = 0; i < parameterNames.length; ++i) {
        if (i != 0) {
            sb.append(", ");
        }

        expandTypeBinding(parameterTypes[i], sb, imports);
        sb.append(" ");
        sb.append(parameterNames[i]);
    }

    if (parameterNames.length > 0) {
        sb.append(", ");
    }

    sb.append(imports.addImport(RemoteServiceUtilities.ASYNCCALLBACK_QUALIFIED_NAME));
    sb.append("<");
    ITypeBinding syncReturnType = overridableSyncMethod.getReturnType();
    if (syncReturnType.isPrimitive()) {
        String wrapperTypeName = JavaASTUtils.getWrapperTypeName(syncReturnType.getName());
        sb.append(imports.addImport(wrapperTypeName));
    } else {
        expandTypeBinding(syncReturnType, sb, imports);
    }
    sb.append("> ");
    sb.append(StringUtilities.computeUniqueName(parameterNames, "callback"));
    sb.append(");");

    return sb.toString();
}