From 50a8c85cd178e70359803208cfb9d4432ea19bdc Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Sun, 10 Nov 2013 19:56:17 +0400 Subject: [PATCH] java.lang.Object constructor generated properly --- .../java/lang/ObjectNativeGenerator.java | 2 +- .../org/teavm/classlib/java/lang/TObject.java | 5 ++++- .../org/teavm/javascript/StatementGenerator.java | 2 +- .../java/org/teavm/javascript/ni/Rename.java | 2 +- .../java/org/teavm/javascript/ni/Superclass.java | 16 ++++++++++++++++ .../teavm/model/resource/ClassRefsRenamer.java | 14 +++++++++++--- 6 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 teavm-core/src/main/java/org/teavm/javascript/ni/Superclass.java diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/ObjectNativeGenerator.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/ObjectNativeGenerator.java index 1a64dc0d9..026202330 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/ObjectNativeGenerator.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/ObjectNativeGenerator.java @@ -15,7 +15,7 @@ public class ObjectNativeGenerator implements Generator { @Override public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) { switch (methodRef.getDescriptor().getName()) { - case "init": + case "": generateInit(context, writer); break; case "getClass": diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java index 17a7c068b..dfd3affd6 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java @@ -2,17 +2,20 @@ package org.teavm.classlib.java.lang; import org.teavm.javascript.ni.GeneratedBy; import org.teavm.javascript.ni.Rename; +import org.teavm.javascript.ni.Superclass; /** * * @author Alexey Andreev */ +@Superclass("") public class TObject { + @Rename("fakeInit") public TObject() { - init(); } @GeneratedBy(ObjectNativeGenerator.class) + @Rename("") private native void init(); @GeneratedBy(ObjectNativeGenerator.class) diff --git a/teavm-core/src/main/java/org/teavm/javascript/StatementGenerator.java b/teavm-core/src/main/java/org/teavm/javascript/StatementGenerator.java index ddc02fa91..f59d23998 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/StatementGenerator.java +++ b/teavm-core/src/main/java/org/teavm/javascript/StatementGenerator.java @@ -489,7 +489,7 @@ public class StatementGenerator implements InstructionVisitor { public String findDeclaringClass(String className, MethodDescriptor method) { ClassHolder cls = classSource.getClassHolder(className); while (cls != null && cls.getMethod(method) == null) { - cls = classSource.getClassHolder(cls.getParent()); + cls = cls.getParent() != null ? classSource.getClassHolder(cls.getParent()) : null; } return cls != null ? cls.getName() : null; } diff --git a/teavm-core/src/main/java/org/teavm/javascript/ni/Rename.java b/teavm-core/src/main/java/org/teavm/javascript/ni/Rename.java index dea50f7ab..0d806ccb2 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/ni/Rename.java +++ b/teavm-core/src/main/java/org/teavm/javascript/ni/Rename.java @@ -10,7 +10,7 @@ import java.lang.annotation.Target; * @author Alexey Andreev */ @Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.METHOD) +@Target({ ElementType.METHOD, ElementType.CONSTRUCTOR }) public @interface Rename { String value(); } diff --git a/teavm-core/src/main/java/org/teavm/javascript/ni/Superclass.java b/teavm-core/src/main/java/org/teavm/javascript/ni/Superclass.java new file mode 100644 index 000000000..de5600d09 --- /dev/null +++ b/teavm-core/src/main/java/org/teavm/javascript/ni/Superclass.java @@ -0,0 +1,16 @@ +package org.teavm.javascript.ni; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + * @author Alexey Andreev + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface Superclass { + String value(); +} diff --git a/teavm-core/src/main/java/org/teavm/model/resource/ClassRefsRenamer.java b/teavm-core/src/main/java/org/teavm/model/resource/ClassRefsRenamer.java index 685ea2afa..cf3f70336 100644 --- a/teavm-core/src/main/java/org/teavm/model/resource/ClassRefsRenamer.java +++ b/teavm-core/src/main/java/org/teavm/model/resource/ClassRefsRenamer.java @@ -3,6 +3,7 @@ package org.teavm.model.resource; import java.util.Map; import org.teavm.codegen.Mapper; import org.teavm.javascript.ni.Rename; +import org.teavm.javascript.ni.Superclass; import org.teavm.model.*; import org.teavm.model.instructions.*; @@ -21,9 +22,15 @@ class ClassRefsRenamer implements InstructionVisitor { ClassHolder renamedCls = new ClassHolder(classNameMapper.map(cls.getName())); renamedCls.getModifiers().addAll(cls.getModifiers()); renamedCls.setLevel(cls.getLevel()); - if (cls.getParent() != null) { - renamedCls.setParent(classNameMapper.map(cls.getParent())); + String parent = cls.getParent(); + AnnotationHolder superclassAnnot = cls.getAnnotations().get(Superclass.class.getName()); + if (superclassAnnot != null) { + parent = superclassAnnot.getValues().get("value").getString(); + if (parent.isEmpty()) { + parent = null; + } } + renamedCls.setParent(parent != null ? classNameMapper.map(parent) : null); for (MethodHolder method : cls.getMethods()) { renamedCls.addMethod(rename(method)); } @@ -71,7 +78,8 @@ class ClassRefsRenamer implements InstructionVisitor { private void rename(AnnotationContainer source, AnnotationContainer target) { for (AnnotationHolder annot : source.all()) { - if (!annot.getType().equals(Rename.class.getName())) { + if (!annot.getType().equals(Rename.class.getName()) && + !annot.getType().equals(Superclass.class.getName())) { target.add(rename(annot)); } }