Skip to content

Commit

Permalink
Fixed build failure on Java 5 due to lowering the bytecode version to…
Browse files Browse the repository at this point in the history
…o early
  • Loading branch information
luontola committed Apr 14, 2015
1 parent 7146fa7 commit 1952d2e
Showing 1 changed file with 22 additions and 12 deletions.
Expand Up @@ -8,8 +8,10 @@
import net.orfjackal.retrolambda.lambdas.*; import net.orfjackal.retrolambda.lambdas.*;
import net.orfjackal.retrolambda.trywithresources.SwallowSuppressedExceptions; import net.orfjackal.retrolambda.trywithresources.SwallowSuppressedExceptions;
import org.objectweb.asm.*; import org.objectweb.asm.*;
import org.objectweb.asm.tree.ClassNode;


import java.util.*; import java.util.*;
import java.util.function.Consumer;


public class Transformers { public class Transformers {


Expand Down Expand Up @@ -55,19 +57,19 @@ public List<byte[]> backportInterface(ClassReader reader) {
// The lambdas must be backported only once, because bad things will happen if a lambda // The lambdas must be backported only once, because bad things will happen if a lambda
// is called by different class name in the interface and its companion class, and then // is called by different class name in the interface and its companion class, and then
// the wrong one of them is written to disk last. // the wrong one of them is written to disk last.
byte[] lambdasBackported = transform(reader, (next) -> { ClassNode lambdasBackported = new ClassNode();
next = new BackportLambdaInvocations(next); ClassVisitor next = lambdasBackported;
return next; next = new BackportLambdaInvocations(next);
}); reader.accept(next, 0);


List<byte[]> results = new ArrayList<>(); List<byte[]> results = new ArrayList<>();
results.add(backportInterface2(new ClassReader(lambdasBackported))); results.add(backportInterface2(lambdasBackported));
results.addAll(extractInterfaceCompanion(new ClassReader(lambdasBackported))); results.addAll(extractInterfaceCompanion(lambdasBackported));
return results; return results;
} }


private byte[] backportInterface2(ClassReader reader) { private byte[] backportInterface2(ClassNode clazz) {
return transform(reader, (next) -> { return transform(clazz, (next) -> {
if (defaultMethodsEnabled) { if (defaultMethodsEnabled) {
next = new RemoveStaticMethods(next); next = new RemoveStaticMethods(next);
next = new RemoveDefaultMethodBodies(next); next = new RemoveDefaultMethodBodies(next);
Expand All @@ -84,19 +86,27 @@ private byte[] backportInterface2(ClassReader reader) {
}); });
} }


private List<byte[]> extractInterfaceCompanion(ClassReader reader) { private List<byte[]> extractInterfaceCompanion(ClassNode clazz) {
Optional<Type> companion = analyzer.getCompanionClass(Type.getObjectType(reader.getClassName())); Optional<Type> companion = analyzer.getCompanionClass(Type.getObjectType(clazz.name));
if (!companion.isPresent()) { if (!companion.isPresent()) {
return Collections.emptyList(); return Collections.emptyList();
} }
return Arrays.asList(transform(reader, (next) -> { return Arrays.asList(transform(clazz, (next) -> {
next = new UpdateRelocatedMethodInvocations(next, analyzer); next = new UpdateRelocatedMethodInvocations(next, analyzer);
next = new ExtractInterfaceCompanionClass(next, companion.get()); next = new ExtractInterfaceCompanionClass(next, companion.get());
return next; return next;
})); }));
} }


private byte[] transform(ClassNode node, ClassVisitorChain chain) {
return transform(node::accept, chain);
}

private byte[] transform(ClassReader reader, ClassVisitorChain chain) { private byte[] transform(ClassReader reader, ClassVisitorChain chain) {
return transform(cv -> reader.accept(cv, 0), chain);
}

private byte[] transform(Consumer<ClassVisitor> reader, ClassVisitorChain chain) {
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS); ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
ClassVisitor next = writer; ClassVisitor next = writer;


Expand All @@ -107,7 +117,7 @@ private byte[] transform(ClassReader reader, ClassVisitorChain chain) {
next = new FixInvokeStaticOnInterfaceMethod(next); next = new FixInvokeStaticOnInterfaceMethod(next);
next = chain.wrap(next); next = chain.wrap(next);


reader.accept(next, 0); reader.accept(next);
return writer.toByteArray(); return writer.toByteArray();
} }


Expand Down

0 comments on commit 1952d2e

Please sign in to comment.