Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for classes in JRT #56

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.sf.feeling.decompiler.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -19,15 +20,76 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException;
import org.eclipse.jdt.internal.compiler.util.JRTUtil;

public class JarClassExtractor
{

/**
* extracts class files from jar/zip archive to specified path. See
* <code>IDecompiler</code> documentation for the format of pareameters.
* extracts class files from jar/zip archive or modules to specified path.
* See <code>IDecompiler</code> documentation for the format of pareameters.
*/
public static void extract( String archivePath, String packege, String className, boolean inner, String to )
throws IOException
{
if ( isClassInJrt( archivePath ) )
{
extractClassFromJrt( archivePath, packege, className, to );
}
else
{
extractClassesFromJar( archivePath, packege, className, inner, to );
}
}

private static boolean isClassInJrt( String archivePath )
{
try
{
return archivePath.endsWith( JRTUtil.JRT_FS_JAR );
}
catch ( NoClassDefFoundError e )
{
// Compatible with pre-Oxygen Eclipse, where JRTUtil does not exist
return false;
}
}

private static void extractClassFromJrt( String archivePath, String packege, String className, String to )
throws IOException, FileNotFoundException
{
File outputFile = new File( to + File.separatorChar + className );
try (FileOutputStream outputStream = new FileOutputStream( outputFile ))
{
File jrtPath = new File( archivePath );
List<String> modules = JRTUtil.getModulesDeclaringPackage( jrtPath, packege, null );
String moduleRelativePath = packege + "/" + className;
byte[] content = findModuleWithFile( jrtPath, modules, moduleRelativePath );
outputStream.write( content );
}
catch ( ClassFormatException e )
{
throw new RuntimeException( "Unable to read JRT file", e );
}
}

private static byte[] findModuleWithFile( File jrtPath, List<String> modules, String moduleRelativeClassPath )
throws IOException, ClassFormatException
{
for ( String module : modules )
{
byte[] content = JRTUtil.getClassfileContent( jrtPath, moduleRelativeClassPath, module );
if ( content != null )
{
return content;
}
}
throw new RuntimeException( "No module in JRT contains class " + moduleRelativeClassPath );
}

private static void extractClassesFromJar( String archivePath, String packege, String className, boolean inner,
String to ) throws IOException, FileNotFoundException
{
ZipFile archive = new ZipFile( archivePath );
List entries = findRelevant( archive, packege, className, inner );
Expand Down Expand Up @@ -65,7 +127,6 @@ public static void extract( String archivePath, String packege, String className
out.close( );
}
}

}

private static List findRelevant( ZipFile archive, String packege, String className, boolean inner )
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@

<repositories>
<repository>
<id>eclipse-juno</id>
<id>eclipse-oxygen</id>
<layout>p2</layout>
<url>http://download.eclipse.org/releases/juno</url>
<url>http://download.eclipse.org/releases/oxygen</url>
</repository>
</repositories>

Expand Down