Skip to content

Commit c487281

Browse files
committed
Support local classes fixes #8.
1 parent faa433d commit c487281

File tree

7 files changed

+187
-3
lines changed

7 files changed

+187
-3
lines changed

CodeAPI

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ buildscript {
1818
}
1919

2020
group 'com.github.jonathanxd'
21-
version '3.0.3-hotfix4'
21+
version '3.1.0'
2222

2323
apply from: project(":CodeAPI").file("gradle/common.gradle")
2424

2525
dependencies {
2626
compile project(":CodeAPI")
27-
compile 'com.github.JonathanxD:BytecodeDisassembler:2.0.3-c2'
27+
compile 'com.github.JonathanxD:BytecodeDisassembler:2.0.3-d'
2828
compile group: 'org.ow2.asm', name: 'asm-all', version: '5.1'
2929
testCompile project(path: ':CodeAPI', configuration: 'tests')
3030
}

src/main/kotlin/com/github/jonathanxd/codeapi/bytecode/gen/visitor/TypeVisitor.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,29 @@ object TypeVisitor : Visitor<TypeDeclaration, BytecodeClass, Any?> {
6969

7070
override fun visit(t: TypeDeclaration, extraData: Data, visitorGenerator: VisitorGenerator<BytecodeClass>, additional: Any?): Array<BytecodeClass> {
7171

72+
/*val extraData = if(additional == null)
73+
data
74+
else
75+
data.clone()*/
76+
7277
val baseDataClone = extraData.clone()
7378

79+
/*if(additional != null) {
80+
val outer = data.getRequired<TypeDeclaration>(TYPE_DECLARATION_REPRESENTATION)
81+
val cw = data.getRequired<ClassVisitor>(CLASS_VISITOR_REPRESENTATION)
82+
83+
if(!outer.`is`(t.outerClass))
84+
throw IllegalArgumentException("Outer class specified to '" + t + "' don't matches the real outer class. " +
85+
"Specified: '" + t.outerClass + "'," +
86+
"Real: '" + outer + "'!")
87+
88+
val inn = Util.visitInner(cw, outer, listOf(t))
89+
90+
91+
92+
data.registerData(INNER_TYPE_REPRESENTATION, )
93+
}*/
94+
7495
val cw = ClassWriter(ClassWriter.COMPUTE_MAXS or ClassWriter.COMPUTE_FRAMES)
7596

7697

src/main/kotlin/com/github/jonathanxd/codeapi/bytecode/gen/visitor/Util.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ object Util {
107107
} else {
108108
if (part is TypeDeclaration) {
109109
typeDeclarationList.add(part)
110+
} else if (part is MethodDeclaration) {
111+
val grabRemovePair = Util.grabAndRemoveInnerDecl(part.body)
112+
113+
typeDeclarationList.addAll(grabRemovePair.first)
114+
115+
val modifiedMethod = part.builder().withBody(grabRemovePair.second).build()
116+
117+
codeSource.add(modifiedMethod)
118+
110119
} else {
111120
codeSource.add(part)
112121
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.test.CommonGen;
33+
import com.github.jonathanxd.codeapi.test.LocalClassTest_;
34+
import com.github.jonathanxd.iutils.annotation.Named;
35+
import com.github.jonathanxd.iutils.object.Pair;
36+
37+
import org.junit.Test;
38+
39+
public class LocalClassTest {
40+
@Test
41+
public void localTest() {
42+
Pair<@Named("Main class") TypeDeclaration, @Named("Source") CodeSource> $ = LocalClassTest_.$();
43+
44+
Class<?> define = CommonBytecodeTest.test(this.getClass(), $._1(), $._2(), aClass -> aClass);
45+
46+
try {
47+
define.newInstance();
48+
} catch (InstantiationException | IllegalAccessException e) {
49+
throw new RuntimeException(e);
50+
}
51+
52+
}
53+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
md5: 45db4018fea4313629a30a39a3762409
2+
3+
version: Java 8 (52)
4+
access: PACKAGE_PRIVATE (32)
5+
6+
source: LocalClass.cai
7+
8+
class test.LocalClass$Greeter extends java.lang.Object {
9+
10+
!access: PACKAGE_PRIVATE (0)
11+
test.LocalClass.Greeter -> test.LocalClass$Greeter
12+
13+
!access: ACC_PRIVATE, ACC_FINAL (18)
14+
private final test.LocalClass localClass$outer1
15+
16+
!access: ACC_PUBLIC (1)
17+
public void greet() {
18+
desc: ()V
19+
maxStack: 2, maxLocals: 1
20+
Label_0:
21+
LINE 5 -> Label_0
22+
getstatic java.lang.System.out (type: java.io.PrintStream)
23+
ldc "Hello" // type: java.lang.String
24+
invokevirtual java.io.PrintStream.println(java.lang.String)void (ownerIsInterface: false)
25+
return
26+
Label_1:
27+
LocalVariables {
28+
index: 0, name: this, start: Label_0, end: Label_1, type: test.LocalClass$Greeter, signature: null
29+
}
30+
}
31+
32+
!access: ACC_PUBLIC (1)
33+
public void <init>(test.LocalClass) {
34+
desc: (Ltest/LocalClass;)V
35+
maxStack: 2, maxLocals: 2
36+
Label_0:
37+
aload 0
38+
invokespecial java.lang.Object.<init>()void (ownerIsInterface: false)
39+
Label_1:
40+
LINE 6 -> Label_1
41+
LINE 7 -> Label_1
42+
aload 0
43+
aload 1
44+
putfield test.LocalClass$Greeter.localClass$outer1 (type: test.LocalClass)
45+
return
46+
Label_2:
47+
LocalVariables {
48+
index: 1, name: localClass$outer1, start: Label_0, end: Label_2, type: test.LocalClass, signature: null
49+
index: 0, name: this, start: Label_0, end: Label_2, type: test.LocalClass$Greeter, signature: null
50+
}
51+
}
52+
53+
!access: PACKAGE_PRIVATE, ACC_STATIC (8)
54+
static void <clinit>() {
55+
desc: ()V
56+
maxStack: 0, maxLocals: 0
57+
return
58+
}
59+
60+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
md5: 66df9ef26ab3e5f26bb2690aa7f1e097
2+
3+
version: Java 8 (52)
4+
access: ACC_PUBLIC (33)
5+
6+
source: LocalClass.cai
7+
8+
public class test.LocalClass extends java.lang.Object {
9+
10+
!access: PACKAGE_PRIVATE (0)
11+
test.LocalClass.Greeter -> test.LocalClass$Greeter
12+
13+
!access: ACC_PUBLIC (1)
14+
public void <init>() {
15+
desc: ()V
16+
maxStack: 3, maxLocals: 1
17+
Label_0:
18+
aload 0
19+
invokespecial java.lang.Object.<init>()void (ownerIsInterface: false)
20+
Label_1:
21+
LINE 3 -> Label_1
22+
new test.LocalClass$Greeter
23+
dup
24+
aload 0
25+
invokespecial test.LocalClass$Greeter.<init>(test.LocalClass)void (ownerIsInterface: false)
26+
invokevirtual test.LocalClass$Greeter.greet()void (ownerIsInterface: false)
27+
return
28+
Label_2:
29+
LocalVariables {
30+
index: 0, name: this, start: Label_0, end: Label_2, type: test.LocalClass, signature: null
31+
}
32+
}
33+
34+
!access: PACKAGE_PRIVATE, ACC_STATIC (8)
35+
static void <clinit>() {
36+
desc: ()V
37+
maxStack: 0, maxLocals: 0
38+
return
39+
}
40+
41+
}

0 commit comments

Comments
 (0)