Example usage for org.eclipse.jdt.core IType getOccurrenceCount

List of usage examples for org.eclipse.jdt.core IType getOccurrenceCount

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IType getOccurrenceCount.

Prototype

int getOccurrenceCount();

Source Link

Document

Returns the position relative to the order this member is defined in the source.

Usage

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

License:Open Source License

private static IJavaElement searchForType(String classSignature, IJavaSearchScope scope,
        IProgressMonitor monitor) throws CoreException, InterruptedException {
    try {/*from ww w  .j  a v a2 s .  c  o  m*/
        synchronized (cachedTypes) {
            IType found = cachedTypes.get(classSignature);
            if (found != null) {
                return found;
            }
            SearchEngine engine = new SearchEngine();
            SearchPattern pattern = null;
            SearchParticipant participant = SearchEngine.getDefaultSearchParticipant();
            LocalSearchRequestor requestor = new LocalSearchRequestor();
            pattern = SearchPattern.createPattern(classSignature.replace('$', '.'), IJavaSearchConstants.CLASS,
                    IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH);

            engine.search(pattern, new SearchParticipant[] { participant }, scope, requestor, monitor);
            if (requestor.matches.size() > 0) {
                found = (IType) requestor.matches.get(0).getElement();
                if (found.getFullyQualifiedName().equals(classSignature)) {
                    cachedTypes.put(classSignature, found);
                    return found;
                }

            }
            String[] className = classSignature.split("\\$");
            found = cachedTypes.get(className[0]);
            if (found == null) {

                //find the class
                pattern = SearchPattern.createPattern(className[0], IJavaSearchConstants.CLASS,
                        IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH);

                engine.search(pattern, new SearchParticipant[] { participant }, scope, requestor, monitor);
                if (monitor.isCanceled()) {
                    throw new InterruptedException();
                }
                if (requestor.matches.size() > 0) {
                    found = (IType) requestor.matches.get(0).getElement();
                    if (found.getFullyQualifiedName().equals(classSignature)) {
                        for (SearchMatch match : requestor.matches) {
                            IType temp = (IType) match.getElement();
                            if (temp.getTypeRoot() instanceof ICompilationUnit) {
                                //prefer source types.
                                found = temp;
                                break;
                            }
                        }
                        if (cachedTypes.size() > 100) {
                            cachedTypes.clear();
                        }
                        cachedTypes.put(className[0], found);
                    } else {
                        found = null;
                    }
                }
            }
            if (found == null)
                return null;
            StringBuilder childTypeName = new StringBuilder();
            childTypeName.append(className[0]);
            //check each of the indexes for the sub-types
            for (int i = 1; i < className.length; i++) {
                childTypeName.append('$');
                childTypeName.append(className[i]);
                IType parent = found;
                found = cachedTypes.get(childTypeName.toString());

                if (found == null) {
                    boolean isAnonymous = false;
                    Integer occurrenceCount = -1;
                    try {
                        occurrenceCount = Integer.parseInt(className[i]);
                        isAnonymous = true;
                    } catch (NumberFormatException e) {
                        isAnonymous = false;
                    }
                    if (isAnonymous) {
                        if (!parent.isBinary()) {
                            found = parent.getType("", occurrenceCount);
                            if (found != null) {
                                if (found.exists()) {
                                    cachedTypes.put(childTypeName.toString(), found);
                                    continue;
                                } else {
                                    found = null;
                                }
                            }
                        } else {
                            //if it is a binary type, there is no hope for 
                            //finding an anonymous inner class. Use the number
                            //as the type name, and cache the handle.
                            found = parent.getType(className[i]);
                            cachedTypes.put(childTypeName.toString(), found);
                            continue;
                        }
                    }
                    ArrayList<IType> childTypes = new ArrayList<IType>();
                    LinkedList<IJavaElement> children = new LinkedList<IJavaElement>();
                    children.addAll(Arrays.asList(parent.getChildren()));
                    while (children.size() > 0) {
                        IJavaElement child = children.removeFirst();
                        if (child instanceof IType) {
                            childTypes.add((IType) child);
                        } else if (child instanceof IMember) {
                            children.addAll(Arrays.asList(((IMember) child).getChildren()));
                        }
                    }
                    int numIndex = 0;
                    while (numIndex < className[i].length()
                            && Character.isDigit(className[i].charAt(numIndex))) {
                        numIndex++;
                    }
                    String name = className[i];
                    try {
                        //get a number at the beginning to find out if 
                        //there is an occurrence count
                        if (numIndex <= name.length()) {
                            occurrenceCount = parseInt(name.substring(0, numIndex));
                            if (occurrenceCount == null) {
                                occurrenceCount = 1;
                            }
                            if (numIndex < name.length() - 1) {
                                name = name.substring(numIndex);
                            } else {
                                name = "";
                            }
                        }
                        for (IType childType : childTypes) {
                            if ("".equals(name)) {
                                if (childType.getOccurrenceCount() == occurrenceCount) {
                                    found = childType;
                                    break;
                                }
                            } else {
                                if (name.equals(childType.getElementName())
                                        && childType.getOccurrenceCount() == occurrenceCount) {
                                    found = childType;
                                    break;
                                }
                            }
                        }
                        if (found == null) {
                            if ("".equals(name)) {
                                found = parent.getTypeRoot().getJavaProject().findType(classSignature);
                                //found = parent.getType("" + occurrenceCount);
                            } else {
                                found = parent.getType(name, occurrenceCount);
                            }
                        }
                        cachedTypes.put(childTypeName.toString(), found);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }
            return found;
        }
    } catch (Exception e) {
        SketchPlugin.getDefault().log(e);
        return null;
    }
}

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

License:Open Source License

public static String getFullyQualifiedName(IType type, boolean includeOccurrenceCount) {
    IType declaring = type;
    String qualifiedName = "";
    try {//  ww  w .  ja  v  a  2s .  c o m
        while (declaring != null) {
            if (declaring.isAnonymous()) {
                qualifiedName = declaring.getOccurrenceCount() + "" + qualifiedName;
            } else {
                qualifiedName = declaring.getElementName() + qualifiedName;
                if (includeOccurrenceCount) {
                    IJavaElement parent = declaring.getParent();
                    if (parent instanceof IMember && !(parent instanceof IType)) {
                        qualifiedName = type.getOccurrenceCount() + qualifiedName;
                    }
                }
            }
            IJavaElement parent = declaring.getParent();
            while (parent != null) {
                if (parent instanceof IType) {
                    declaring = (IType) parent;
                    break;
                }
                parent = parent.getParent();
            }
            if (parent != null) {
                qualifiedName = "$" + qualifiedName;
            } else {
                declaring = null;
            }
        }
    } catch (JavaModelException e) {
        return type.getFullyQualifiedName();
    }
    String pack = type.getPackageFragment().getElementName();
    if (!"".equals(pack))
        pack = pack + ".";
    qualifiedName = pack + qualifiedName;
    return qualifiedName;
}

From source file:ca.uvic.chisel.javasketch.ui.internal.presentation.TraceThreadLabelProvider.java

License:Open Source License

@Override
public String getText(Object element) {
    try {// w ww  . j  a v  a2 s  .  com
        IJavaElement je = null;

        if (element instanceof ICall) {
            IMessage target = ((ICall) element).getTarget();
            if (target != null) {
                je = JavaSearchUtils.findElement(target.getActivation(), new NullProgressMonitor());
            }
        } else if (element instanceof IThrow) {
            return "Exception";
        } else if (element instanceof IReply) {
            String sig = ((IReply) element).getActivation().getMethod().getSignature();
            return Signature.getSimpleName(Signature.toString(Signature.getReturnType(sig)));
        } else if (element instanceof ITraceModel) {
            je = JavaSearchUtils.findElement((ITraceModel) element, new NullProgressMonitor());
        }
        if (je != null) {
            if (je instanceof IType) {
                IType type = (IType) je;
                //qualify with all the parent type names
                String name = type.getElementName();

                if (type.isAnonymous()) {
                    name = type.getTypeQualifiedName();
                } else if (type.getOccurrenceCount() > 1) {
                    name = type.getOccurrenceCount() + name;
                }
                IJavaElement parent = type.getParent();
                while (parent != null) {
                    if (parent instanceof IType) {
                        IType pt = (IType) parent;
                        int occurrence = pt.getOccurrenceCount();
                        if (pt.isAnonymous()) {
                            name = occurrence + "$" + name;
                        } else {
                            name = ((occurrence > 1) ? occurrence : "") + pt.getElementName() + "$" + name;
                        }
                    }
                    parent = parent.getParent();
                }
                return name;
            }
            return workbenchLabelProvider.getText(je);
        } else if (element instanceof ITraceModel) {
            return uresolvedModelElement((ITraceModel) element);
        }
    } catch (InterruptedException e) {
    } catch (CoreException e) {
    } catch (Exception e) {
    }
    return (element != null) ? element.toString() : "";
}

From source file:org.eclipse.mat.jdt.OpenSourceFileJob.java

License:Open Source License

private IType resolveInnerTypes(IType match) throws JavaModelException {
    if (this.innerTypes == null)
        return match;

    for (final Object innerType : innerTypes) {
        Stack<IJavaElement> stack = new Stack<IJavaElement>();
        stack.push(match);//from w  w  w.  j  a va2  s  .c  o  m

        while (!stack.isEmpty()) {
            IJavaElement subject = stack.pop();

            if (subject instanceof IType) {
                IType type = (IType) subject;
                if (innerType instanceof Integer) {
                    if (type.isAnonymous() && type.getOccurrenceCount() == (Integer) innerType) {
                        match = type;
                        break;
                    }
                } else {
                    if (innerType.equals(type.getElementName())) {
                        match = type;
                        break;
                    }
                }
            }

            if (subject instanceof IParent)
                for (IJavaElement child : ((IParent) subject).getChildren())
                    stack.push(child);
        }
    }

    return match;
}