Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
teletha committed Nov 19, 2024
1 parent d88be08 commit 988f802
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 11 deletions.
61 changes: 61 additions & 0 deletions src/main/java/reincarnation/JavaAnnotationDecompiler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2024 Nameless Production Committee
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://opensource.org/licenses/mit-license.php
*/
package reincarnation;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Opcodes;

import reincarnation.meta.AnnotationMeta;
import reincarnation.meta.AnnotationsMeta;

public class JavaAnnotationDecompiler extends AnnotationVisitor {

/** The metadata holder. */
private final AnnotationMeta meta;

public JavaAnnotationDecompiler(Class clazz, AnnotationsMeta meta) {
super(Opcodes.ASM9);

System.out.println(clazz);
this.meta = new AnnotationMeta(clazz);
}

/**
* {@inheritDoc}
*/
@Override
public void visit(String name, Object value) {
super.visit(name, value);
}

/**
* {@inheritDoc}
*/
@Override
public AnnotationVisitor visitAnnotation(String name, String descriptor) {
return super.visitAnnotation(name, descriptor);
}

/**
* {@inheritDoc}
*/
@Override
public AnnotationVisitor visitArray(String name) {
return super.visitArray(name);
}

/**
* {@inheritDoc}
*/
@Override
public void visitEnum(String name, String descriptor, String value) {
super.visitEnum(name, descriptor, value);
}
}
13 changes: 13 additions & 0 deletions src/main/java/reincarnation/JavaClassDecompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,24 @@
import java.lang.reflect.Method;
import java.util.Objects;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Type;

import kiss.I;
import reincarnation.meta.AnnotationsMeta;
import reincarnation.util.GeneratedCodes;

class JavaClassDecompiler extends ClassVisitor {

/** The current processing source. */
private final Reincarnation source;

/** The annotation holder. */
private final AnnotationsMeta meta = new AnnotationsMeta();

/**
* Java class decompiler
*/
Expand All @@ -51,6 +56,14 @@ public void visit(int version, int access, String name, String signature, String
}
}

/**
* {@inheritDoc}
*/
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
return new JavaAnnotationDecompiler(OperandUtil.load(Type.getType(desc)), meta);
}

/**
* {@inheritDoc}
*/
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/reincarnation/JavaMethodDecompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import reincarnation.coder.Code;
import reincarnation.coder.Coder;
import reincarnation.coder.Naming;
import reincarnation.meta.AnnotationsMeta;
import reincarnation.operator.AccessMode;
import reincarnation.operator.AssignOperator;
import reincarnation.operator.BinaryOperator;
Expand Down Expand Up @@ -279,6 +280,8 @@ class JavaMethodDecompiler extends MethodVisitor implements Code, Naming, NodeMa
/** The label aware actions. */
private final MultiMap<Node, WiseConsumer<Node>> ends = new MultiMap(false);

private final AnnotationsMeta annotations = new AnnotationsMeta();

/**
* @param source
* @param locals
Expand Down Expand Up @@ -328,7 +331,8 @@ public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
if (desc.equals(DEBUGGER)) {
debugger.enable = true;
}
return null; // do nothing
return new JavaAnnotationDecompiler(OperandUtil.load(Type.getType(desc)), annotations); // do
// nothing
}

/**
Expand Down
20 changes: 13 additions & 7 deletions src/main/java/reincarnation/coder/java/JavaCoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1225,13 +1225,17 @@ private void writeAnnotation(AnnotatedElement element) {
*/
private String writeAnnotationValue(Object value) {
if (value instanceof Annotation a) {
return "@" + name(a.annotationType()) + Join.of(a.annotationType().getDeclaredMethods())
.ignoreEmpty()
.prefix("(")
.separator("," + space)
.suffix(")")
.skip(m -> Objects.deepEquals(readAnnotationValue(a, m), m.getDefaultValue()))
.converter(m -> m.getName() + space + "=" + space + writeAnnotationValue(readAnnotationValue(a, m)));
try {
return "@" + name(a.annotationType()) + Join.of(a.annotationType().getDeclaredMethods())
.ignoreEmpty()
.prefix("(")
.separator("," + space)
.suffix(")")
.skip(m -> Objects.deepEquals(readAnnotationValue(a, m), m.getDefaultValue()))
.converter(m -> m.getName() + space + "=" + space + writeAnnotationValue(readAnnotationValue(a, m)));
} catch (Exception e) {
return a.toString();
}
} else if (value instanceof String) {
return "\"" + value + "\"";
} else if (value instanceof Character) {
Expand Down Expand Up @@ -1265,6 +1269,8 @@ private String writeAnnotationValue(Object value) {
*/
private Object readAnnotationValue(Annotation annotation, Method method) {
try {
System.out.println(annotation + " " + method);
method.setAccessible(true);
return method.invoke(annotation);
} catch (Exception e) {
throw I.quiet(e);
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/reincarnation/meta/AnnotationMeta.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2024 Nameless Production Committee
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://opensource.org/licenses/mit-license.php
*/
package reincarnation.meta;

import java.util.LinkedHashMap;
import java.util.Map;

public class AnnotationMeta {

public final Class clazz;

public final Map<String, Object> values = new LinkedHashMap();

public AnnotationMeta(Class clazz) {
this.clazz = clazz;
}
}
18 changes: 18 additions & 0 deletions src/main/java/reincarnation/meta/AnnotationsMeta.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (C) 2024 Nameless Production Committee
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://opensource.org/licenses/mit-license.php
*/
package reincarnation.meta;

import java.util.ArrayList;
import java.util.List;

public class AnnotationsMeta {

public final List<AnnotationMeta> annotations = new ArrayList();
}
9 changes: 8 additions & 1 deletion src/test/java/reincarnation/ReincarnationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
import java.lang.constant.ConstantDesc;
import java.time.temporal.ChronoField;
import java.util.AbstractCollection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

import org.junit.jupiter.api.Test;

Expand All @@ -30,7 +32,7 @@ void rebirth() {

@Test
void rebirthCoreClass() {
// Reincarnation.rebirth(ArrayList.class);
Reincarnation.rebirth(ArrayList.class);
Reincarnation.rebirth(Properties.class);
}

Expand Down Expand Up @@ -63,4 +65,9 @@ void rebirthCoreAnnotation() {
Reincarnation.rebirth(Retention.class);
Reincarnation.rebirth(Deprecated.class);
}

@Test
void unexportedAnnotationAccess() {
Reincarnation.rebirth(ConcurrentHashMap.class);
}
}
4 changes: 2 additions & 2 deletions src/test/java/reincarnation/Sample.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
*/
package reincarnation;

import java.lang.invoke.MethodHandle;
import java.util.concurrent.ConcurrentHashMap;

import reincarnation.coder.java.JavaCodingOption;

public class Sample {

public static void main(String[] args) {
System.out.println(Reincarnation.rebirth(MethodHandle.class, new JavaCodingOption()));
System.out.println(Reincarnation.rebirth(ConcurrentHashMap.class, new JavaCodingOption()));
}
}

0 comments on commit 988f802

Please sign in to comment.