Example usage for org.eclipse.jdt.internal.core PackageFragment getHandleFromMemento

List of usage examples for org.eclipse.jdt.internal.core PackageFragment getHandleFromMemento

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core PackageFragment getHandleFromMemento.

Prototype

@Override
    public IJavaElement getHandleFromMemento(String token, MementoTokenizer memento, WorkingCopyOwner owner) 

Source Link

Usage

From source file:org.eclipse.ajdt.core.AspectJCore.java

License:Open Source License

public static IJavaElement create(String handleIdentifier, WorkingCopyOwner owner) {

    if (handleIdentifier == null) {
        return null;
    }//from www  .  ja v a  2s.  c  om
    Map aspectsInJavaFiles = new HashMap();

    boolean isCodeElement = false;
    String codeElementHandle = ""; //$NON-NLS-1$

    int codeElementDelimPos = indexOfIgnoringEscapes(handleIdentifier, AspectElement.JEM_CODEELEMENT);
    if (codeElementDelimPos != -1) {
        isCodeElement = true;
        codeElementHandle = handleIdentifier.substring(codeElementDelimPos + 1);
        handleIdentifier = handleIdentifier.substring(0, codeElementDelimPos);
    }

    AJMementoTokenizer memento = new AJMementoTokenizer(handleIdentifier);
    while (memento.hasMoreTokens()) {
        String token = memento.nextToken();
        if ((token.charAt(0) == AspectElement.JEM_ASPECT_CU)
                || (token.charAt(0) == JavaElement.JEM_COMPILATIONUNIT)
                || (token.charAt(0) == JavaElement.JEM_CLASSFILE)) {

            int index;
            if (token.charAt(0) == AspectElement.JEM_ASPECT_CU) {
                index = handleIdentifier.indexOf(AspectElement.JEM_ASPECT_CU);
            } else if (token.charAt(0) == JavaElement.JEM_COMPILATIONUNIT) {
                index = handleIdentifier.indexOf(JavaElement.JEM_COMPILATIONUNIT);
            } else { // JEM_CLASSFILE 
                index = handleIdentifier.indexOf(JavaElement.JEM_CLASSFILE);
            }

            if (index != -1) {
                IJavaElement je = JavaCore.create(handleIdentifier.substring(0, index));
                if (je instanceof PackageFragment) {
                    PackageFragment pf = (PackageFragment) je;
                    String cuName = handleIdentifier.substring(index + 1);
                    int ind1 = cuName.indexOf(JavaElement.JEM_TYPE);
                    if (ind1 != -1) {
                        cuName = cuName.substring(0, ind1);
                    }
                    int ind2 = cuName.indexOf(AspectElement.JEM_ASPECT_TYPE);
                    if (ind2 != -1) {
                        cuName = cuName.substring(0, ind2);
                    }
                    int ind3 = cuName.indexOf(AspectElement.JEM_ITD_METHOD);
                    if (ind3 != -1) {
                        cuName = cuName.substring(0, ind3);
                    }
                    ind3 = cuName.indexOf(AspectElement.JEM_ITD_FIELD);
                    if (ind3 != -1) {
                        cuName = cuName.substring(0, ind3);
                    }
                    int ind4 = cuName.indexOf(AspectElement.JEM_DECLARE);
                    if (ind4 != -1) {
                        cuName = cuName.substring(0, ind4);
                    }
                    int ind5 = cuName.indexOf(AspectElement.JEM_IMPORTDECLARATION);
                    if (ind5 != -1) {
                        cuName = cuName.substring(0, ind5);
                    }
                    int ind6 = cuName.indexOf(AspectElement.JEM_PACKAGEDECLARATION);
                    if (ind6 != -1) {
                        cuName = cuName.substring(0, ind6);
                    }
                    if (CoreUtils.ASPECTJ_SOURCE_ONLY_FILTER.accept(cuName)) {
                        // no need to use a cuprovider because we know this
                        // is an AJCompilationUnit
                        JavaElement cu = new AJCompilationUnit(pf, cuName, owner);
                        token = memento.nextToken();
                        if (!memento.hasMoreTokens()) {
                            return cu;
                        }
                        IJavaElement restEl = cu.getHandleFromMemento(memento.nextToken(), memento, owner);
                        if (restEl != null) {
                            if (isCodeElement) {
                                // there was an AJCodeElement at the end of
                                // the handle
                                IJavaElement codeEl = getCodeElement(codeElementHandle, (JavaElement) restEl);
                                if (codeEl != null) {
                                    return codeEl;
                                }
                            }
                            return restEl;
                        }
                    } else {
                        // Use the default working copy owner for Java elements
                        IJavaElement restEl = pf.getHandleFromMemento(token, memento,
                                DefaultWorkingCopyOwner.PRIMARY);
                        if (restEl != null) {
                            if (isCodeElement) {
                                // there was an AJCodeElement at the end of
                                // the handle
                                IJavaElement codeEl = getCodeElement(codeElementHandle, (JavaElement) restEl);
                                if (codeEl != null) {
                                    return codeEl;
                                }
                            }
                            return restEl;
                        } else if (ind2 != -1) { // An aspect in a .java file...
                            int index3 = handleIdentifier.indexOf(AspectElement.JEM_ASPECT_TYPE);
                            String aspectName = handleIdentifier.substring(index3 + 1);
                            boolean identifierIsAspect = true;
                            int ind7 = aspectName.indexOf(AspectElement.JEM_DECLARE);
                            if (ind7 != -1) {
                                aspectName = aspectName.substring(0, ind7);
                                identifierIsAspect = false;
                            }
                            int ind8 = aspectName.indexOf(AspectElement.JEM_ADVICE);
                            if (ind8 != -1) {
                                aspectName = aspectName.substring(0, ind8);
                                identifierIsAspect = false;
                            }
                            int ind9 = aspectName.indexOf(AspectElement.JEM_ITD_METHOD);
                            if (ind9 != -1) {
                                aspectName = aspectName.substring(0, ind9);
                                identifierIsAspect = false;
                            }
                            ind9 = aspectName.indexOf(AspectElement.JEM_ITD_FIELD);
                            if (ind9 != -1) {
                                aspectName = aspectName.substring(0, ind9);
                                identifierIsAspect = false;
                            }
                            int ind10 = aspectName.indexOf(AspectElement.JEM_ASPECT_TYPE);
                            if (ind10 != -1) {
                                aspectName = aspectName.substring(0, ind10);
                                identifierIsAspect = false;
                            }
                            int ind11 = aspectName.indexOf(AspectElement.JEM_TYPE);
                            if (ind11 != -1) {
                                aspectName = aspectName.substring(0, ind11);
                                identifierIsAspect = false;
                            }
                            int ind12 = aspectName.indexOf(AspectElement.JEM_FIELD);
                            if (ind12 != -1) {
                                aspectName = aspectName.substring(0, ind12);
                                identifierIsAspect = false;
                            }
                            int ind13 = aspectName.indexOf(AspectElement.JEM_METHOD);
                            if (ind13 != -1) {
                                aspectName = aspectName.substring(0, ind13);
                                identifierIsAspect = false;
                            }
                            int ind14 = aspectName.indexOf(AspectElement.JEM_POINTCUT);
                            if (ind14 != -1) {
                                aspectName = aspectName.substring(0, ind14);
                                identifierIsAspect = false;
                            }
                            IOpenable openable;
                            if (cuName.endsWith(".class")) {
                                openable = pf.getClassFile(cuName);
                            } else {
                                openable = pf.getCompilationUnit(cuName);
                            }
                            List l;
                            if (aspectsInJavaFiles.get(openable) instanceof List) {
                                l = (List) aspectsInJavaFiles.get(openable);
                            } else {
                                l = new ArrayList();
                                aspectsInJavaFiles.put(openable, l);
                            }
                            JavaElement aspectEl = null;
                            for (Iterator iter = l.iterator(); iter.hasNext();) {
                                AspectElement element = (AspectElement) iter.next();
                                if (element.getElementName().equals(aspectName)) {
                                    aspectEl = element;
                                }
                            }
                            if (aspectEl == null) {
                                if (openable instanceof ClassFile) {
                                    ClassFile cOpenable = (ClassFile) openable;
                                    aspectEl = new BinaryAspectElement((JavaElement) cOpenable, aspectName);
                                } else {
                                    aspectEl = new AspectElement((JavaElement) openable, aspectName);
                                }
                                l.add(aspectEl);
                            }
                            int afterAspectIndex = index3 + aspectName.length() + 1;

                            if (identifierIsAspect) {
                                return aspectEl;
                            } else {
                                memento.setIndexTo(afterAspectIndex);
                                return aspectEl.getHandleFromMemento(memento.nextToken(), memento, owner);
                            }
                        }
                    }
                }
            }
        }
    }
    // XXX can we get here???
    if (isCodeElement) {
        // an injar aspect with no parent
        return new AJInjarElement(codeElementHandle);
    }
    return JavaCore.create(handleIdentifier);
}