Skip to content

Commit

Permalink
GH-624 ClassGenerator breaks Java interop by adding _ to method names (
Browse files Browse the repository at this point in the history
…Fix #624)
  • Loading branch information
dzikoysk committed Mar 1, 2021
1 parent 6f498bd commit 32c6d16
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/swing.panda
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ main {

type Window : Runnable {

override run() {
override run () -> PrimitiveVoid {
JFrame frame = new JFrame('MonkaS')
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
frame.setSize(720, 380)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ public final class ClassGenerator {
public CtClass allocate(Type type) {
String javaName = type.getName()
.replace("/", "$")
.replace(".", "$")
.replace("@", "$")
.replace("::", "$")
.replace(":", "_")
.replace(":", "")
.replace("<", "")
.replace(">", "")
+ "_" + ID.incrementAndGet();

CtClass javaType = Kind.isInterface(type)
Expand Down Expand Up @@ -161,7 +164,7 @@ public void generate(Type type) throws CannotCompileException, NotFoundException
Map<String, TypeMethod> methods = new HashMap<>();

for (TypeMethod method : type.getMethods().getDeclaredProperties()) {
String generatedName = "_" + method.getSimpleName().replaceAll("[^A-Za-z0-9]", "");
String generatedName = (method.isOverriding() ? "" : "_") + method.getSimpleName().replaceAll("[^A-Za-z0-9_$]", "");

CtClass[] parameters = ClassPoolUtils.toCt(ParameterUtils.parametersToClasses(method.getParameters()));
CtMethod javaMethod = new CtMethod(getCtClass(method.getReturnType().getKnownType()), generatedName, parameters, javaType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public final class PandaMethod extends AbstractParametrizedMember<TypeMethod> im
private final boolean isAbstract;
private final boolean isStatic;
private final boolean isNative;
private final boolean isOverriding;

protected PandaMethod(PandaMethodBuilder builder) {
super(builder);
Expand All @@ -36,6 +37,7 @@ protected PandaMethod(PandaMethodBuilder builder) {
this.isAbstract = builder.isAbstract;
this.isNative = builder.isNative;
this.isStatic = builder.isStatic;
this.isOverriding = builder.isOverriding;
}

@Override
Expand All @@ -58,6 +60,11 @@ public boolean isStatic() {
return isStatic;
}

@Override
public boolean isOverriding() {
return isOverriding;
}

@Override
public String getName() {
return getType() + "#" + getSimpleName() + "(" + ParameterUtils.toString(getParameters()) + ") → " + getReturnType();
Expand All @@ -73,6 +80,7 @@ public static final class PandaMethodBuilder extends PandaParametrizedExecutable
protected boolean isAbstract;
protected boolean isStatic;
protected boolean isNative;
protected boolean isOverriding;

private PandaMethodBuilder() { }

Expand All @@ -90,6 +98,11 @@ public PandaMethodBuilder isAbstract(boolean isAbstract) {
return this;
}

public PandaMethodBuilder isOverriding(boolean isOverriding) {
this.isOverriding = isOverriding;
return this;
}

public PandaMethodBuilder isStatic(boolean isStatic) {
this.isStatic = isStatic;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public interface TypeMethod extends ParametrizedMember {
*/
boolean isNative();

boolean isOverriding();

/**
* Check if method is static
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
package org.panda_lang.panda.examples

import groovy.transform.CompileStatic
import org.junit.jupiter.api.Test

@CompileStatic
final class SwingExample extends PandaTestSpecification {

static void main(String[] args) {
@Test
void 'should compile and handle swing bindings' () {
launch '/', 'swing.panda'
}

Expand Down
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,11 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<version>3.0.0-M5</version>
<configuration>
<!-- Activate the use of TCP to transmit events to the plugin -->
<forkNode implementation="org.apache.maven.plugin.surefire.extensions.SurefireForkNodeFactory"/>
</configuration>
</plugin>

<!-- explicitly define maven-deploy-plugin after other to force exec order -->
Expand Down

0 comments on commit 32c6d16

Please sign in to comment.