Skip to content

Commit 383ccef

Browse files
committed
Fixed Class literal bug (hotfix-5)
1 parent 234fc4e commit 383ccef

File tree

3 files changed

+126
-1
lines changed

3 files changed

+126
-1
lines changed

src/main/kotlin/com/github/jonathanxd/codeapi/bytecode/util/LiteralUtil.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ object LiteralUtil {
9494

9595
mv.visitFieldInsn(Opcodes.GETSTATIC, wrapperTypeSpec, "TYPE", classType)
9696
} else {
97-
mv.visitLdcInsn(type)
97+
val asmType = Type.getType(type.javaSpecName)
98+
mv.visitLdcInsn(asmType)
9899
}
99100
} else {
100101
throw IllegalArgumentException("Cannot handle literal: $num")
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* CodeAPI-BytecodeWriter - Framework to generate Java code and Bytecode code. <https://github.com/JonathanxD/CodeAPI-BytecodeWriter>
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 TheRealBuggy/JonathanxD (https://github.com/JonathanxD/ & https://github.com/TheRealBuggy/) <[email protected]>
7+
* Copyright (c) contributors
8+
*
9+
*
10+
* Permission is hereby granted, free of charge, to any person obtaining a copy
11+
* of this software and associated documentation files (the "Software"), to deal
12+
* in the Software without restriction, including without limitation the rights
13+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
* copies of the Software, and to permit persons to whom the Software is
15+
* furnished to do so, subject to the following conditions:
16+
*
17+
* The above copyright notice and this permission notice shall be included in
18+
* all copies or substantial portions of the Software.
19+
*
20+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26+
* THE SOFTWARE.
27+
*/
28+
package com.github.jonathanxd.codeapi.test.asm;
29+
30+
import com.github.jonathanxd.codeapi.CodeSource;
31+
import com.github.jonathanxd.codeapi.base.TypeDeclaration;
32+
import com.github.jonathanxd.codeapi.bytecode.BytecodeClass;
33+
import com.github.jonathanxd.codeapi.bytecode.classloader.CodeClassLoader;
34+
import com.github.jonathanxd.codeapi.bytecode.gen.BytecodeGenerator;
35+
import com.github.jonathanxd.codeapi.common.CodeParameter;
36+
import com.github.jonathanxd.codeapi.helper.Predefined;
37+
import com.github.jonathanxd.codeapi.literal.Literals;
38+
39+
import org.junit.Test;
40+
41+
import java.util.EnumSet;
42+
43+
import static com.github.jonathanxd.codeapi.CodeAPI.sourceOfParts;
44+
import static com.github.jonathanxd.codeapi.Types.VOID;
45+
import static com.github.jonathanxd.codeapi.common.CodeModifier.PUBLIC;
46+
import static com.github.jonathanxd.codeapi.factory.ClassFactory.aClass;
47+
import static com.github.jonathanxd.codeapi.factory.MethodFactory.method;
48+
49+
public class LiteralBugTest {
50+
51+
52+
@Test
53+
public void literalBugTest() throws Throwable {
54+
CodeSource source = sourceOfParts(
55+
method(EnumSet.of(PUBLIC), "test", VOID, new CodeParameter[]{},
56+
sourceOfParts(
57+
Predefined.invokePrintln(Literals.CLASS(Integer.class))
58+
)
59+
)
60+
);
61+
62+
TypeDeclaration decl = aClass(EnumSet.of(PUBLIC), "com.MyClass", source);
63+
64+
BytecodeGenerator bytecodeGenerator = new BytecodeGenerator();
65+
66+
BytecodeClass[] gen = bytecodeGenerator.gen(decl);
67+
68+
ResultSaver.save(this.getClass(), gen);
69+
70+
CodeClassLoader codeClassLoader = new CodeClassLoader();
71+
72+
Class<?> define = codeClassLoader.define(gen);
73+
74+
define.getDeclaredMethod("test").invoke(define.newInstance());
75+
76+
77+
}
78+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
md5: f08e583d0e08e79c9171c65bfb8eec6d
2+
3+
version: Java 8 (52)
4+
access: ACC_PUBLIC (33)
5+
6+
source: MyClass.cai
7+
8+
public class com.MyClass extends java.lang.Object {
9+
10+
!access: ACC_PUBLIC (1)
11+
public void test() {
12+
desc: ()V
13+
maxStack: 2, maxLocals: 1
14+
Label_0:
15+
getstatic java.lang.System.out (type: java.io.PrintStream)
16+
ldc Ljava/lang/Integer; // type: java.lang.Class
17+
invokevirtual java.io.PrintStream.println(java.lang.Object)void (ownerIsInterface: false)
18+
return
19+
Label_1:
20+
LocalVariables {
21+
index: 0, name: this, start: Label_0, end: Label_1, type: com.MyClass, signature: null
22+
}
23+
}
24+
25+
!access: ACC_PUBLIC (1)
26+
public void <init>() {
27+
desc: ()V
28+
maxStack: 1, maxLocals: 1
29+
Label_0:
30+
aload 0
31+
invokespecial java.lang.Object.<init>()void (ownerIsInterface: false)
32+
return
33+
Label_1:
34+
LocalVariables {
35+
index: 0, name: this, start: Label_0, end: Label_1, type: com.MyClass, signature: null
36+
}
37+
}
38+
39+
!access: PACKAGE_PRIVATE, ACC_STATIC (8)
40+
static void <clinit>() {
41+
desc: ()V
42+
maxStack: 0, maxLocals: 0
43+
return
44+
}
45+
46+
}

0 commit comments

Comments
 (0)