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;
}
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);
}
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()));
}
}
}
Aggregations