Skip to content

Commit 3e3e472

Browse files
committed
Added Dup and Pop code parts and changed version template: koresframework/Kores#46
1 parent 9b4535f commit 3e3e472

File tree

11 files changed

+383
-8
lines changed

11 files changed

+383
-8
lines changed

build.gradle

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

2020
group 'com.github.jonathanxd'
21-
version '3.1.1'
21+
version '3.1.0-3.1.2'
2222

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

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.github.jonathanxd.codeapi.bytecode.exception
2+
3+
import com.github.jonathanxd.codeapi.bytecode.BytecodeClass
4+
5+
class ClassCheckException(message: String, cause: Throwable, val bytecodeClasses: Array<out BytecodeClass>, val failedBytecodeClass: BytecodeClass) : RuntimeException(message, cause)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.bytecode.extra
29+
30+
import com.github.jonathanxd.codeapi.base.Typed
31+
32+
/**
33+
* CodeAPI-BytecodeWriter Dup part. This part will dup result of [part].
34+
*/
35+
data class Dup(val part: Typed) : Typed by part
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.bytecode.extra
29+
30+
import com.github.jonathanxd.codeapi.CodePart
31+
32+
/**
33+
* CodeAPI-BytecodeWriter Pop part. This part will pop the stack value.
34+
*
35+
* This class is commonly used in else statements to clear value created by [Dup] instruction. See the 'DupTest' test class.
36+
*/
37+
object Pop : CodePart

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ import com.github.jonathanxd.codeapi.bytecode.CHECK
3636
import com.github.jonathanxd.codeapi.bytecode.VISIT_LINES
3737
import com.github.jonathanxd.codeapi.bytecode.VisitLineType
3838
import com.github.jonathanxd.codeapi.bytecode.common.MVData
39+
import com.github.jonathanxd.codeapi.bytecode.exception.ClassCheckException
40+
import com.github.jonathanxd.codeapi.bytecode.extra.Dup
41+
import com.github.jonathanxd.codeapi.bytecode.extra.Pop
3942
import com.github.jonathanxd.codeapi.bytecode.gen.visitor.*
4043
import com.github.jonathanxd.codeapi.common.Data
4144
import com.github.jonathanxd.codeapi.exception.ProcessingException
@@ -102,6 +105,9 @@ class BytecodeGenerator @JvmOverloads constructor(val sourceFile: (TypeDeclarati
102105
addUncheckedVisitor(ClassDeclaration::class.java, TypeVisitor)
103106
addUncheckedVisitor(InterfaceDeclaration::class.java, TypeVisitor)
104107

108+
// Extra
109+
addVisitor(Dup::class.java, DupVisitor)
110+
addVisitor(Pop::class.java, PopVisitor)
105111
}
106112

107113
override val options: Options = this.options_
@@ -140,10 +146,13 @@ class BytecodeGenerator @JvmOverloads constructor(val sourceFile: (TypeDeclarati
140146
additional.methodVisitor.visitLineNumber(line, lbl)
141147

142148
}
143-
144-
return super.generateTo(partClass, codePart, extraData, consumer, additional).let {
145-
check(it)
146-
it
149+
try {
150+
return super.generateTo(partClass, codePart, extraData, consumer, additional).let {
151+
check(it)
152+
it
153+
}
154+
}catch (e: Throwable) {
155+
throw if(e is ProcessingException) throw e.cause?.let { it as? ClassCheckException ?: e } ?: e else e
147156
}
148157
}
149158

@@ -156,7 +165,7 @@ class BytecodeGenerator @JvmOverloads constructor(val sourceFile: (TypeDeclarati
156165
try {
157166
ClassReader(bytecode).accept(CheckClassAdapter(ClassNode(), true), 0)
158167
} catch (t: Throwable) {
159-
throw ProcessingException("Failed to check bytecode of class ${it.type.qualifiedName}", t)
168+
throw ClassCheckException("Failed to check bytecode of class ${it.type.qualifiedName}", t, classes, it)
160169
}
161170
}
162171
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.bytecode.gen.visitor
29+
30+
import com.github.jonathanxd.codeapi.bytecode.BytecodeClass
31+
import com.github.jonathanxd.codeapi.bytecode.common.MVData
32+
import com.github.jonathanxd.codeapi.bytecode.extra.Dup
33+
import com.github.jonathanxd.codeapi.common.Data
34+
import com.github.jonathanxd.codeapi.gen.visit.VisitorGenerator
35+
import com.github.jonathanxd.codeapi.gen.visit.VoidVisitor
36+
import org.objectweb.asm.Opcodes
37+
38+
object DupVisitor : VoidVisitor<Dup, BytecodeClass, MVData> {
39+
40+
override fun voidVisit(t: Dup, extraData: Data, visitorGenerator: VisitorGenerator<BytecodeClass>, additional: MVData) {
41+
val part = t.part
42+
visitorGenerator.generateTo(part::class.java, part, extraData, additional)
43+
additional.methodVisitor.visitInsn(Opcodes.DUP)
44+
}
45+
46+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.bytecode.gen.visitor
29+
30+
import com.github.jonathanxd.codeapi.bytecode.BytecodeClass
31+
import com.github.jonathanxd.codeapi.bytecode.common.MVData
32+
import com.github.jonathanxd.codeapi.bytecode.extra.Pop
33+
import com.github.jonathanxd.codeapi.common.Data
34+
import com.github.jonathanxd.codeapi.gen.visit.VisitorGenerator
35+
import com.github.jonathanxd.codeapi.gen.visit.VoidVisitor
36+
import org.objectweb.asm.Opcodes
37+
38+
object PopVisitor : VoidVisitor<Pop, BytecodeClass, MVData> {
39+
40+
override fun voidVisit(t: Pop, extraData: Data, visitorGenerator: VisitorGenerator<BytecodeClass>, additional: MVData) {
41+
additional.methodVisitor.visitInsn(Opcodes.POP)
42+
}
43+
44+
}

src/test/java/com/github/jonathanxd/codeapi/test/asm/CommonBytecodeTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.github.jonathanxd.codeapi.bytecode.BytecodeClass;
3434
import com.github.jonathanxd.codeapi.bytecode.BytecodeOptions;
3535
import com.github.jonathanxd.codeapi.bytecode.VisitLineType;
36+
import com.github.jonathanxd.codeapi.bytecode.exception.ClassCheckException;
3637
import com.github.jonathanxd.codeapi.bytecode.gen.BytecodeGenerator;
3738
import com.github.jonathanxd.iutils.annotation.Named;
3839
import com.github.jonathanxd.iutils.exception.RethrowException;
@@ -66,7 +67,13 @@ public class CommonBytecodeTest {
6667

6768
BCLoader bcLoader = new BCLoader();
6869

69-
BytecodeClass[] bytecodeClasses = bytecodeGenerator.gen(source);
70+
BytecodeClass[] bytecodeClasses;
71+
72+
try {
73+
bytecodeClasses = bytecodeGenerator.gen(source);
74+
} catch (ClassCheckException e) {
75+
bytecodeClasses = e.getBytecodeClasses();
76+
}
7077

7178
Class<?> first = null;
7279

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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.CodeAPI;
31+
import com.github.jonathanxd.codeapi.Types;
32+
import com.github.jonathanxd.codeapi.base.TypeDeclaration;
33+
import com.github.jonathanxd.codeapi.builder.ClassDeclarationBuilder;
34+
import com.github.jonathanxd.codeapi.builder.ConstructorDeclarationBuilder;
35+
import com.github.jonathanxd.codeapi.builder.IfStatementBuilder;
36+
import com.github.jonathanxd.codeapi.bytecode.extra.Dup;
37+
import com.github.jonathanxd.codeapi.bytecode.extra.Pop;
38+
import com.github.jonathanxd.codeapi.common.CodeModifier;
39+
import com.github.jonathanxd.codeapi.common.TypeSpec;
40+
import com.github.jonathanxd.codeapi.helper.Predefined;
41+
import com.github.jonathanxd.codeapi.literal.Literals;
42+
import com.github.jonathanxd.codeapi.util.Stack;
43+
44+
import org.junit.Test;
45+
46+
import java.util.Collections;
47+
48+
public class DupTest {
49+
50+
public static String getNull() {
51+
return null;
52+
}
53+
54+
public static String getNotNull() {
55+
return "Aa";
56+
}
57+
58+
@Test
59+
public void dupTest() {
60+
TypeDeclaration getNull = gen("getNull");
61+
62+
CommonBytecodeTest.test(this.getClass(), getNull, CodeAPI.source(getNull));
63+
64+
TypeDeclaration getNotNull = gen("getNotNull");
65+
66+
CommonBytecodeTest.test(this.getClass(), getNotNull, CodeAPI.source(getNotNull));
67+
}
68+
69+
private TypeDeclaration gen(String methodName) {
70+
return ClassDeclarationBuilder.builder()
71+
.withModifiers(CodeModifier.PUBLIC)
72+
.withQualifiedName("test.DupTest_" + methodName)
73+
.withSuperClass(Types.OBJECT)
74+
.withBody(CodeAPI.source(
75+
ConstructorDeclarationBuilder.builder()
76+
.withModifiers(CodeModifier.PUBLIC)
77+
.withBody(
78+
CodeAPI.source(Predefined.invokePrintlnStr(
79+
IfStatementBuilder.builder()
80+
.withExpressions(CodeAPI.checkNotNull(
81+
new Dup(CodeAPI.invokeStatic(DupTest.class, methodName, new TypeSpec(Types.STRING), Collections.emptyList()))
82+
))
83+
.withBody(CodeAPI.source(Stack.INSTANCE))
84+
.withElseStatement(CodeAPI.source(Pop.INSTANCE, Literals.STRING("NULL")))
85+
.build()
86+
))
87+
)
88+
.build()
89+
))
90+
.build();
91+
}
92+
}

0 commit comments

Comments
 (0)