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

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

Introduction

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

Prototype

int CLASS_FILE

To view the source code for org.eclipse.jdt.core IJavaElement CLASS_FILE.

Click Source Link

Document

Constant representing a class file.

Usage

From source file:org.apache.felix.sigil.eclipse.model.util.JavaHelper.java

License:Apache License

private static void findIndirectReferences(Set<String> indirect, String pckg, IParent parent, IJavaProject p)
        throws JavaModelException {
    for (IJavaElement e : parent.getChildren()) {
        boolean skip = false;
        switch (e.getElementType()) {
        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            IPackageFragmentRoot rt = (IPackageFragmentRoot) e;
            IClasspathEntry ce = rt.getRawClasspathEntry();
            IPath path = ce.getPath();/*from   w  w  w.  j a v a  2  s.  c  o m*/
            skip = "org.eclipse.jdt.launching.JRE_CONTAINER".equals(path.toString());
            break;
        case IJavaElement.CLASS_FILE:
            IClassFile cf = (IClassFile) e;
            if (cf.getElementName().startsWith(pckg)) {
                findIndirectReferences(indirect, findPackage(cf.getType().getSuperclassName()), p, p);
            }
            break;
        case IJavaElement.COMPILATION_UNIT:
            ICompilationUnit cu = (ICompilationUnit) e;
            break;
        }

        if (!skip && e instanceof IParent) {
            IParent newParent = (IParent) e;
            findIndirectReferences(indirect, pckg, newParent, p);
        }
    }
}

From source file:org.apache.felix.sigil.eclipse.ui.util.ResourcesDialogHelper.java

License:Apache License

public static BackgroundLoadingSelectionDialog<String> createClassSelectDialog(Shell shell, String title,
        final ISigilProjectModel project, String selected, final String ifaceOrParentClass) {
    final BackgroundLoadingSelectionDialog<String> dialog = new BackgroundLoadingSelectionDialog<String>(shell,
            "Class Name", true);

    IJobRunnable job = new IJobRunnable() {
        public IStatus run(IProgressMonitor monitor) {
            try {
                for (IJavaElement e : JavaHelper.findTypes(project.getJavaModel(),
                        IJavaElement.PACKAGE_FRAGMENT)) {
                    IPackageFragment root = (IPackageFragment) e;
                    if (project.isInBundleClasspath(root)) {
                        for (IJavaElement e1 : JavaHelper.findTypes(root, IJavaElement.COMPILATION_UNIT,
                                IJavaElement.CLASS_FILE)) {
                            ITypeRoot typeRoot = (ITypeRoot) e1;
                            IType type = (IType) JavaHelper.findType(typeRoot, IJavaElement.TYPE);
                            if (JavaHelper.isAssignableTo(ifaceOrParentClass, type)) {
                                dialog.addElement(type.getFullyQualifiedName());
                            }/*from w w  w .j a  v  a2  s  . c om*/
                        }
                    }
                }

                return Status.OK_STATUS;
            } catch (JavaModelException e) {
                return e.getStatus();
            }
        }

    };

    dialog.addBackgroundJob("Scanning for activators in project", job);

    return dialog;
}

From source file:org.apache.felix.sigil.ui.eclipse.ui.util.ResourcesDialogHelper.java

License:Apache License

public static BackgroundLoadingSelectionDialog<String> createClassSelectDialog(Shell shell, String title,
        final ISigilProjectModel project, String selected, final String ifaceOrParentClass) {
    final BackgroundLoadingSelectionDialog<String> dialog = new BackgroundLoadingSelectionDialog<String>(shell,
            "Class Name", true);

    IJobRunnable job = new IJobRunnable() {
        public IStatus run(IProgressMonitor monitor) {
            try {
                for (IJavaElement e : JavaHelper.findTypes(project.getJavaModel(),
                        IJavaElement.PACKAGE_FRAGMENT_ROOT)) {
                    IPackageFragmentRoot root = (IPackageFragmentRoot) e;
                    if (project.isInBundleClasspath(root)) {
                        for (IJavaElement e1 : JavaHelper.findTypes(root, IJavaElement.COMPILATION_UNIT,
                                IJavaElement.CLASS_FILE)) {
                            ITypeRoot typeRoot = (ITypeRoot) e1;
                            IType type = (IType) JavaHelper.findType(typeRoot, IJavaElement.TYPE);
                            if (JavaHelper.isAssignableTo(ifaceOrParentClass, type)) {
                                dialog.addElement(type.getFullyQualifiedName());
                            }/*from www .  j  a va  2 s  .  c  o m*/
                        }
                    }
                }

                return Status.OK_STATUS;
            } catch (JavaModelException e) {
                return e.getStatus();
            }
        }

    };

    dialog.addBackgroundJob("Scanning for activators in project", job);

    return dialog;
}

From source file:org.apache.tapestrytools.ui.internal.tcc.editor.PackagesPart.java

License:Open Source License

private void collectCustomComponents(IProject project) {
    List<ComponentInstance> componentList = new ArrayList<ComponentInstance>();
    try {/*from  ww w  .  ja v a2  s  .com*/
        IPackageFragmentRoot[] roots = JavaCore.create(project).getAllPackageFragmentRoots();
        for (int i = 0; i < roots.length; i++) {
            IPackageFragmentRoot root = roots[i];
            String srcPath = root.getElementName();
            for (ComponentPackage pi : model.getCustomPackageList()) {
                if (pi.isArchive() == root.isArchive() && srcPath.equals(pi.getFragmentRoot())) {
                    if (!root.isArchive()) {
                        //Load custom components from source directory
                        String wholePath = "/" + srcPath + "/" + pi.getPath().replace(".", "/");
                        IFolder folder = project.getFolder(wholePath);
                        if (folder.exists()) {
                            IResource[] fileList = folder.members();
                            for (IResource file : fileList) {
                                String fullPath = file.getFullPath().toString();
                                String classFile = fullPath.replace(".tml", ".java").substring(1);
                                classFile = classFile.substring(classFile.indexOf("/"));
                                if (fullPath.endsWith(".tml") && file.getType() == IResource.FILE
                                        && project.getFile(classFile).exists()) {
                                    IFile componentFile = (IFile) file;
                                    Element rootElement = getRootElementOfXML(componentFile.getContents());
                                    if (rootElement.getNodeName().trim().equals("t:container")) {
                                        String componentName = classFile.substring(classFile.lastIndexOf("/"));
                                        componentName = componentName.substring(0, componentName.indexOf("."));
                                        if (componentName.startsWith("/"))
                                            componentName = componentName.substring(1);
                                        ComponentInstance ci = new ComponentInstance();
                                        ci.setId(componentName);
                                        ci.setName(pi.getPrefix() + ":" + componentName);
                                        ci.setPath(pi.getPath());
                                        ci.setPrefix(pi.getPrefix());
                                        ci.setText(componentName);
                                        componentList.add(ci);
                                    }
                                }
                            }
                        }
                    } else {
                        // Load custom components from jar files
                        for (IJavaElement pack : root.getChildren()) {
                            if (pack != null && pack instanceof PackageFragment
                                    && pack.getElementName().equals(pi.getPath())) {
                                for (Object packo : ((PackageFragment) pack)
                                        .getChildrenOfType(IJavaElement.CLASS_FILE)) {
                                    ClassFile packi = (ClassFile) packo;
                                    String itemName = packi.getElementName();
                                    if (itemName.indexOf('$') < 0 && itemName.endsWith(".class")) {
                                        ComponentInstance ci = new ComponentInstance();
                                        String componentName = itemName.substring(0, itemName.length() - 6);
                                        ci.setId(componentName);
                                        ci.setName(pi.getPrefix() + ":" + componentName);
                                        ci.setPath(pi.getPath());
                                        ci.setPrefix(pi.getPrefix());
                                        ci.setText(componentName);
                                        componentList.add(ci);
                                    }
                                }
                                break;
                            }
                        }
                    }
                }
            }
        }
        model.setComponentList(componentList);
        markDirty();
    } catch (JavaModelException e) {
        e.printStackTrace();
    } catch (CoreException e) {
        e.printStackTrace();
    }
}

From source file:org.eclim.plugin.jdt.command.search.SearchCommand.java

License:Open Source License

/**
 * Creates a Position from the supplied SearchMatch.
 *
 * @param project The project searching from.
 * @param match The SearchMatch./*from   w ww.  ja  v a2 s  . c o m*/
 * @return The Position.
 */
protected Position createPosition(IProject project, SearchMatch match) throws Exception {
    IJavaElement element = (IJavaElement) match.getElement();
    IJavaElement parent = JavaUtils.getPrimaryElement(element);

    String file = null;
    String elementName = JavaUtils.getFullyQualifiedName(parent);
    if (parent.getElementType() == IJavaElement.CLASS_FILE) {
        IResource resource = parent.getResource();
        // occurs with a referenced project as a lib with no source and class
        // files that are not archived in that project
        if (resource != null && resource.getType() == IResource.FILE && !isJarArchive(resource.getLocation())) {
            file = resource.getLocation().toOSString();

        } else {
            IPath path = null;
            IPackageFragmentRoot root = (IPackageFragmentRoot) parent.getParent().getParent();
            resource = root.getResource();
            if (resource != null) {
                if (resource.getType() == IResource.PROJECT) {
                    path = ProjectUtils.getIPath((IProject) resource);
                } else {
                    path = resource.getLocation();
                }
            } else {
                path = root.getPath();
            }

            String classFile = elementName.replace('.', File.separatorChar);
            if (isJarArchive(path)) {
                file = "jar:file://" + path.toOSString() + '!' + classFile + ".class";
            } else {
                file = path.toOSString() + '/' + classFile + ".class";
            }

            // android injects its jdk classes, so filter those out if the project
            // doesn't have the android nature.
            if (ANDROID_JDK_URL.matcher(file).matches() && project != null
                    && !project.hasNature(ANDROID_NATURE)) {
                return null;
            }

            // if a source path attachment exists, use it.
            IPath srcPath = root.getSourceAttachmentPath();
            if (srcPath != null) {
                String rootPath;
                IProject elementProject = root.getJavaProject().getProject();

                // determine if src path is project relative or file system absolute.
                if (srcPath.isAbsolute() && elementProject.getName().equals(srcPath.segment(0))) {
                    rootPath = ProjectUtils.getFilePath(elementProject, srcPath.toString());
                } else {
                    rootPath = srcPath.toOSString();
                }
                String srcFile = FileUtils.toUrl(rootPath + File.separator + classFile + ".java");

                // see if source file exists at source path.
                FileSystemManager fsManager = VFS.getManager();
                FileObject fileObject = fsManager.resolveFile(srcFile.replace("%", "%25"));
                if (fileObject.exists()) {
                    file = srcFile;

                    // jdk sources on osx are under a "src/" dir in the jar
                } else if (Os.isFamily(Os.FAMILY_MAC)) {
                    srcFile = FileUtils
                            .toUrl(rootPath + File.separator + "src" + File.separator + classFile + ".java");
                    fileObject = fsManager.resolveFile(srcFile.replace("%", "%25"));
                    if (fileObject.exists()) {
                        file = srcFile;
                    }
                }
            }
        }
    } else {
        IPath location = match.getResource().getLocation();
        file = location != null ? location.toOSString() : null;
    }

    elementName = JavaUtils.getFullyQualifiedName(element);
    return Position.fromOffset(file.replace('\\', '/'), elementName, match.getOffset(), match.getLength());
}

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

License:Open Source License

/**
 * Gets the primary element (compilation unit or class file) for the supplied
 * element.//  w w w  . j  a  va 2  s  . co  m
 *
 * @param element The element.
 * @return The primary element.
 */
public static IJavaElement getPrimaryElement(IJavaElement element) {
    IJavaElement parent = element;
    while (parent.getElementType() != IJavaElement.COMPILATION_UNIT
            && parent.getElementType() != IJavaElement.CLASS_FILE) {
        parent = parent.getParent();
    }
    return parent;
}

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

License:Open Source License

/**
 * Get the offset of the supplied element within the source.
 *
 * @param element The element/*from  w  w w  . j  av a  2s .  co  m*/
 * @return The offset or -1 if it could not be determined.
 */
public static int getElementOffset(IJavaElement element) throws Exception {
    IJavaElement parent = getPrimaryElement(element);
    CompilationUnit cu = null;
    switch (parent.getElementType()) {
    case IJavaElement.COMPILATION_UNIT:
        cu = ASTUtils.getCompilationUnit((ICompilationUnit) parent);
        break;
    case IJavaElement.CLASS_FILE:
        try {
            cu = ASTUtils.getCompilationUnit((IClassFile) parent);
        } catch (IllegalStateException ise) {
            // no source attachement
        }
        break;
    }

    if (cu != null) {
        ASTNode[] nodes = ASTNodeSearchUtil.getDeclarationNodes(element, cu);
        if (nodes != null && nodes.length > 0) {
            int offset = nodes[0].getStartPosition();
            if (nodes[0] instanceof BodyDeclaration) {
                Javadoc docs = ((BodyDeclaration) nodes[0]).getJavadoc();
                if (docs != null) {
                    offset += docs.getLength() + 1;
                }
            }
            return offset;
        }
    }
    return -1;
}

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

License:Open Source License

/**
 * Gets the fully qualified name of the supplied java element.
 * <p/>//  ww w .ja  v  a2 s. com
 * NOTE: For easy of determining fields and method segments, they are appended
 * with a javadoc style '#' instead of the normal '.'.
 *
 * @param element The IJavaElement.
 *
 * @return The fully qualified name.
 */
public static String getFullyQualifiedName(IJavaElement element) {
    IJavaElement parent = element;
    while (parent.getElementType() != IJavaElement.COMPILATION_UNIT
            && parent.getElementType() != IJavaElement.CLASS_FILE) {
        parent = parent.getParent();
    }

    StringBuffer elementName = new StringBuffer().append(parent.getParent().getElementName()).append('.')
            .append(FileUtils.getFileName(parent.getElementName()));

    switch (element.getElementType()) {
    case IJavaElement.FIELD:
        IField field = (IField) element;
        elementName.append('#').append(field.getElementName());
        break;
    case IJavaElement.METHOD:
        IMethod method = (IMethod) element;
        elementName.append('#').append(method.getElementName()).append('(');
        String[] parameters = method.getParameterTypes();
        for (int ii = 0; ii < parameters.length; ii++) {
            if (ii != 0) {
                elementName.append(", ");
            }
            elementName.append(Signature.toString(parameters[ii]).replace('/', '.'));
        }
        elementName.append(')');
        break;
    }

    return elementName.toString();
}

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

License:Open Source License

/**
 * Constructs a compilation unit relative name for the supplied type.
 * <p/>// w ww  .j a va  2  s  .  co  m
 * If the type is imported, in java.lang, or in the same package as the source
 * file, then the type name returned is unqualified, otherwise the name
 * returned is the fully qualified type name.
 *
 * @param src The compilation unit.
 * @param type The type.
 *
 * @return The relative type name.
 */
public static String getCompilationUnitRelativeTypeName(ICompilationUnit src, IType type) throws Exception {
    String typeName = type.getFullyQualifiedName().replace('$', '.');
    if (JavaUtils.containsImport(src, type)) {
        typeName = type.getElementName();

        int parentType = type.getParent().getElementType();
        if (parentType == IJavaElement.TYPE) {
            typeName = type.getParent().getElementName() + '.' + typeName;
        } else if (parentType == IJavaElement.CLASS_FILE) {
            String parentName = type.getParent().getElementName();
            int index = parentName.indexOf('$');
            if (index != -1) {
                parentName = parentName.substring(0, index);
                typeName = parentName + '.' + typeName;
            }
        }
    } else {
        typeName = type.getFullyQualifiedName().replace('$', '.');
    }

    return typeName;
}

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

License:Open Source License

/**
 * Gets the signature for the supplied type.
 *
 * @param typeInfo The typeInfo./*from   w w  w .  j a  va  2s  .co m*/
 * @return The signature.
 */
public static String getTypeSignature(TypeInfo typeInfo) throws Exception {
    StringBuffer buffer = new StringBuffer();
    IType type = typeInfo.getType();
    int flags = type.getFlags();
    if (Flags.isPublic(flags)) {
        buffer.append("public ");
    }

    buffer.append(type.isClass() ? "class " : "interface ");
    IJavaElement parent = type.getParent();
    if (parent.getElementType() == IJavaElement.TYPE) {
        buffer.append(type.getParent().getElementName()).append('.');
    } else if (parent.getElementType() == IJavaElement.CLASS_FILE) {
        int index = parent.getElementName().indexOf('$');
        if (index != -1) {
            buffer.append(parent.getElementName().substring(0, index)).append('.');
        }
    }
    buffer.append(type.getElementName());
    String[] params = typeInfo.getTypeParameters();
    String[] args = typeInfo.getTypeArguments();
    if (params != null && params.length > 0 && args != null && args.length > 0) {
        buffer.append('<');
        for (int ii = 0; ii < args.length; ii++) {
            if (ii > 0) {
                buffer.append(',');
            }
            buffer.append(args[ii]);
        }
        buffer.append('>');
    }
    return buffer.toString();
}