Search in sources :

Example 1 with ClassFile

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

the class Util method getJdkLevel.

/**
     * Get the jdk level of this root.
     * The value can be:
     * <ul>
     * <li>major<<16 + minor : see predefined constants on ClassFileConstants </li>
     * <li><code>0</null> if the root is a source package fragment root or if a Java model exception occured</li>
     * </ul>
     * Returns the jdk level
     */
public static long getJdkLevel(Object targetLibrary) {
    try {
        ClassFileReader reader = null;
        if (targetLibrary instanceof IFolder) {
            // only internal classfolders are allowed
            IFile classFile = findFirstClassFile((IFolder) targetLibrary);
            if (classFile != null)
                reader = Util.newClassFileReader(classFile);
        } else {
            // root is a jar file or a zip file
            ZipFile jar = null;
            try {
                IPath path = null;
                if (targetLibrary instanceof IResource) {
                    path = ((IResource) targetLibrary).getFullPath();
                } else if (targetLibrary instanceof File) {
                    File f = (File) targetLibrary;
                    if (!f.isDirectory()) {
                        path = new Path(((File) targetLibrary).getPath());
                    }
                }
                if (path != null) {
                    jar = JavaModelManager.getJavaModelManager().getZipFile(path);
                    for (Enumeration e = jar.entries(); e.hasMoreElements(); ) {
                        ZipEntry member = (ZipEntry) e.nextElement();
                        String entryName = member.getName();
                        if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(entryName)) {
                            reader = ClassFileReader.read(jar, entryName);
                            break;
                        }
                    }
                }
            } catch (CoreException e) {
            // ignore
            } finally {
                JavaModelManager.getJavaModelManager().closeZipFile(jar);
            }
        }
        if (reader != null) {
            return reader.getVersion();
        }
    } catch (CoreException e) {
    // ignore
    } catch (ClassFormatException e) {
    // ignore
    } catch (IOException e) {
    // ignore
    }
    return 0;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) Enumeration(java.util.Enumeration) IPath(org.eclipse.core.runtime.IPath) IClassFileReader(org.eclipse.jdt.core.util.IClassFileReader) ClassFileReader(org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) ClassFormatException(org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException) ZipFile(java.util.zip.ZipFile) CoreException(org.eclipse.core.runtime.CoreException) IClassFile(org.eclipse.jdt.core.IClassFile) ClassFile(org.eclipse.jdt.internal.core.ClassFile) ZipFile(java.util.zip.ZipFile) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IResource(org.eclipse.core.resources.IResource) IFolder(org.eclipse.core.resources.IFolder)

Example 2 with ClassFile

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

the class SourceMapper method getType.

/**
     * Returns the type with the given <code>typeName</code>.  Returns inner classes
     * as well.
     */
protected IType getType(String typeName) {
    if (typeName.length() == 0) {
        IJavaElement classFile = this.binaryType.getParent();
        String classFileName = classFile.getElementName();
        StringBuffer newClassFileName = new StringBuffer();
        int lastDollar = classFileName.lastIndexOf('$');
        for (int i = 0; i <= lastDollar; i++) newClassFileName.append(classFileName.charAt(i));
        newClassFileName.append(Integer.toString(this.anonymousCounter));
        PackageFragment pkg = (PackageFragment) classFile.getParent();
        return new BinaryType(new ClassFile(pkg, newClassFileName.toString()), typeName);
    } else if (this.binaryType.getElementName().equals(typeName))
        return this.binaryType;
    else
        return this.binaryType.getType(typeName);
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) PackageFragment(org.eclipse.jdt.internal.core.PackageFragment) BinaryType(org.eclipse.jdt.internal.core.BinaryType) IBinaryType(org.eclipse.jdt.internal.compiler.env.IBinaryType) IClassFile(org.eclipse.jdt.core.IClassFile) ClassFile(org.eclipse.jdt.internal.core.ClassFile)

Example 3 with ClassFile

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

the class Util method getUnresolvedJavaElement.

/**
     * Return the java element corresponding to the given compiler binding.
     */
public static JavaElement getUnresolvedJavaElement(TypeBinding typeBinding, WorkingCopyOwner workingCopyOwner, BindingsToNodesMap bindingsToNodes) {
    if (typeBinding == null)
        return null;
    switch(typeBinding.kind()) {
        case Binding.ARRAY_TYPE:
            typeBinding = ((org.eclipse.jdt.internal.compiler.lookup.ArrayBinding) typeBinding).leafComponentType();
            return getUnresolvedJavaElement(typeBinding, workingCopyOwner, bindingsToNodes);
        case Binding.BASE_TYPE:
        case Binding.WILDCARD_TYPE:
        case Binding.INTERSECTION_TYPE:
            return null;
        default:
            if (typeBinding.isCapture())
                return null;
    }
    ReferenceBinding referenceBinding;
    if (typeBinding.isParameterizedType() || typeBinding.isRawType())
        referenceBinding = (ReferenceBinding) typeBinding.erasure();
    else
        referenceBinding = (ReferenceBinding) typeBinding;
    char[] fileName = referenceBinding.getFileName();
    if (referenceBinding.isLocalType() || referenceBinding.isAnonymousType()) {
        // local or anonymous type
        if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(fileName)) {
            int jarSeparator = CharOperation.indexOf(IDependent.JAR_FILE_ENTRY_SEPARATOR, fileName);
            // pkgEnd is exclusive
            int pkgEnd = CharOperation.lastIndexOf('/', fileName);
            if (pkgEnd == -1)
                pkgEnd = CharOperation.lastIndexOf(File.separatorChar, fileName);
            if (// if in a jar and no slash, it is a default package -> pkgEnd should be equal to jarSeparator
            jarSeparator != -1 && pkgEnd < jarSeparator)
                pkgEnd = jarSeparator;
            if (pkgEnd == -1)
                return null;
            IPackageFragment pkg = getPackageFragment(fileName, pkgEnd, jarSeparator);
            char[] constantPoolName = referenceBinding.constantPoolName();
            if (constantPoolName == null) {
                ClassFile classFile = (ClassFile) getClassFile(fileName);
                return classFile == null ? null : (JavaElement) classFile.getType();
            }
            pkgEnd = CharOperation.lastIndexOf('/', constantPoolName);
            char[] classFileName = CharOperation.subarray(constantPoolName, pkgEnd + 1, constantPoolName.length);
            ClassFile classFile = (ClassFile) pkg.getClassFile(new String(classFileName) + SuffixConstants.SUFFIX_STRING_class);
            return (JavaElement) classFile.getType();
        }
        ICompilationUnit cu = getCompilationUnit(fileName, workingCopyOwner);
        if (cu == null)
            return null;
        // must use getElementAt(...) as there is no back pointer to the defining method (scope is null after resolution has ended)
        try {
            int sourceStart = ((org.eclipse.jdt.internal.compiler.lookup.LocalTypeBinding) referenceBinding).sourceStart;
            return (JavaElement) cu.getElementAt(sourceStart);
        } catch (JavaModelException e) {
            // does not exist
            return null;
        }
    } else if (referenceBinding.isTypeVariable()) {
        // type parameter
        final String typeVariableName = new String(referenceBinding.sourceName());
        org.eclipse.jdt.internal.compiler.lookup.Binding declaringElement = ((org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding) referenceBinding).declaringElement;
        if (declaringElement instanceof MethodBinding) {
            IMethod declaringMethod = (IMethod) getUnresolvedJavaElement((MethodBinding) declaringElement, workingCopyOwner, bindingsToNodes);
            return (JavaElement) declaringMethod.getTypeParameter(typeVariableName);
        } else {
            IType declaringType = (IType) getUnresolvedJavaElement((TypeBinding) declaringElement, workingCopyOwner, bindingsToNodes);
            return (JavaElement) declaringType.getTypeParameter(typeVariableName);
        }
    } else {
        // case of a WilCardBinding that doesn't have a corresponding Java element
        if (fileName == null)
            return null;
        // member or top level type
        TypeBinding declaringTypeBinding = typeBinding.enclosingType();
        if (declaringTypeBinding == null) {
            // top level type
            if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(fileName)) {
                ClassFile classFile = (ClassFile) getClassFile(fileName);
                if (classFile == null)
                    return null;
                return (JavaElement) classFile.getType();
            }
            ICompilationUnit cu = getCompilationUnit(fileName, workingCopyOwner);
            if (cu == null)
                return null;
            return (JavaElement) cu.getType(new String(referenceBinding.sourceName()));
        } else {
            // member type
            IType declaringType = (IType) getUnresolvedJavaElement(declaringTypeBinding, workingCopyOwner, bindingsToNodes);
            if (declaringType == null)
                return null;
            return (JavaElement) declaringType.getType(new String(referenceBinding.sourceName()));
        }
    }
}
Also used : FieldBinding(org.eclipse.jdt.internal.compiler.lookup.FieldBinding) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) MethodBinding(org.eclipse.jdt.internal.compiler.lookup.MethodBinding) Binding(org.eclipse.jdt.internal.compiler.lookup.Binding) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) JavaModelException(org.eclipse.jdt.core.JavaModelException) IClassFile(org.eclipse.jdt.core.IClassFile) ClassFile(org.eclipse.jdt.internal.core.ClassFile) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) IType(org.eclipse.jdt.core.IType) JavaElement(org.eclipse.jdt.internal.core.JavaElement) IJavaElement(org.eclipse.jdt.core.IJavaElement) MethodBinding(org.eclipse.jdt.internal.compiler.lookup.MethodBinding) IMethod(org.eclipse.jdt.core.IMethod)

Aggregations

IClassFile (org.eclipse.jdt.core.IClassFile)3 ClassFile (org.eclipse.jdt.internal.core.ClassFile)3 IJavaElement (org.eclipse.jdt.core.IJavaElement)2 File (java.io.File)1 IOException (java.io.IOException)1 Enumeration (java.util.Enumeration)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1 IFile (org.eclipse.core.resources.IFile)1 IFolder (org.eclipse.core.resources.IFolder)1 IResource (org.eclipse.core.resources.IResource)1 CoreException (org.eclipse.core.runtime.CoreException)1 IPath (org.eclipse.core.runtime.IPath)1 Path (org.eclipse.core.runtime.Path)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IMethod (org.eclipse.jdt.core.IMethod)1 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1 IType (org.eclipse.jdt.core.IType)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 IClassFileReader (org.eclipse.jdt.core.util.IClassFileReader)1