forked from objectionary/jeo-maven-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(objectionary#528): suggest ugly solution
- Loading branch information
1 parent
c352335
commit 4dd3098
Showing
4 changed files
with
117 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/org/eolang/jeo/representation/bytecode/CustomClassVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.eolang.jeo.representation.bytecode; | ||
|
||
import java.lang.reflect.Field; | ||
import org.eolang.jeo.representation.DefaultVersion; | ||
import org.objectweb.asm.ClassReader; | ||
import org.objectweb.asm.ClassVisitor; | ||
import org.objectweb.asm.ClassWriter; | ||
import org.objectweb.asm.MethodVisitor; | ||
import org.objectweb.asm.tree.ClassNode; | ||
|
||
public final class CustomClassVisitor extends ClassVisitor { | ||
|
||
private final CustomClassWriter writer; | ||
|
||
|
||
public CustomClassVisitor(final int api) { | ||
this(api, new CustomClassWriter()); | ||
} | ||
|
||
public CustomClassVisitor(final CustomClassWriter writer) { | ||
this(new DefaultVersion().api(), writer); | ||
} | ||
|
||
public CustomClassVisitor(final int api, final CustomClassWriter writer) { | ||
super(api, writer); | ||
this.writer = writer; | ||
} | ||
|
||
public MethodVisitor visitCustomMethodWithComputation( | ||
final int access, | ||
final String name, | ||
final String descriptor, | ||
final String signature, | ||
final String[] exceptions | ||
) { | ||
|
||
try { | ||
final Field field = ClassWriter.class.getDeclaredField("compute"); | ||
field.setAccessible(true); | ||
field.setInt(this.writer, 4); | ||
final MethodVisitor original = this.visitMethod( | ||
access, name, descriptor, signature, exceptions | ||
); | ||
field.setInt(this.writer, 0); | ||
return original; | ||
} catch (final NoSuchFieldException | IllegalAccessException exception) { | ||
throw new RuntimeException(exception); | ||
} | ||
} | ||
|
||
public Bytecode bytecode() { | ||
return new Bytecode(this.writer.toByteArray()); | ||
} | ||
} |