Example usage for org.eclipse.jdt.core IJavaElement isReadOnly

List of usage examples for org.eclipse.jdt.core IJavaElement isReadOnly

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaElement isReadOnly.

Prototype

boolean isReadOnly();

Source Link

Document

Returns whether this Java element is read-only.

Usage

From source file:cfgrecognition.loader.SootClassLoader.java

License:Open Source License

protected void dfsDomTree(IJavaElement element) throws Exception {
    //      if (monitor.isCanceled()) {
    //         return;
    //      }//from  ww w  .  j a va2  s. c  om

    if (element.isReadOnly())
        return;

    int elementType = element.getElementType();
    if (elementType == IJavaElement.COMPILATION_UNIT) {
        ICompilationUnit cu = (ICompilationUnit) element;
        IType[] allTypes = cu.getTypes();
        for (IType aType : allTypes) {
            dfsDomTree(aType);
        }
    } else if (elementType == IJavaElement.TYPE) {
        IType aType = (IType) element;

        if (aType.isClass()) {
            // Load a type in Soot
            load(aType.getFullyQualifiedName());
        }

        // Go inside the methods to look for Anonymous Inner Class
        for (IMethod m : aType.getMethods()) {
            IJavaElement[] elems = m.getChildren();
            for (IJavaElement elem : elems) {
                if (elem.getElementType() == IJavaElement.TYPE) {
                    dfsDomTree(elem);
                }
            }
        }

        // For inner classes
        IType[] allTypes = aType.getTypes();
        for (IType childType : allTypes) {
            dfsDomTree(childType);
        }
    }

}

From source file:com.aqua.wikiwizard.WikiExportModel.java

License:Apache License

private void collectAllTypesInProject(ArrayList<IType> allTypes, IParent element) throws JavaModelException {
    IJavaElement[] elements = element.getChildren();

    for (IJavaElement je : elements) {
        // Skip the jars
        if (je.isReadOnly()) {
            continue;
        }/*from   w ww. ja  va  2  s.c o  m*/

        // End of the road, we found a type
        if (je instanceof IType) {
            allTypes.add((IType) je);
            continue;
        }

        // There are more, enter recursion
        if (je instanceof IParent) {
            collectAllTypesInProject(allTypes, (IParent) je);
        }
    }
}

From source file:edu.buffalo.cse.green.editor.DiagramEditor.java

License:Open Source License

/**
 * Opens a blank editor./*from  w  w w . j a  v a 2 s .c om*/
 * 
 * @param element - The element to place the diagram file in.
 * @return a reference to the opened editor, if successful.
 */
private static DiagramEditor createEditor(IJavaElement element) throws JavaModelException {
    IWorkbenchWindow dwindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    IWorkbenchPage workbenchPage = dwindow.getActivePage();
    IPackageFragment packFrag = null;
    IPath elementPath;

    // get the project itself, if desired (for creating DIA)
    if (element.isReadOnly() || PlugIn.getBooleanPreference(P_FORCE_DIA_IN_PROJECT)) {
        element = element.getAncestor(IJavaElement.JAVA_PROJECT);
    }

    if (element instanceof IJavaProject) {
        IJavaProject project = (IJavaProject) element;
        packFrag = project.getPackageFragments()[0];
    } else if (!(element instanceof IPackageFragment)) {
        packFrag = (IPackageFragment) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
    } else {
        packFrag = (IPackageFragment) element;
    }

    // create a path to the diagram file with current extension
    elementPath = packFrag.getPath()
            .append(packFrag.getJavaProject().getElementName() + "." + PluginConstants.GREEN_EXTENSION);

    try {
        IFile diaFile = DiagramEditor.getFileNotExist(element.getJavaProject().getProject(), elementPath);
        DiagramEditor editor = (DiagramEditor) IDE.openEditor(workbenchPage, diaFile, true);
        ACTIVE_EDITOR = editor;
        return editor;
    } catch (CoreException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:edu.buffalo.cse.green.editor.DiagramEditor.java

License:Open Source License

/**
 * Maps an element to its corresponding <code>CompilationUnit</code>.
 * //  ww w .  j  a  v a 2  s  . c o  m
 * @param element - The element.
 * @param cu - Its corresponding <code>CompilationUnit</code>.
 */
public void put(IJavaElement element, CompilationUnit cu) {
    String id = element.getHandleIdentifier();

    _map.put(id, cu);

    if (!(element.isReadOnly())) {
        _cuModMap.put(id, element.getResource().getModificationStamp());
    }
}

From source file:edu.clarkson.serl.critic.loader.SootClassLoader.java

License:Open Source License

protected void dfsDomTree(IJavaElement element, IProgressMonitor monitor) throws Exception {
    if (monitor.isCanceled()) {
        return;//from   w ww  .  j a v  a  2 s.c  o  m
    }

    if (element.isReadOnly())
        return;

    int elementType = element.getElementType();
    if (elementType == IJavaElement.COMPILATION_UNIT) {
        ICompilationUnit cu = (ICompilationUnit) element;
        IType[] allTypes = cu.getTypes();
        for (IType aType : allTypes) {
            dfsDomTree(aType, monitor);
        }
    } else if (elementType == IJavaElement.TYPE) {
        IType aType = (IType) element;

        if (aType.isClass()) {
            // Load a type in Soot
            load(aType.getFullyQualifiedName());
        }

        // Go inside the methods to look for Anonymous Inner Class
        for (IMethod m : aType.getMethods()) {
            IJavaElement[] elems = m.getChildren();
            for (IJavaElement elem : elems) {
                if (elem.getElementType() == IJavaElement.TYPE) {
                    dfsDomTree(elem, monitor);
                }
            }
        }

        // For inner classes
        IType[] allTypes = aType.getTypes();
        for (IType childType : allTypes) {
            dfsDomTree(childType, monitor);
        }
    }

}

From source file:edu.cmu.cs.crystal.internal.WorkspaceUtilities.java

License:Open Source License

/**
 * A recursive traversal of the IJavaModel starting from the given
 * element to collect all ICompilationUnits.
 * Each compilation unit corresponds to each java file.
 *  /*w  w w . ja va  2 s.com*/
 * @param javaElement a node in the IJavaModel that will be traversed
 * @return a list of compilation units or <code>null</code> if no comp units are found
 */
public static List<ICompilationUnit> collectCompilationUnits(IJavaElement javaElement) {
    List<ICompilationUnit> list = null, temp = null;
    // We are traversing the JavaModel for COMPILATION_UNITs
    if (javaElement.getElementType() == IJavaElement.COMPILATION_UNIT) {
        list = new ArrayList<ICompilationUnit>();
        list.add((ICompilationUnit) javaElement);
        return list;
    }

    // Non COMPILATION_UNITs will have to be further traversed
    if (javaElement instanceof IParent) {
        IParent parent = (IParent) javaElement;

        // Do not traverse PACKAGE_FRAGMENT_ROOTs that are ReadOnly
        // this ignores libraries and .class files
        if (javaElement.getElementType() == IJavaElement.PACKAGE_FRAGMENT_ROOT && javaElement.isReadOnly()) {
            return null;
        }

        // Traverse
        try {
            if (parent.hasChildren()) {
                IJavaElement[] children = parent.getChildren();
                for (int i = 0; i < children.length; i++) {
                    temp = collectCompilationUnits(children[i]);
                    if (temp != null)
                        if (list == null)
                            list = temp;
                        else
                            list.addAll(temp);
                }
            }
        } catch (JavaModelException jme) {
            log.log(Level.SEVERE, "Problem traversing Java model element: " + parent, jme);
        }
    } else {
        log.warning("Encountered a model element that's not a comp unit or parent: " + javaElement);
    }

    return list;
}

From source file:org.codehaus.groovy.eclipse.codebrowsing.requestor.CodeSelectRequestor.java

License:Apache License

/**
 * Converts the maybeRequested element into a resolved element by creating a unique key for it.
 *///from   ww  w  .  j  a  v  a2  s  .  c  o  m
private IJavaElement resolveRequestedElement(TypeLookupResult result, IJavaElement maybeRequested) {
    AnnotatedNode declaration = (AnnotatedNode) result.declaration;
    if (declaration instanceof PropertyNode && maybeRequested instanceof IMethod) {
        // the field associated with this property does not exist, use the method instead
        String getterName = maybeRequested.getElementName();
        MethodNode maybeDeclaration = (MethodNode) declaration.getDeclaringClass().getMethods(getterName)
                .get(0);
        declaration = maybeDeclaration == null ? declaration : maybeDeclaration;
    }

    String uniqueKey = createUniqueKey(declaration, result.type, result.declaringType, maybeRequested);
    IJavaElement candidate;

    // Create the Groovy Resolved Element, which is like a resolved element, but contains extraDoc, as
    // well as the inferred declaration (which may not be the same as the actual declaration)
    switch (maybeRequested.getElementType()) {
    case IJavaElement.FIELD:
        if (maybeRequested.isReadOnly()) {
            candidate = new GroovyResolvedBinaryField((JavaElement) maybeRequested.getParent(),
                    maybeRequested.getElementName(), uniqueKey, result.extraDoc, result.declaration);
        } else {
            candidate = new GroovyResolvedSourceField((JavaElement) maybeRequested.getParent(),
                    maybeRequested.getElementName(), uniqueKey, result.extraDoc, result.declaration);
        }
        break;
    case IJavaElement.METHOD:
        if (maybeRequested.isReadOnly()) {
            candidate = new GroovyResolvedBinaryMethod((JavaElement) maybeRequested.getParent(),
                    maybeRequested.getElementName(), ((IMethod) maybeRequested).getParameterTypes(), uniqueKey,
                    result.extraDoc, result.declaration);
        } else {
            candidate = new GroovyResolvedSourceMethod((JavaElement) maybeRequested.getParent(),
                    maybeRequested.getElementName(), ((IMethod) maybeRequested).getParameterTypes(), uniqueKey,
                    result.extraDoc, result.declaration);
        }
        break;
    case IJavaElement.TYPE:
        if (maybeRequested.isReadOnly()) {
            candidate = new GroovyResolvedBinaryType((JavaElement) maybeRequested.getParent(),
                    maybeRequested.getElementName(), uniqueKey, result.extraDoc, result.declaration);
        } else {
            candidate = new GroovyResolvedSourceType((JavaElement) maybeRequested.getParent(),
                    maybeRequested.getElementName(), uniqueKey, result.extraDoc, result.declaration);
        }
        break;
    default:
        candidate = maybeRequested;
    }
    requestedElement = candidate;
    return requestedElement;
}

From source file:org.codehaus.jdt.groovy.model.GroovyClassFileWorkingCopy.java

License:Open Source License

/**
 * Translates from the source element of this synthetic compilation unit into a binary element of the underlying classfile.
 * //  www. ja  va2  s.  com
 * @param source the source element to translate
 * @return the same element, but in binary form, or closest possible match if this element doesn't exist
 */
public IJavaElement convertToBinary(IJavaElement source) {
    if (source.isReadOnly()) {
        // already binary
        return source;
    }
    if (source.getElementType() == IJavaElement.COMPILATION_UNIT) {
        return classFile;
    }
    if (!(source instanceof IMember)) {
        return classFile;
    }

    // get ancestors to type root
    List<IJavaElement> srcAncestors = new ArrayList<IJavaElement>(3);
    IJavaElement srcCandidate = source;
    while (srcCandidate != null && srcCandidate != this) {
        srcAncestors.add(srcCandidate);
        srcCandidate = srcCandidate.getParent();
    }

    // now, traverse the classFile using the ancestor list in reverse order
    IJavaElement binCandidate = classFile;
    try {
        while (srcAncestors.size() > 0) {
            srcCandidate = srcAncestors.remove(srcAncestors.size() - 1);
            if (!(srcCandidate instanceof IParent)) {
                break;
            }

            String candidateName = srcCandidate.getElementName();
            IJavaElement[] binChildren = ((IParent) binCandidate).getChildren();
            boolean found = false;
            for (IJavaElement binChild : binChildren) {
                if (binChild.getElementName().equals(candidateName) ||
                // check for implicit closure class
                        (binChild.getElementType() == IJavaElement.TYPE && binChild.getParent().getElementName()
                                .equals(candidateName + '$' + binChild.getElementName() + ".class"))) {
                    binCandidate = binChild;
                    found = true;
                    break;
                }
            }
            if (!found) {
                break;
            }
        }
    } catch (JavaModelException e) {
        Util.log(e);
    }

    return binCandidate;
}

From source file:org.eclipse.ajdt.core.model.AJProjectModelFacade.java

License:Open Source License

/**
 * @return a program element that corresponds to the given java element.
 *//*from  w  w  w  .j a v  a 2  s  .  co  m*/
public IProgramElement javaElementToProgramElement(IJavaElement je) {
    if (!isInitialized) {
        return IHierarchy.NO_STRUCTURE;
    }
    String ajHandle = je.getHandleIdentifier();

    boolean isBinary = false;
    if (isBinaryHandle(ajHandle) || je.isReadOnly()) {
        ajHandle = convertToAspectJBinaryHandle(ajHandle, false);
        isBinary = true;
    } else if (isFromExternalProject(je)) {
        ajHandle = convertToAspectJBinaryHandle(ajHandle, true);
        isBinary = true;
    }

    // check to see if we need to replace { (compilation unit) with * (aj compilation unit)
    // if using cuprovider, then aj compilation units have {, but needs to change to *
    ICompilationUnit cu = null;
    if (je instanceof IMember) {
        cu = ((IMember) je).getCompilationUnit();
    } else if (je instanceof IPackageDeclaration) {
        IJavaElement parent = ((IPackageDeclaration) je).getParent();
        if (parent instanceof ICompilationUnit) {
            cu = (ICompilationUnit) parent;
        }
    } else if (je instanceof AJCodeElement) {
        cu = ((AJCodeElement) je).getCompilationUnit();
        // get the occurence count 
        int count = ((AJCodeElement) je).occurrenceCount;
        // need the first bang after the last close paren
        int lastParen = ajHandle.lastIndexOf(')');
        int firstBang = ajHandle.indexOf(JavaElement.JEM_COUNT, lastParen);
        if (firstBang > -1) {
            ajHandle = ajHandle.substring(0, firstBang);
            if (count > 1) {
                // there is more than one element
                // with this name
                ajHandle += "" + JavaElement.JEM_COUNT + count;
            }
        }

    } else if (je instanceof ILocalVariable) {
        IOpenable openable = ((ILocalVariable) je).getOpenable();
        cu = openable instanceof ICompilationUnit ? (ICompilationUnit) openable : null;
    } else if (je instanceof ImportDeclaration) {
        cu = ((ImportDeclaration) je).getCompilationUnit();
    } else if (je instanceof ImportContainer) {
        cu = ((ImportContainer) je).getCompilationUnit();
    } else if (je instanceof ICompilationUnit) {
        cu = (ICompilationUnit) je;
    }
    if (cu != null) {
        IResource resource = cu.getResource();
        if (resource != null && resource.exists()
                && CoreUtils.ASPECTJ_SOURCE_ONLY_FILTER.accept(resource.getName())) {
            ajHandle = ajHandle.replaceFirst("" + JavaElement.JEM_ESCAPE + JavaElement.JEM_COMPILATIONUNIT,
                    Character.toString(AspectElement.JEM_ASPECT_CU));
        }
    }

    IProgramElement ipe = structureModel.findElementForHandleOrCreate(ajHandle, false);
    if (ipe == null) {
        if (isBinary) {
            // might be an aspect in a class file.  JDT doesn't know it is an aspect
            // try looking for handle again, but use an Aspect token
            // problem will be if this is an aspect contained in a class or vice versa
            ajHandle = ajHandle.replace(JavaElement.JEM_TYPE, AspectElement.JEM_ASPECT_TYPE);
            ipe = structureModel.findElementForHandleOrCreate(ajHandle, false);
        }
        if (ipe == null) {
            // occurs when the handles are not working properly
            return IHierarchy.NO_STRUCTURE;
        }
    }
    return ipe;
}

From source file:org.eclipse.jdt.internal.core.CopyResourceElementsOperation.java

License:Open Source License

/**
 * @see MultiOperation/*  w  ww  .  j a v  a 2s .co m*/
 */
protected void verify(IJavaElement element) throws JavaModelException {
    if (element == null || !element.exists())
        error(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, element);

    if (element.isReadOnly() && (isRename() || isMove()))
        error(IJavaModelStatusConstants.READ_ONLY, element);

    IResource resource = ((JavaElement) element).resource();
    if (resource instanceof IFolder) {
        if (resource.isLinked()) {
            error(IJavaModelStatusConstants.INVALID_RESOURCE, element);
        }
    }

    int elementType = element.getElementType();

    if (elementType == IJavaElement.COMPILATION_UNIT) {
        org.eclipse.jdt.internal.core.CompilationUnit compilationUnit = (org.eclipse.jdt.internal.core.CompilationUnit) element;
        if (isMove() && compilationUnit.isWorkingCopy() && !compilationUnit.isPrimary())
            error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element);
    } else if (elementType != IJavaElement.PACKAGE_FRAGMENT) {
        error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element);
    }

    JavaElement dest = (JavaElement) getDestinationParent(element);
    verifyDestination(element, dest);
    if (this.renamings != null) {
        verifyRenaming(element);
    }
}