Example usage for org.eclipse.jdt.core IMethod isSimilar

List of usage examples for org.eclipse.jdt.core IMethod isSimilar

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IMethod isSimilar.

Prototype

boolean isSimilar(IMethod method);

Source Link

Document

Returns whether this method is similar to the given method.

Usage

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;// w w w.java 2  s. co  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;/*w  w w .j  a  v a 2 s .c om*/

        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 w w  .  ja  v a  2s. c  om*/
        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;/*w ww .j  a v  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

public boolean visit(ClassInstanceCreation node) {
    if (!(message instanceof ICall))
        return false;
    if (containsMessage(node)) {
        ICall call = (ICall) message;//from w  ww.jav a 2 s.  c om
        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:ca.uvic.chisel.javasketch.internal.JavaSearchUtils.java

License:Open Source License

/**
 * Searches the given thread for activations that match the given java method.
 * @param thread the thread to search.//from   www.  jav a  2s  . c o m
 * @param method the method to check.
 * @param monitor this is a long-running process, and should be done inside a monitor.
 * @return a list of found activations
 * @throws CoreException if an error occurred during retrieval of the activations
 */
public static List<IActivation> findActivationsForMethod(IThread thread, IMethod element,
        IProgressMonitor monitor) throws CoreException {
    DBProgramSketch sketch = (DBProgramSketch) SketchPlugin.getDefault().getSketch(thread);
    ArrayList<IActivation> activations = new ArrayList<IActivation>();
    if (sketch == null) {
        return activations;
    }
    Exception ex = null;
    monitor.beginTask("Searching for matching activations", IProgressMonitor.UNKNOWN);
    try {
        PreparedStatement methodStatement = sketch.getPortal().prepareStatement(METHOD_QUERY);
        IType methodType = element.getDeclaringType();
        String qualifiedName = methodType.getFullyQualifiedName();
        String methodName = element.getElementName();
        methodStatement.setString(1, qualifiedName);
        methodStatement.setString(2, methodName);
        methodStatement.setLong(3, ((ThreadImpl) thread).getModelID());
        ResultSet results = methodStatement.executeQuery();

        while (results.next()) {
            if (monitor.isCanceled())
                break;
            String signature = results.getString(1);
            //try to find a method that matches in the declaring type
            IMethod matchMethod = methodType.getMethod(methodName, Signature.getParameterTypes(signature));
            if (element.isSimilar(matchMethod)) {
                long model_id = results.getLong("model_id");
                IActivation a = ((ThreadImpl) thread).getByModelID(model_id);
                if (a != null) {
                    activations.add(a);
                }
            }
        }
    } catch (IllegalArgumentException e) {
        ex = e;
    } catch (SQLException e) {
        ex = e;
    }
    monitor.done();
    if (ex != null) {
        throw new CoreException(SketchPlugin.getDefault().createStatus(ex));
    }
    return activations;
}

From source file:ca.uvic.chisel.javasketch.internal.JavaSearchUtils.java

License:Open Source License

public static ITraceClassMethod findSimilarMethod(ITrace trace, IMethod method) {
    IType declaringType = method.getDeclaringType();
    IProgramSketch sketch = SketchPlugin.getDefault().getSketch(trace);
    if (sketch != null) {
        ITraceClass tc = findSimilarType(trace, declaringType);
        if (tc != null) {
            try {
                PreparedStatement statement = sketch.getPortal().prepareStatement(
                        "SELECT method_signature FROM Method WHERE type_name=? AND method_name=?");
                statement.setString(1, tc.getName());
                statement.setString(2, method.getElementName());
                ResultSet results = statement.executeQuery();
                while (results.next()) {
                    String signature = results.getString(1);
                    IMethod compareMethod = declaringType.getMethod(method.getElementName(),
                            Signature.getParameterTypes(signature));
                    if (method.isSimilar(compareMethod)) {
                        return tc.findMethod(method.getElementName(), signature);
                    }//from w ww.ja  v  a2s.  c  o m
                }
            } catch (IllegalArgumentException e) {
            } catch (SQLException e) {
            } catch (CoreException e) {
            }
        }
    }
    return null;
}

From source file:net.sourceforge.metrics.calculators.Norm.java

License:Open Source License

private int countMethods(IType[] supers, int overridden, List<IMethod> counted, IMethod myMethod)
        throws JavaModelException {
    int totalOverridden = overridden;
    for (IType super1 : supers) {
        IMethod[] inheritedMethods = super1.getMethods();
        for (IMethod inheritedMethod : inheritedMethods) {
            if (counted.contains(myMethod)) {
                continue;
            }// w ww .j a  v  a 2s .c  o  m
            IMethod inherited = inheritedMethod;
            int inheritedFlags = inherited.getFlags();
            // don't have to consider static methods
            if ((inheritedFlags & Flags.AccStatic) != 0) {
                continue;
            }
            // don't have to consider private methods
            if ((inheritedFlags & Flags.AccPrivate) != 0) {
                continue;
            }
            // don't count abstract methods unless preferences dictate it
            if ((!getPrefs().countAbstract()) && ((inheritedFlags & Flags.AccAbstract) != 0)) {
                continue;
            }
            // methods must have same signature and return type
            if (!inherited.isSimilar(myMethod)) {
                continue;
            }
            // don't count methods invoking super unless preferences
            // override
            if ((getPrefs().countSuper()) || (!containsSuperCall(myMethod))) {
                totalOverridden++;
                counted.add(myMethod);
            }
        }
    }
    return totalOverridden;
}

From source file:org.eclim.plugin.jdt.util.TypeUtils.java

License:Open Source License

/**
 * Determines if any of the super types of the supplied type contains the
 * supplied method and if so return that super type.
 *
 * @param type The type./*ww w . ja  v  a2  s  .c o  m*/
 * @param method The method.
 * @return The super type that contains the method, or null if none.
 */
public static TypeInfo getSuperTypeContainingMethod(IType type, IMethod method) throws Exception {
    TypeInfo[] types = getSuperTypes(type);
    for (TypeInfo info : types) {
        IMethod[] methods = info.getType().getMethods();
        for (IMethod m : methods) {
            if (m.isSimilar(method)) {
                return info;
            }
        }
    }
    return null;
}

From source file:org.eclipse.ajdt.core.javaelements.ITDAwareSourceTypeInfo.java

License:Open Source License

private List<IJavaElement> getITDs(SourceType type) throws JavaModelException {
    AJProjectModelFacade model = AJProjectModelFactory.getInstance().getModelForJavaElement(type);
    if (model.hasModel()) {
        List<IJavaElement> itds = new ArrayList<IJavaElement>();
        List<IJavaElement> rels = model.getRelationshipsForElement(type,
                AJRelationshipManager.ASPECT_DECLARATIONS);

        List<IMethod> childMethods = null;

        for (IJavaElement ije : rels) {
            if (ije instanceof IntertypeElement) {
                IntertypeElement elt = (IntertypeElement) ije;
                IMember member = elt.createMockDeclaration(type);
                // null if the ITD doesn't exist in the AspectJ hierarchy
                // will happen if the Java side has partial compilation 
                // and aspectj side does not
                if (member != null) {

                    // should not add this ITD if it is a duplicate
                    // of another ITD
                    if (!isAlreadyAnITD(itds, member)) {
                        continue;
                    }//from  www .j a  v  a 2s  .com

                    itds.add(member);

                    // additional processing for interfaces
                    if (handle.isInterface()) {

                        if (member.getElementType() == IJavaElement.FIELD) {
                            // Bug 262969
                            // Interfaces can't have fields, so ignore
                            // Reconciling errors occur if an ITD field is
                            // referenced outside of the aspect that declares them, 
                            // but only if the declared type of the object is an interface.
                            itds.remove(member);
                        } else if (member.getElementType() == IJavaElement.METHOD) {
                            // now look to see if this ITD a method that provides
                            // a default implementation for an interface method
                            // use IMethod.isSimilar
                            if (childMethods == null) {
                                childMethods = (List<IMethod>) type.getChildrenOfType(IJavaElement.METHOD);
                            }
                            for (IMethod method : childMethods) {
                                if (method.isSimilar((IMethod) member)) {
                                    itds.remove(member);
                                    break;
                                }
                            }
                        }
                    }
                }
            } else if (ije instanceof DeclareElement) {
                DeclareElement elt = (DeclareElement) ije;

                // use createElementInfo, not getElementInfo because 
                // we don't want it cached
                DeclareElementInfo info = (DeclareElementInfo) elt.createElementInfo();
                if (info == null || info.getAJKind() != Kind.DECLARE_PARENTS) {
                    continue;
                }

                char[][] newSupers = info.getTypes();
                augmentHierarchy(newSupers);
            } else if (ije instanceof AspectElement || ije instanceof BinaryAspectElement) {
                // likely a declare parents instantiated in a concrete aspect, but declared in a super aspect
                IProgramElement ipe = model.javaElementToProgramElement(ije);
                Map<String, List<String>> declareParentsMap = ipe.getDeclareParentsMap();
                if (declareParentsMap != null) {
                    augmentHierarchy(declareParentsMap.get(type.getFullyQualifiedName()));
                }
            } else if (ije instanceof IType) {
                // an ITIT
                itds.add(new ITIT(type, (IType) ije, model.javaElementToProgramElement(ije)));
            }
        }
        return itds;
    }
    return new LinkedList<IJavaElement>();
}