diff --git a/teavm-classlib/src/test/java/org/teavm/platform/metadata/MetadataGeneratorTest.java b/teavm-classlib/src/test/java/org/teavm/platform/metadata/MetadataGeneratorTest.java index 280ee32f5..49de83443 100644 --- a/teavm-classlib/src/test/java/org/teavm/platform/metadata/MetadataGeneratorTest.java +++ b/teavm-classlib/src/test/java/org/teavm/platform/metadata/MetadataGeneratorTest.java @@ -32,7 +32,7 @@ public class MetadataGeneratorTest { } @MetadataProvider(TestResourceGenerator.class) - private native TestResource getInt(); + private native int getInt(); @Test public void intExposed() { diff --git a/teavm-core/src/main/java/org/teavm/javascript/Decompiler.java b/teavm-core/src/main/java/org/teavm/javascript/Decompiler.java index 35d7f5fdd..9a0e8078e 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/Decompiler.java +++ b/teavm-core/src/main/java/org/teavm/javascript/Decompiler.java @@ -132,7 +132,7 @@ public class Decompiler { continue; } if (method.getAnnotations().get(InjectedBy.class.getName()) != null || - methodsToPass.contains(method)) { + methodsToPass.contains(method.getReference())) { continue; } MethodNode methodNode = decompile(method); diff --git a/teavm-platform/src/main/java/org/teavm/platform/plugin/BuildTimeResourceProxyBuilder.java b/teavm-platform/src/main/java/org/teavm/platform/plugin/BuildTimeResourceProxyBuilder.java index 0baf44b47..f288a4cfd 100644 --- a/teavm-platform/src/main/java/org/teavm/platform/plugin/BuildTimeResourceProxyBuilder.java +++ b/teavm-platform/src/main/java/org/teavm/platform/plugin/BuildTimeResourceProxyBuilder.java @@ -17,6 +17,7 @@ package org.teavm.platform.plugin; import java.lang.reflect.Method; import java.util.*; +import org.teavm.codegen.SourceWriter; import org.teavm.platform.metadata.Resource; import org.teavm.platform.metadata.ResourceArray; import org.teavm.platform.metadata.ResourceMap; @@ -73,11 +74,22 @@ class BuildTimeResourceProxyBuilder { " that is not an interface"); } scanIface(rootIface); + Method writeMethod; + try { + writeMethod = ResourceWriter.class.getMethod("write", SourceWriter.class); + } catch (NoSuchMethodException e) { + throw new AssertionError("Method must exist", e); + } + String[] properties = new String[propertyIndexes.size()]; + for (Map.Entry entry : propertyIndexes.entrySet()) { + properties[entry.getValue()] = entry.getKey(); + } + methods.put(writeMethod, new BuildTimeResourceWriterMethod(properties)); return new BuildTimeResourceProxyFactory(methods, initialData); } private void scanIface(Class iface) { - if (iface.isAnnotationPresent(Resource.class)) { + if (!iface.isAnnotationPresent(Resource.class)) { throw new IllegalArgumentException("Error creating a new resource of type " + iface.getName() + ". This type is not marked with the " + Resource.class.getName() + " annotation"); } @@ -170,7 +182,7 @@ class BuildTimeResourceProxyBuilder { } private void scanSetter(Method method) { - String propertyName = extractPropertyName(method.getName().substring(2)); + String propertyName = extractPropertyName(method.getName().substring(3)); if (propertyName == null || !method.getReturnType().equals(void.class) || method.getParameterTypes().length != 1) { throwInvalidMethod(method); diff --git a/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceAccessorGenerator.java b/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceAccessorGenerator.java index 5e8d7542d..746e3e9b7 100644 --- a/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceAccessorGenerator.java +++ b/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceAccessorGenerator.java @@ -33,43 +33,43 @@ class ResourceAccessorGenerator implements Injector { switch (methodRef.getName()) { case "get": if (methodRef.getDescriptor().parameterType(1) == ValueType.INTEGER) { - context.writeExpr(context.getArgument(1)); + context.writeExpr(context.getArgument(0)); context.getWriter().append('['); - context.writeExpr(context.getArgument(2)); + context.writeExpr(context.getArgument(1)); context.getWriter().append(']'); } else { - context.writeExpr(context.getArgument(1)); - writePropertyAccessor(context, context.getArgument(2)); + context.writeExpr(context.getArgument(0)); + writePropertyAccessor(context, context.getArgument(1)); } break; case "put": context.getWriter().append('('); if (methodRef.getDescriptor().parameterType(1) == ValueType.INTEGER) { - context.writeExpr(context.getArgument(1)); + context.writeExpr(context.getArgument(0)); context.getWriter().append('['); - context.writeExpr(context.getArgument(2)); - } else { context.writeExpr(context.getArgument(1)); - writePropertyAccessor(context, context.getArgument(2)); + } else { + context.writeExpr(context.getArgument(0)); + writePropertyAccessor(context, context.getArgument(1)); } context.getWriter().append(']').ws().append('=').ws(); - context.writeExpr(context.getArgument(3)); - context.getWriter().append(')'); - break; - case "add": - context.writeExpr(context.getArgument(1)); - context.getWriter().append(".push("); context.writeExpr(context.getArgument(2)); context.getWriter().append(')'); break; - case "has": + case "add": + context.writeExpr(context.getArgument(0)); + context.getWriter().append(".push("); context.writeExpr(context.getArgument(1)); + context.getWriter().append(')'); + break; + case "has": + context.writeExpr(context.getArgument(0)); context.getWriter().append(".hasOwnProperty("); - writeStringExpr(context, context.getArgument(2)); + writeStringExpr(context, context.getArgument(1)); context.getWriter().append(')'); break; case "size": - context.writeExpr(context.getArgument(1)); + context.writeExpr(context.getArgument(0)); context.getWriter().append(".length"); break; case "castToInt": @@ -84,7 +84,7 @@ class ResourceAccessorGenerator implements Injector { case "castFromBoolean": case "castFromFloat": case "castFromDouble": - context.writeExpr(context.getArgument(1)); + context.writeExpr(context.getArgument(0)); break; case "castToIntWrapper": castToWrapper(context, Integer.class, int.class); @@ -124,29 +124,37 @@ class ResourceAccessorGenerator implements Injector { break; case "castToString": context.getWriter().append("$rt_str("); - context.writeExpr(context.getArgument(1)); + context.writeExpr(context.getArgument(0)); context.getWriter().append(")"); break; case "castFromString": context.getWriter().append("$rt_ustr("); - context.writeExpr(context.getArgument(1)); + context.writeExpr(context.getArgument(0)); context.getWriter().append(")"); break; } } private void castToWrapper(InjectorContext context, Class wrapper, Class primitive) throws IOException { + context.getWriter().append('('); + context.writeExpr(context.getArgument(0)); + context.getWriter().ws().append("==").ws().append("null").ws().append('?').ws().append("null") + .ws().append(':').ws(); context.getWriter().appendMethodBody(new MethodReference(wrapper, "valueOf", primitive, wrapper)).append('('); - context.writeExpr(context.getArgument(1)); - context.getWriter().append(')'); + context.writeExpr(context.getArgument(0)); + context.getWriter().append("))"); } private void castFromWrapper(InjectorContext context, Class wrapper, Class primitive) throws IOException { + context.getWriter().append('('); + context.writeExpr(context.getArgument(0)); + context.getWriter().ws().append("==").ws().append("null").ws().append('?').ws().append("null") + .ws().append(':').ws(); String primitiveName = primitive.getName(); context.getWriter().appendMethodBody(new MethodReference(wrapper, primitiveName + "Value", primitive)) .append('('); - context.writeExpr(context.getArgument(1)); - context.getWriter().append(')'); + context.writeExpr(context.getArgument(0)); + context.getWriter().append("))"); } private void writePropertyAccessor(InjectorContext context, Expr property) throws IOException { diff --git a/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceTransformer.java b/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceTransformer.java index cf7f0b4af..9e33c292f 100644 --- a/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceTransformer.java +++ b/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceTransformer.java @@ -224,7 +224,7 @@ class ResourceTransformer implements ClassHolderTransformer { private String getPropertyName(String name) { if (name.length() == 1) { - return name; + return name.toLowerCase(); } if (Character.isUpperCase(name.charAt(1))) { return name; diff --git a/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceWriter.java b/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceWriter.java index 5f1be5689..b457a5d70 100644 --- a/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceWriter.java +++ b/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceWriter.java @@ -22,6 +22,6 @@ import org.teavm.codegen.SourceWriter; * * @author Alexey Andreev */ -interface ResourceWriter { +public interface ResourceWriter { void write(SourceWriter writer) throws IOException; }