Skip to content

Commit

Permalink
Add class resolver function to ClassDataEntryWriter
Browse files Browse the repository at this point in the history
This diff allows you to pass in a custom class resolver function for the ClassDataEntryWriter.
  • Loading branch information
bengt-GS authored and rubenpieters committed Apr 11, 2024
1 parent d796d3e commit f1e922e
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions base/src/main/java/proguard/io/ClassDataEntryWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,53 @@
*/
package proguard.io;

import java.io.*;
import proguard.classfile.*;
import java.io.DataOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import proguard.classfile.ClassConstants;
import proguard.classfile.ClassPool;
import proguard.classfile.Clazz;
import proguard.classfile.io.ProgramClassWriter;
import proguard.classfile.visitor.ClassVisitor;

/**
* This {@link DataEntryWriter} finds received class entries in the given class pool and writes them
* out to the given data entry writer. For resource entries, it returns valid output streams. For
* class entries, it returns output streams that must not be used.
*
* @see IdleRewriter
* @author Eric Lafortune
* @see IdleRewriter
*/
public class ClassDataEntryWriter implements DataEntryWriter {
private final ClassPool classPool;
private final DataEntryWriter dataEntryWriter;

private final ClassVisitor extraClassVisitor;

/**
* Creates a new ClassDataEntryWriter.
*
* @param classPool the class pool in which classes are found.
* @param dataEntryWriter the writer to which the class file is written.
*/
public ClassDataEntryWriter(ClassPool classPool, DataEntryWriter dataEntryWriter) {
this(classPool, dataEntryWriter, null);
}

/**
* Creates a new ClassDataEntryWriter.
*
* @param classPool the class pool in which classes are found.
* @param dataEntryWriter the writer to which the class file is written.
* @param extraClassVisitor a class visitor that will be visited for each class that is written.
*/
public ClassDataEntryWriter(
ClassPool classPool, DataEntryWriter dataEntryWriter, ClassVisitor extraClassVisitor) {
this.classPool = classPool;
this.dataEntryWriter = dataEntryWriter;
this.extraClassVisitor = extraClassVisitor;
}

// Implementations for DataEntryWriter.
Expand Down Expand Up @@ -82,6 +104,9 @@ public OutputStream createOutputStream(DataEntry dataEntry) throws IOException {
DataOutputStream classOutputStream = new DataOutputStream(outputStream);
try {
clazz.accept(new ProgramClassWriter(classOutputStream));
if (extraClassVisitor != null) {
clazz.accept(extraClassVisitor);
}
} catch (RuntimeException e) {
throw (RuntimeException)
new RuntimeException(
Expand Down

0 comments on commit f1e922e

Please sign in to comment.