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.JavaModelException;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException;
import org.eclipse.jdt.internal.compiler.env.IBinaryModule;
import org.eclipse.jdt.internal.compiler.env.IDependent;
import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
import org.eclipse.jdt.internal.core.JarPackageFragmentRoot;
import org.eclipse.jdt.internal.core.JavaModelManager;
import org.eclipse.jdt.internal.core.JrtPackageFragmentRoot;
import org.eclipse.jdt.internal.core.ModularClassFile;
import org.eclipse.jdt.internal.core.PackageFragmentRoot;
import org.eclipse.jdt.internal.core.nd.java.JavaIndex;
public class BinaryModuleFactory {
public static BinaryModuleDescriptor createDescriptor(ModularClassFile modularClassFile) {
return createDescriptor(modularClassFile.getPackageFragmentRoot(), modularClassFile);
}
private static BinaryModuleDescriptor createDescriptor(PackageFragmentRoot root, ModularClassFile classFile) {
IPath location = JavaIndex.getLocationForElement(root);
if (location == null) {
return null;
}
String entryName = TypeConstants.MODULE_INFO_CLASS_NAME_STRING;
IPath workspacePath = root.getPath();
String indexPath;
char[] moduleName = null;
if (root instanceof JarPackageFragmentRoot) {
entryName = ((JarPackageFragmentRoot) root).getClassFilePath(entryName);
indexPath = root.getHandleIdentifier() + IDependent.JAR_FILE_ENTRY_SEPARATOR + entryName;
if (root instanceof JrtPackageFragmentRoot) {
moduleName = root.getElementName().toCharArray();
}
} else {
location = location.append(entryName);
indexPath = workspacePath.append(entryName).toString();
workspacePath = classFile.resource().getFullPath();
}
return new BinaryModuleDescriptor(location.toString().toCharArray(), moduleName,
workspacePath.toString().toCharArray(), indexPath.toCharArray());
}
public static IBinaryModule readModule(BinaryModuleDescriptor descriptor, IProgressMonitor monitor) throws JavaModelException, ClassFormatException {
return rawReadModule(descriptor, true);
}
public static IBinaryModule rawReadModule(BinaryModuleDescriptor descriptor, boolean fullyInitialize) throws JavaModelException, ClassFormatException {
try {
return rawReadModuleTestForExists(descriptor, fullyInitialize, true);
} catch (FileNotFoundException e) {
throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
}
}
public static IBinaryModule rawReadModuleTestForExists(BinaryModuleDescriptor descriptor, boolean fullyInitialize,
boolean useInvalidArchiveCache) throws JavaModelException, ClassFormatException, FileNotFoundException {
if (descriptor == null) {
return null;
}
if (descriptor.isInJarFile()) {
ZipFile zip = null;
try {
zip = JavaModelManager.getJavaModelManager().getZipFile(new Path(new String(descriptor.workspacePath)),
useInvalidArchiveCache);
String entryName = TypeConstants.MODULE_INFO_CLASS_NAME_STRING;
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);
}
ClassFileReader classFileReader = new ClassFileReader(contents, descriptor.indexPath, fullyInitialize);
return classFileReader.getModuleDeclaration();
}
} 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);
}
ClassFileReader classFileReader = new ClassFileReader(contents, file.getFullPath().toString().toCharArray(), fullyInitialize);
return classFileReader.getModuleDeclaration();
}
return null;
}
}