Skip to content

Commit

Permalink
Merge pull request #3 from jsight/lincolnthree-WINDUP-233
Browse files Browse the repository at this point in the history
Fixes for issues finding original class files based upon decompiled resu...
  • Loading branch information
lincolnthree committed Aug 26, 2014
2 parents 6a7ece1 + 8d6c099 commit a0ddaa4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 52 deletions.
@@ -1,10 +1,10 @@
package org.jboss.windup.decompiler.api;

import java.util.Collections;
import java.util.HashSet;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.Map;

/**
* Keeps a count of successful decompilations and list of failed ones, in the form of an exception with String path and
Expand All @@ -16,22 +16,16 @@
public class DecompilationResult
{
private final List<DecompilationFailure> failed = new LinkedList<>();
private final Set<String> decompiled = new HashSet<>();
private final Set<String> decompiledOutputFiles = new HashSet<>();
private final Map<String, String> decompiledFiles = new HashMap<>();

public void addDecompiledOutputFile(String path)
public void addDecompiled(String inputPath, String path)
{
this.decompiledOutputFiles.add(path);
this.decompiledFiles.put(inputPath, path);
}

public Set<String> getDecompiledOutputFiles()
public Map<String, String> getDecompiledFiles()
{
return this.decompiledOutputFiles;
}

public void addDecompiled(String path)
{
this.decompiled.add(path);
return this.decompiledFiles;
}

public void addFailure(DecompilationFailure failure)
Expand All @@ -43,10 +37,4 @@ public List<DecompilationFailure> getFailures()
{
return Collections.unmodifiableList(this.failed);
}

public Set<String> getDecompiled()
{
return this.decompiled;
}

}
@@ -1,14 +1,14 @@
package org.jboss.windup.decompiler;

import org.jboss.windup.decompiler.api.DecompilationException;
import org.jboss.windup.decompiler.api.Decompiler;
import org.jboss.windup.decompiler.api.DecompilationResult;
import org.jboss.windup.decompiler.api.DecompilationFailure;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

import org.apache.commons.io.FileUtils;
import org.jboss.windup.decompiler.api.DecompilationException;
import org.jboss.windup.decompiler.api.DecompilationFailure;
import org.jboss.windup.decompiler.api.DecompilationResult;
import org.jboss.windup.decompiler.api.Decompiler;
import org.jboss.windup.decompiler.util.ZipUtil;
import org.junit.After;
import org.junit.Assert;
Expand Down Expand Up @@ -80,7 +80,8 @@ public void testDecompileWicketJar() throws DecompilationException
else
log.error(sb.toString());
}
log.info("Compilation results: {} succeeded, {} failed.", res.getDecompiled().size(), res.getFailures().size());
log.info("Compilation results: {} succeeded, {} failed.", res.getDecompiledFiles().size(), res.getFailures()
.size());

final File sampleFile = new File(outputFolder, "org/apache/wicket/model/LoadableDetachableModel.java");
Assert.assertTrue("Decompiled class files exist:\n " + sampleFile.getAbsolutePath(), sampleFile.exists());
Expand Down Expand Up @@ -115,7 +116,8 @@ public void testDecompileWicketJarDirectory() throws DecompilationException, IOE
else
log.error(sb.toString());
}
log.info("Compilation results: {} succeeded, {} failed.", res.getDecompiled().size(), res.getFailures().size());
log.info("Compilation results: {} succeeded, {} failed.", res.getDecompiledFiles().size(), res.getFailures()
.size());

final File sampleFile = new File(outputFolder, "org/apache/wicket/model/LoadableDetachableModel.java");
Assert.assertTrue("Decompiled class did not exist in: " + sampleFile.getAbsolutePath(), sampleFile.exists());
Expand Down
Expand Up @@ -80,8 +80,8 @@ public DecompilationResult decompileClassFile(File classFile, File outputDir) th
{
DecompilerSettings settings = getDefaultSettings(outputDir);
MetadataSystem metadataSystem = new NoRetryMetadataSystem(settings.getTypeLoader());
this.decompileType(metadataSystem, typeName);
res.addDecompiled(name);
File outputFile = this.decompileType(metadataSystem, typeName);
res.addDecompiled(classFile.getAbsolutePath(), outputFile.getAbsolutePath());
}
catch (Throwable e)
{
Expand All @@ -96,6 +96,14 @@ public DecompilationResult decompileClassFile(File classFile, File outputDir) th

@Override
public DecompilationResult decompileDirectory(File classesDir, File outputDir) throws DecompilationException
{
DecompilationResult result = new DecompilationResult();
decompileDirectory(classesDir, outputDir, result);
return result;
}

public void decompileDirectory(File classesDir, File outputDir, DecompilationResult result)
throws DecompilationException
{
if (classesDir == null)
throw new IllegalArgumentException("Directory to decompile must not be null.");
Expand All @@ -115,22 +123,12 @@ public DecompilationResult decompileDirectory(File classesDir, File outputDir) t
DecompilerSettings settings = getDefaultSettings(outputDir);
MetadataSystem metadataSystem = new NoRetryMetadataSystem(settings.getTypeLoader());

DecompilationResult result = new DecompilationResult();

final List<File> files = Arrays.asList(classesDir.listFiles());
for (File file : files)
{
if (file.isDirectory())
{
DecompilationResult intermediateResult = decompileDirectory(file, new File(outputDir, file.getName()));
for (String decompiled : intermediateResult.getDecompiled())
{
result.addDecompiled(decompiled);
}
for (DecompilationFailure failure : intermediateResult.getFailures())
{
result.addFailure(failure);
}
decompileDirectory(file, new File(outputDir, file.getName()), result);
continue;
}

Expand All @@ -143,8 +141,8 @@ public DecompilationResult decompileDirectory(File classesDir, File outputDir) t

try
{
this.decompileType(metadataSystem, typeName);
result.addDecompiled(name);
File outputFile = this.decompileType(metadataSystem, typeName);
result.addDecompiled(file.getAbsolutePath(), outputFile.getAbsolutePath());
}
catch (Throwable e)
{
Expand All @@ -154,8 +152,6 @@ public DecompilationResult decompileDirectory(File classesDir, File outputDir) t
result.addFailure(failure);
}
}

return result;
}

@Override
Expand Down Expand Up @@ -212,8 +208,7 @@ public DecompilationResult decompileArchive(File archive, File outputDir) throws
File outputFile = this.decompileType(metadataSystem, typeName);
if (outputFile != null)
{
res.addDecompiled(name);
res.addDecompiledOutputFile(outputFile.getAbsolutePath());
res.addDecompiled(name, outputFile.getAbsolutePath());
}

// Taken from mstrobel's, not sure what's the purpose.
Expand Down
Expand Up @@ -3,7 +3,7 @@
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;
import java.util.Map;

import org.jboss.windup.config.GraphRewrite;
import org.jboss.windup.config.operation.ruleelement.AbstractIterationOperation;
Expand Down Expand Up @@ -56,11 +56,22 @@ public void perform(final GraphRewrite event, final EvaluationContext context, f
try
{
DecompilationResult result = decompiler.decompileArchive(archive, outputDir);
Set<String> decompiledOutputFileSet = result.getDecompiledOutputFiles();
Map<String, String> decompiledOutputFiles = result.getDecompiledFiles();

FileModelService fileService = new FileModelService(event.getGraphContext());
for (String decompiledOutputFile : decompiledOutputFileSet)
for (Map.Entry<String, String> decompiledEntry : decompiledOutputFiles.entrySet())
{
// original source is a path inside the archive... split it up by separator, and append
// it to the unzipped directory
String[] classFilePathTokens = decompiledEntry.getKey().split("\\\\|/");

Path classFilePath = Paths.get(payload.getUnzippedDirectory().getFilePath());
for (String pathToken : classFilePathTokens)
{
classFilePath = classFilePath.resolve(pathToken);
}

String decompiledOutputFile = decompiledEntry.getValue();
FileModel decompiledFileModel = fileService.getUniqueByProperty(FileModel.FILE_PATH,
decompiledOutputFile);

Expand All @@ -78,25 +89,27 @@ public void perform(final GraphRewrite event, final EvaluationContext context, f

if (decompiledOutputFile.endsWith(".java"))
{

if (!(decompiledFileModel instanceof JavaSourceFileModel))
{
decompiledFileModel = GraphService.addTypeToModel(event.getGraphContext(),
decompiledFileModel, JavaSourceFileModel.class);
}
JavaSourceFileModel decompiledSourceFileModel = (JavaSourceFileModel) decompiledFileModel;

Path classFilepath = Paths.get(decompiledOutputFile.substring(0,
decompiledOutputFile.length() - 5)
+ ".class");
FileModel classFileModel = fileService.getUniqueByProperty(
FileModel.FILE_PATH, classFilepath);
FileModel.FILE_PATH, classFilePath.toAbsolutePath().toString());
if (classFileModel != null && classFileModel instanceof JavaClassFileModel)
{
JavaClassFileModel classModel = (JavaClassFileModel) classFileModel;
classModel.getJavaClass().setDecompiledSource(decompiledSourceFileModel);
decompiledSourceFileModel.setPackageName(classModel.getPackageName());
}
else
{
throw new WindupException(
"Failed to find original JavaClassFileModel for decompiled Java file: "
+ decompiledOutputFile + " at: " + classFilePath.toString());
}
}
payload.addDecompiledFileModel(decompiledFileModel);
}
Expand Down

0 comments on commit a0ddaa4

Please sign in to comment.