Skip to content

Commit

Permalink
Refactor: avoid the EnhancedClassReader type spreading through the co…
Browse files Browse the repository at this point in the history
…debase
  • Loading branch information
luontola committed Aug 14, 2018
1 parent e5f6758 commit ea49b21
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 32 deletions.
@@ -1,4 +1,4 @@
// Copyright © 2013-2017 Esko Luontola and other Retrolambda contributors // Copyright © 2013-2018 Esko Luontola and other Retrolambda contributors
// This software is released under the Apache License 2.0. // This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0 // The license text is at http://www.apache.org/licenses/LICENSE-2.0


Expand All @@ -23,10 +23,10 @@ public class ClassAnalyzer {
private final Map<MethodRef, MethodRef> renamedLambdaMethods = new HashMap<>(); private final Map<MethodRef, MethodRef> renamedLambdaMethods = new HashMap<>();


public void analyze(byte[] bytecode, boolean isJavacHacksEnabled) { public void analyze(byte[] bytecode, boolean isJavacHacksEnabled) {
analyze(new EnhancedClassReader(bytecode, isJavacHacksEnabled)); analyze(EnhancedClassReader.create(bytecode, isJavacHacksEnabled));
} }


public void analyze(EnhancedClassReader cr) { public void analyze(ClassReader cr) {
ClassInfo c = new ClassInfo(cr); ClassInfo c = new ClassInfo(cr);
classes.put(c.type, c); classes.put(c.type, c);


Expand All @@ -38,7 +38,7 @@ public void analyze(EnhancedClassReader cr) {
analyzeClassOrInterface(c, cr); analyzeClassOrInterface(c, cr);
} }


private void analyzeClass(ClassInfo c, EnhancedClassReader cr) { private void analyzeClass(ClassInfo c, ClassReader cr) {
cr.accept(new ClassVisitor(ASM5) { cr.accept(new ClassVisitor(ASM5) {
private String owner; private String owner;


Expand All @@ -65,7 +65,7 @@ public MethodVisitor visitMethod(int access, String name, String desc, String si
}, ClassReader.SKIP_CODE); }, ClassReader.SKIP_CODE);
} }


private void analyzeInterface(ClassInfo c, EnhancedClassReader cr) { private void analyzeInterface(ClassInfo c, ClassReader cr) {
cr.accept(new ClassVisitor(ASM5) { cr.accept(new ClassVisitor(ASM5) {
private String owner; private String owner;
private String companion; private String companion;
Expand Down Expand Up @@ -101,7 +101,7 @@ public MethodVisitor visitMethod(int access, String name, String desc, String si
}, ClassReader.SKIP_CODE); }, ClassReader.SKIP_CODE);
} }


private void analyzeClassOrInterface(ClassInfo c, EnhancedClassReader cr) { private void analyzeClassOrInterface(ClassInfo c, ClassReader cr) {
cr.accept(new ClassVisitor(ASM5) { cr.accept(new ClassVisitor(ASM5) {
private String owner; private String owner;


Expand Down
@@ -1,10 +1,9 @@
// Copyright © 2013-2017 Esko Luontola and other Retrolambda contributors // Copyright © 2013-2018 Esko Luontola and other Retrolambda contributors
// This software is released under the Apache License 2.0. // This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0 // The license text is at http://www.apache.org/licenses/LICENSE-2.0


package net.orfjackal.retrolambda; package net.orfjackal.retrolambda;


import net.orfjackal.retrolambda.ext.ow2asm.EnhancedClassReader;
import net.orfjackal.retrolambda.interfaces.*; import net.orfjackal.retrolambda.interfaces.*;
import net.orfjackal.retrolambda.lambdas.*; import net.orfjackal.retrolambda.lambdas.*;
import net.orfjackal.retrolambda.requirenonnull.RequireNonNull; import net.orfjackal.retrolambda.requirenonnull.RequireNonNull;
Expand All @@ -27,7 +26,7 @@ public Transformers(int targetVersion, boolean defaultMethodsEnabled, ClassAnaly
this.analyzer = analyzer; this.analyzer = analyzer;
} }


public byte[] backportLambdaClass(EnhancedClassReader reader) { public byte[] backportLambdaClass(ClassReader reader) {
return transform(reader, (next) -> { return transform(reader, (next) -> {
if (defaultMethodsEnabled) { if (defaultMethodsEnabled) {
// Lambda classes are generated dynamically, so they were not // Lambda classes are generated dynamically, so they were not
Expand All @@ -44,7 +43,7 @@ public byte[] backportLambdaClass(EnhancedClassReader reader) {
}); });
} }


public byte[] backportClass(EnhancedClassReader reader) { public byte[] backportClass(ClassReader reader) {
return transform(reader, (next) -> { return transform(reader, (next) -> {
if (defaultMethodsEnabled) { if (defaultMethodsEnabled) {
next = new UpdateRelocatedMethodInvocations(next, analyzer); next = new UpdateRelocatedMethodInvocations(next, analyzer);
Expand All @@ -55,7 +54,7 @@ public byte[] backportClass(EnhancedClassReader reader) {
}); });
} }


public List<byte[]> backportInterface(EnhancedClassReader reader) { 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.
Expand Down Expand Up @@ -104,7 +103,7 @@ private byte[] transform(ClassNode node, ClassVisitorChain chain) {
return transform(node.name, node::accept, chain); return transform(node.name, node::accept, chain);
} }


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


Expand Down
@@ -1,4 +1,4 @@
// Copyright © 2013-2015 Esko Luontola <www.orfjackal.net> // Copyright © 2013-2018 Esko Luontola and other Retrolambda contributors
// This software is released under the Apache License 2.0. // This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0 // The license text is at http://www.apache.org/licenses/LICENSE-2.0


Expand All @@ -8,20 +8,21 @@


public class EnhancedClassReader extends ClassReader { public class EnhancedClassReader extends ClassReader {


private final boolean isJavacHacksEnabled; public static ClassReader create(byte[] bytecode, boolean isJavacHacksEnabled) {
if (isJavacHacksEnabled) {
return new EnhancedClassReader(bytecode);
} else {
return new ClassReader(bytecode);
}
}


public EnhancedClassReader(byte[] b, boolean isJavacHacksEnabled) { private EnhancedClassReader(byte[] b) {
super(b); super(b);
this.isJavacHacksEnabled = isJavacHacksEnabled;
} }


@Override @Override
protected Label readLabel(int offset, Label[] labels) { protected Label readLabel(int offset, Label[] labels) {
if (!isJavacHacksEnabled) {
return super.readLabel(offset, labels);
}
// A workaround suggested by Evgeny Mandrikov. See more: https://gitlab.ow2.org/asm/asm/issues/317845 // A workaround suggested by Evgeny Mandrikov. See more: https://gitlab.ow2.org/asm/asm/issues/317845
return super.readLabel(Math.min(offset, labels.length - 1), labels); return super.readLabel(Math.min(offset, labels.length - 1), labels);
} }

} }
@@ -1,10 +1,11 @@
// Copyright © 2013-2015 Esko Luontola <www.orfjackal.net> // Copyright © 2013-2018 Esko Luontola and other Retrolambda contributors
// This software is released under the Apache License 2.0. // This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0 // The license text is at http://www.apache.org/licenses/LICENSE-2.0


package net.orfjackal.retrolambda.files; package net.orfjackal.retrolambda.files;


import net.orfjackal.retrolambda.ext.ow2asm.EnhancedClassReader; import net.orfjackal.retrolambda.ext.ow2asm.EnhancedClassReader;
import org.objectweb.asm.ClassReader;


import java.io.IOException; import java.io.IOException;
import java.nio.file.*; import java.nio.file.*;
Expand All @@ -21,7 +22,7 @@ public void writeClass(byte[] bytecode, boolean isJavacHacksEnabled) throws IOEx
if (bytecode == null) { if (bytecode == null) {
return; return;
} }
EnhancedClassReader cr = new EnhancedClassReader(bytecode, isJavacHacksEnabled); ClassReader cr = EnhancedClassReader.create(bytecode, isJavacHacksEnabled);
Path relativePath = outputDir.getFileSystem().getPath(cr.getClassName() + ".class"); Path relativePath = outputDir.getFileSystem().getPath(cr.getClassName() + ".class");
writeFile(relativePath, bytecode); writeFile(relativePath, bytecode);
} }
Expand Down
@@ -1,18 +1,17 @@
// Copyright © 2013-2015 Esko Luontola <www.orfjackal.net> // Copyright © 2013-2018 Esko Luontola and other Retrolambda contributors
// This software is released under the Apache License 2.0. // This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0 // The license text is at http://www.apache.org/licenses/LICENSE-2.0


package net.orfjackal.retrolambda.interfaces; package net.orfjackal.retrolambda.interfaces;


import net.orfjackal.retrolambda.ext.ow2asm.EnhancedClassReader;
import net.orfjackal.retrolambda.util.Flags; import net.orfjackal.retrolambda.util.Flags;
import org.objectweb.asm.*; import org.objectweb.asm.*;


import java.util.*; import java.util.*;


public class ClassInfo { public class ClassInfo {


public final EnhancedClassReader reader; public final ClassReader reader;
private final int access; private final int access;
public final Type type; public final Type type;
public final Type superclass; public final Type superclass;
Expand All @@ -27,7 +26,7 @@ public ClassInfo() {
this.superclass = null; this.superclass = null;
} }


public ClassInfo(EnhancedClassReader cr) { public ClassInfo(ClassReader cr) {
this.reader = cr; this.reader = cr;
this.access = cr.getAccess(); this.access = cr.getAccess();
this.type = Type.getObjectType(cr.getClassName()); this.type = Type.getObjectType(cr.getClassName());
Expand Down
@@ -1,14 +1,13 @@
// Copyright © 2013-2017 Esko Luontola and other Retrolambda contributors // Copyright © 2013-2018 Esko Luontola and other Retrolambda contributors
// This software is released under the Apache License 2.0. // This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0 // The license text is at http://www.apache.org/licenses/LICENSE-2.0


package net.orfjackal.retrolambda.lambdas; package net.orfjackal.retrolambda.lambdas;


import com.esotericsoftware.minlog.Log; import com.esotericsoftware.minlog.Log;
import net.orfjackal.retrolambda.Transformers; import net.orfjackal.retrolambda.Transformers;
import net.orfjackal.retrolambda.files.OutputDirectory;

import net.orfjackal.retrolambda.ext.ow2asm.EnhancedClassReader; import net.orfjackal.retrolambda.ext.ow2asm.EnhancedClassReader;
import net.orfjackal.retrolambda.files.OutputDirectory;


import java.io.IOException; import java.io.IOException;


Expand All @@ -32,7 +31,7 @@ public void saveIfLambda(String className, byte[] bytecode) {


private void reifyLambdaClass(String className, byte[] bytecode) { private void reifyLambdaClass(String className, byte[] bytecode) {
Log.info("Saving lambda class: " + className); Log.info("Saving lambda class: " + className);
bytecode = transformers.backportLambdaClass(new EnhancedClassReader(bytecode, isJavacHacksEnabled)); bytecode = transformers.backportLambdaClass(EnhancedClassReader.create(bytecode, isJavacHacksEnabled));
try { try {
saver.writeClass(bytecode, isJavacHacksEnabled); saver.writeClass(bytecode, isJavacHacksEnabled);
} catch (IOException e) { } catch (IOException e) {
Expand Down
@@ -1,4 +1,4 @@
// Copyright © 2013-2014 Esko Luontola <www.orfjackal.net> // Copyright © 2013-2018 Esko Luontola and other Retrolambda contributors
// This software is released under the Apache License 2.0. // This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0 // The license text is at http://www.apache.org/licenses/LICENSE-2.0


Expand All @@ -24,7 +24,7 @@ public byte[] transform(ClassLoader loader, String className, Class<?> classBein
if (className == null) { if (className == null) {
// Since JDK 8 build b121 or so, lambda classes have a null class name, // Since JDK 8 build b121 or so, lambda classes have a null class name,
// but we can read it from the bytecode where the name still exists. // but we can read it from the bytecode where the name still exists.
className = new EnhancedClassReader(classfileBuffer, isJavacHacksEnabled).getClassName(); className = EnhancedClassReader.create(classfileBuffer, isJavacHacksEnabled).getClassName();
} }
if (lambdaClassSaver != null) { if (lambdaClassSaver != null) {
lambdaClassSaver.saveIfLambda(className, classfileBuffer); lambdaClassSaver.saveIfLambda(className, classfileBuffer);
Expand Down

0 comments on commit ea49b21

Please sign in to comment.