Search in sources :

Example 6 with JavaElement

use of org.eclipse.jdt.internal.core.JavaElement in project che by eclipse.

the class Util method getUnresolvedJavaElement.

/**
     * Returns the IInitializer that contains the given local variable in the given type
     */
public static JavaElement getUnresolvedJavaElement(int localSourceStart, int localSourceEnd, JavaElement type) {
    try {
        if (!(type instanceof IType))
            return null;
        IInitializer[] initializers = ((IType) type).getInitializers();
        for (int i = 0; i < initializers.length; i++) {
            IInitializer initializer = initializers[i];
            ISourceRange sourceRange = initializer.getSourceRange();
            if (sourceRange != null) {
                int initializerStart = sourceRange.getOffset();
                int initializerEnd = initializerStart + sourceRange.getLength();
                if (initializerStart <= localSourceStart && localSourceEnd <= initializerEnd) {
                    return (JavaElement) initializer;
                }
            }
        }
        return null;
    } catch (JavaModelException e) {
        return null;
    }
}
Also used : JavaElement(org.eclipse.jdt.internal.core.JavaElement) IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaModelException(org.eclipse.jdt.core.JavaModelException) IInitializer(org.eclipse.jdt.core.IInitializer) IType(org.eclipse.jdt.core.IType) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 7 with JavaElement

use of org.eclipse.jdt.internal.core.JavaElement in project che by eclipse.

the class ToolFactory method createDefaultClassFileReader.

/**
     * Create a classfile reader onto a classfile Java element.
     * Create a default classfile reader, able to expose the internal representation of a given classfile
     * according to the decoding flag used to initialize the reader.
     * Answer null if the file named fileName doesn't represent a valid .class file.
     * <p/>
     * The decoding flags are described in IClassFileReader.
     *
     * @param classfile
     *         the classfile element to introspect
     * @param decodingFlag
     *         the flag used to decode the class file reader.
     * @return a default classfile reader
     * @see org.eclipse.jdt.core.util.IClassFileReader
     */
public static IClassFileReader createDefaultClassFileReader(IClassFile classfile, int decodingFlag) {
    IPackageFragmentRoot root = (IPackageFragmentRoot) classfile.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
    if (root != null) {
        try {
            if (root instanceof JarPackageFragmentRoot) {
                String archiveName = null;
                ZipFile jar = null;
                try {
                    jar = ((JarPackageFragmentRoot) root).getJar();
                    archiveName = jar.getName();
                } finally {
                    JavaModelManager.getJavaModelManager().closeZipFile(jar);
                }
                PackageFragment packageFragment = (PackageFragment) classfile.getParent();
                String classFileName = classfile.getElementName();
                String entryName = org.eclipse.jdt.internal.core.util.Util.concatWith(packageFragment.names, classFileName, '/');
                return createDefaultClassFileReader(archiveName, entryName, decodingFlag);
            } else {
                InputStream in = null;
                try {
                    in = ((IFile) ((JavaElement) classfile).resource()).getContents();
                    return createDefaultClassFileReader(in, decodingFlag);
                } finally {
                    if (in != null)
                        try {
                            in.close();
                        } catch (IOException e) {
                        // ignore
                        }
                }
            }
        } catch (CoreException e) {
        // unable to read
        }
    }
    return null;
}
Also used : PackageFragment(org.eclipse.jdt.internal.core.PackageFragment) JavaElement(org.eclipse.jdt.internal.core.JavaElement) IJavaElement(org.eclipse.jdt.core.IJavaElement) ZipFile(java.util.zip.ZipFile) CoreException(org.eclipse.core.runtime.CoreException) JarPackageFragmentRoot(org.eclipse.jdt.internal.core.JarPackageFragmentRoot) InputStream(java.io.InputStream) IOException(java.io.IOException) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 8 with JavaElement

use of org.eclipse.jdt.internal.core.JavaElement in project jbosstools-hibernate by jbosstools.

the class NewHibernateMappingFileWizard method processJavaElements.

protected void processJavaElements(Object obj, int depth) {
    if (depth < 0) {
        return;
    }
    try {
        if (obj instanceof ICompilationUnit) {
            ICompilationUnit cu = (ICompilationUnit) obj;
            selectionCU.add(cu);
        } else if (obj instanceof JavaProject && depth > 0) {
            JavaProject javaProject = (JavaProject) obj;
            IPackageFragmentRoot[] pfr = javaProject.getAllPackageFragmentRoots();
            for (IPackageFragmentRoot element : pfr) {
                processJavaElements(element, depth - 1);
            }
        } else if (obj instanceof PackageFragment) {
            PackageFragment packageFragment = (PackageFragment) obj;
            ICompilationUnit[] cus = packageFragment.getCompilationUnits();
            for (ICompilationUnit cu : cus) {
                selectionCU.add(cu);
            }
        } else if (obj instanceof PackageFragmentRoot && depth > 0) {
            JavaElement javaElement = (JavaElement) obj;
            JavaElementInfo javaElementInfo = (JavaElementInfo) javaElement.getElementInfo();
            IJavaElement[] je = javaElementInfo.getChildren();
            for (IJavaElement element : je) {
                processJavaElements(element, depth - 1);
            }
        } else if (obj instanceof JavaElement) {
            JavaElement javaElement = (JavaElement) obj;
            ICompilationUnit cu = javaElement.getCompilationUnit();
            selectionCU.add(cu);
        }
    } catch (JavaModelException e) {
    // just ignore it!
    // HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ", e); //$NON-NLS-1$
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) PackageFragment(org.eclipse.jdt.internal.core.PackageFragment) IJavaElement(org.eclipse.jdt.core.IJavaElement) IJavaProject(org.eclipse.jdt.core.IJavaProject) JavaProject(org.eclipse.jdt.internal.core.JavaProject) PackageFragmentRoot(org.eclipse.jdt.internal.core.PackageFragmentRoot) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) JavaElement(org.eclipse.jdt.internal.core.JavaElement) IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaElementInfo(org.eclipse.jdt.internal.core.JavaElementInfo) JavaModelException(org.eclipse.jdt.core.JavaModelException) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Aggregations

IJavaElement (org.eclipse.jdt.core.IJavaElement)8 JavaElement (org.eclipse.jdt.internal.core.JavaElement)8 JavaModelException (org.eclipse.jdt.core.JavaModelException)5 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 IType (org.eclipse.jdt.core.IType)3 PackageFragment (org.eclipse.jdt.internal.core.PackageFragment)3 PackageFragmentRoot (org.eclipse.jdt.internal.core.PackageFragmentRoot)3 IPath (org.eclipse.core.runtime.IPath)2 IJavaProject (org.eclipse.jdt.core.IJavaProject)2 IMethod (org.eclipse.jdt.core.IMethod)2 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)2 MethodBinding (org.eclipse.jdt.internal.compiler.lookup.MethodBinding)2 ReferenceBinding (org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding)2 TypeBinding (org.eclipse.jdt.internal.compiler.lookup.TypeBinding)2 JavaElementInfo (org.eclipse.jdt.internal.core.JavaElementInfo)2 JavaProject (org.eclipse.jdt.internal.core.JavaProject)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1