package org.eclipse.jdt.internal.core.nd.java.model;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResourceStatus;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IJavaModelStatusConstants;
import org.eclipse.jdt.core.IOrdinaryClassFile;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException;
import org.eclipse.jdt.internal.compiler.env.IBinaryType;
import org.eclipse.jdt.internal.compiler.env.IDependent;
import org.eclipse.jdt.internal.compiler.util.SuffixConstants;
import org.eclipse.jdt.internal.core.ClassFile;
import org.eclipse.jdt.internal.core.JarPackageFragmentRoot;
import org.eclipse.jdt.internal.core.JavaModelManager;
import org.eclipse.jdt.internal.core.PackageFragment;
import org.eclipse.jdt.internal.core.PackageFragmentRoot;
import org.eclipse.jdt.internal.core.nd.IReader;
import org.eclipse.jdt.internal.core.nd.Nd;
import org.eclipse.jdt.internal.core.nd.db.IndexException;
import org.eclipse.jdt.internal.core.nd.indexer.Indexer;
import org.eclipse.jdt.internal.core.nd.java.JavaIndex;
import org.eclipse.jdt.internal.core.nd.java.JavaNames;
import org.eclipse.jdt.internal.core.nd.java.NdResourceFile;
import org.eclipse.jdt.internal.core.nd.java.NdType;
import org.eclipse.jdt.internal.core.nd.java.TypeRef;
import org.eclipse.jdt.internal.core.nd.util.CharArrayUtils;
import org.eclipse.jdt.internal.core.util.Util;
public class BinaryTypeFactory {
public static final class NotInIndexException extends Exception {
private static final long serialVersionUID = 2859848007651528256L;
public NotInIndexException() {
}
}
private static BinaryTypeDescriptor createDescriptor(PackageFragment pkg, ClassFile classFile) {
PackageFragmentRoot root = (PackageFragmentRoot) pkg.getParent();
IPath location = JavaIndex.getLocationForElement(root);
if (location == null) {
return null;
}
String entryName = Util.concatWith(pkg.names, classFile.getElementName(), '/');
String name = Util.concatWith(pkg.names, classFile.getName(), '/');
String overridePath = root.getClassFilePath(entryName);
if (overridePath != entryName) {
entryName = overridePath;
String versionPath = overridePath.substring(0, overridePath.indexOf(entryName));
name = versionPath + name;
}
char[] fieldDescriptor = CharArrayUtils.concat(new char[] { 'L' },
name.toCharArray(), new char[] { ';' });
IPath workspacePath = root.getPath();
String indexPath;
if (root instanceof JarPackageFragmentRoot) {
indexPath = root.getHandleIdentifier() + IDependent.JAR_FILE_ENTRY_SEPARATOR + entryName;
} else {
location = location.append(entryName);
indexPath = workspacePath.append(entryName).toString();
workspacePath = classFile.resource().getFullPath();
}
return new BinaryTypeDescriptor(location.toString().toCharArray(), fieldDescriptor,
workspacePath.toString().toCharArray(), indexPath.toCharArray());
}
public static BinaryTypeDescriptor createDescriptor(IOrdinaryClassFile classFile) {
ClassFile concreteClass = (ClassFile)classFile;
PackageFragment parent = (PackageFragment) classFile.getParent();
return createDescriptor(parent, concreteClass);
}
public static BinaryTypeDescriptor createDescriptor(IType type) {
return createDescriptor(type.getClassFile());
}
public static IBinaryType create(IOrdinaryClassFile classFile, IProgressMonitor monitor) throws JavaModelException, ClassFormatException {
BinaryTypeDescriptor descriptor = createDescriptor(classFile);
return readType(descriptor, monitor);
}
public static IBinaryType readType(BinaryTypeDescriptor descriptor, IProgressMonitor monitor) throws JavaModelException, ClassFormatException {
if (JavaIndex.isEnabled()) {
try {
return readFromIndex(JavaIndex.getIndex(), descriptor, monitor);
} catch (NotInIndexException e) {
}
}
return rawReadType(descriptor, true);
}
public static ClassFileReader rawReadType(BinaryTypeDescriptor descriptor, boolean fullyInitialize) throws JavaModelException, ClassFormatException {
try {
return rawReadTypeTestForExists(descriptor, fullyInitialize, true);
} catch (FileNotFoundException e) {
throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
}
}
public static ClassFileReader rawReadTypeTestForExists(BinaryTypeDescriptor descriptor, boolean fullyInitialize,
boolean useInvalidArchiveCache) throws JavaModelException, ClassFormatException, FileNotFoundException {
if (descriptor == null) {
return null;
}
if (descriptor.isInJarFile()) {
if (CharOperation.indexOf("jrt-fs.jar".toCharArray(), descriptor.location, false) == -1) {
ZipFile zip = null;
try {
zip = JavaModelManager.getJavaModelManager().getZipFile(new Path(new String(descriptor.workspacePath)),
useInvalidArchiveCache);
char[] entryNameCharArray = CharArrayUtils.concat(
JavaNames.fieldDescriptorToBinaryName(descriptor.fieldDescriptor), SuffixConstants.SUFFIX_class);
String entryName = new String(entryNameCharArray);
ZipEntry ze = zip.getEntry(entryName);
if (ze != null) {
byte contents[];
try {
contents = org.eclipse.jdt.internal.compiler.util.Util.getZipEntryByteContent(ze, zip);
} catch (IOException ioe) {
throw new JavaModelException(ioe, IJavaModelStatusConstants.IO_EXCEPTION);
}
return new ClassFileReader(contents, descriptor.indexPath, fullyInitialize);
}
} catch (CoreException e) {
throw new JavaModelException(e);
} finally {
JavaModelManager.getJavaModelManager().closeZipFile(zip);
}
}
} else {
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(new String(descriptor.workspacePath)));
byte[] contents;
try (InputStream stream = file.getContents(true)) {
contents = org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsByteArray(stream, -1);
} catch (CoreException e) {
IStatus status = e.getStatus();
if (status.getCode() == IResourceStatus.RESOURCE_NOT_FOUND) {
throw new FileNotFoundException();
}
throw new JavaModelException(e);
} catch (IOException e) {
throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
}
return new ClassFileReader(contents, file.getFullPath().toString().toCharArray(), fullyInitialize);
}
return null;
}
public static IBinaryType readFromIndex(JavaIndex index, BinaryTypeDescriptor descriptor, IProgressMonitor monitor) throws JavaModelException, NotInIndexException {
char[] fieldDescriptor = descriptor.fieldDescriptor;
Nd nd = index.getNd();
if (descriptor.location != null) {
try (IReader lock = nd.acquireReadLock()) {
try {
TypeRef typeRef = TypeRef.create(nd, descriptor.location, fieldDescriptor);
NdType type = typeRef.get();
if (type == null) {
NdResourceFile resourceFile = index.getResourceFile(descriptor.location);
if (index.isUpToDate(resourceFile)) {
return null;
}
throw new NotInIndexException();
}
NdResourceFile resourceFile = type.getResourceFile();
if (index.isUpToDate(resourceFile)) {
IndexBinaryType result = new IndexBinaryType(typeRef, descriptor.indexPath);
result.initSimpleAttributes();
return result;
}
throw new NotInIndexException();
} catch (CoreException e) {
throw new JavaModelException(e);
}
} catch (IndexException e) {
Package.log("Index corruption detected. Rebuilding index.", e);
Indexer.getInstance().requestRebuildIndex();
}
}
throw new NotInIndexException();
}
}