diff --git a/teavm-classlib/src/test/java/org/teavm/platform/metadata/DependentTestResource.java b/teavm-classlib/src/test/java/org/teavm/platform/metadata/DependentTestResource.java index 3415d46a1..5b6fbd963 100644 --- a/teavm-classlib/src/test/java/org/teavm/platform/metadata/DependentTestResource.java +++ b/teavm-classlib/src/test/java/org/teavm/platform/metadata/DependentTestResource.java @@ -19,8 +19,7 @@ package org.teavm.platform.metadata; * * @author Alexey Andreev */ -@Resource -public interface DependentTestResource { +public interface DependentTestResource extends Resource { String getBar(); void setBar(String bar); 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 1c56755aa..ad4bf808a 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,11 +32,11 @@ public class MetadataGeneratorTest { } @MetadataProvider(TestResourceGenerator.class) - private native int getInt(); + private native IntResource getInt(); @Test public void intExposed() { - assertEquals(23, getInt()); + assertEquals(23, getInt().getValue()); } @MetadataProvider(TestResourceGenerator.class) @@ -52,20 +52,13 @@ public class MetadataGeneratorTest { assertEquals(3.14, res.getF(), 0.001); assertEquals(2.72, res.getG(), 0.001); - assertEquals(Integer.valueOf(26), res.getH()); - assertNull(res.getI()); - assertEquals(Byte.valueOf((byte)27), res.getJ()); - assertEquals(Short.valueOf((short)28), res.getK()); - assertEquals(100, res.getL().floatValue(), 0.1); - assertEquals(200, res.getM().doubleValue(), 0.1); - assertEquals("qwe", res.getFoo()); assertEquals(2, res.getArrayA().size()); - assertEquals(Integer.valueOf(2), res.getArrayA().get(0)); - assertEquals(Integer.valueOf(3), res.getArrayA().get(1)); + assertEquals(2, res.getArrayA().get(0).getValue()); + assertEquals(3, res.getArrayA().get(1).getValue()); assertEquals(1, res.getArrayB().size()); - assertEquals("baz", res.getArrayB().get(0)); + assertEquals("baz", res.getArrayB().get(0).getBar()); assertNull(res.getArrayC()); } } diff --git a/teavm-classlib/src/test/java/org/teavm/platform/metadata/TestResource.java b/teavm-classlib/src/test/java/org/teavm/platform/metadata/TestResource.java index d64800bbd..60c4ab2b4 100644 --- a/teavm-classlib/src/test/java/org/teavm/platform/metadata/TestResource.java +++ b/teavm-classlib/src/test/java/org/teavm/platform/metadata/TestResource.java @@ -19,8 +19,7 @@ package org.teavm.platform.metadata; * * @author Alexey Andreev */ -@Resource -public interface TestResource { +public interface TestResource extends Resource { int getA(); void setA(int a); @@ -45,55 +44,31 @@ public interface TestResource { void setG(double g); - Integer getH(); - - void setH(Integer a); - - Boolean getI(); - - void setI(Boolean i); - - Byte getJ(); - - void setJ(Byte d); - - Short getK(); - - void setK(Short k); - - Float getL(); - - void setL(Float l); - - Double getM(); - - void setM(Double g); - String getFoo(); void setFoo(String foo); - ResourceArray getArrayA(); + ResourceArray getArrayA(); - void setArrayA(ResourceArray arrayA); + void setArrayA(ResourceArray arrayA); ResourceArray getArrayB(); void setArrayB(ResourceArray arrayB); - ResourceArray> getArrayC(); + ResourceArray> getArrayC(); - void setArrayC(ResourceArray> arrayC); + void setArrayC(ResourceArray> arrayC); - ResourceMap getMapA(); + ResourceMap getMapA(); - void setMapA(ResourceMap mapA); + void setMapA(ResourceMap mapA); ResourceMap getMapB(); void setMapB(ResourceMap mapB); - ResourceMap> getMapC(); + ResourceMap> getMapC(); - void setMapC(ResourceMap> mapC); + void setMapC(ResourceMap> mapC); } diff --git a/teavm-classlib/src/test/java/org/teavm/platform/metadata/TestResourceGenerator.java b/teavm-classlib/src/test/java/org/teavm/platform/metadata/TestResourceGenerator.java index 2dbf1e392..6edf353ce 100644 --- a/teavm-classlib/src/test/java/org/teavm/platform/metadata/TestResourceGenerator.java +++ b/teavm-classlib/src/test/java/org/teavm/platform/metadata/TestResourceGenerator.java @@ -23,12 +23,12 @@ import org.teavm.model.MethodReference; */ public class TestResourceGenerator implements MetadataGenerator { @Override - public Object generateMetadata(MetadataGeneratorContext context, MethodReference method) { + public Resource generateMetadata(MetadataGeneratorContext context, MethodReference method) { switch (method.getName()) { case "getNull": return null; case "getInt": - return 23; + return createInt(context, 23); case "getResource": return getResource(context); default: @@ -36,7 +36,7 @@ public class TestResourceGenerator implements MetadataGenerator { } } - private Object getResource(MetadataGeneratorContext context) { + private Resource getResource(MetadataGeneratorContext context) { TestResource resource = context.createResource(TestResource.class); resource.setA(23); resource.setB(false); @@ -44,17 +44,11 @@ public class TestResourceGenerator implements MetadataGenerator { resource.setE((short)25); resource.setF(3.14f); resource.setG(2.72); - resource.setH(26); - resource.setI(null); - resource.setJ((byte)27); - resource.setK((short)28); - resource.setL(100f); - resource.setM(200.0); resource.setFoo("qwe"); - ResourceArray array = context.createResourceArray(); - array.add(2); - array.add(3); + ResourceArray array = context.createResourceArray(); + array.add(createInt(context, 2)); + array.add(createInt(context, 3)); resource.setArrayA(array); DependentTestResource dep = context.createResource(DependentTestResource.class); dep.setBar("baz"); @@ -63,4 +57,10 @@ public class TestResourceGenerator implements MetadataGenerator { resource.setArrayB(resArray); return resource; } + + private IntResource createInt(MetadataGeneratorContext context, int value) { + IntResource res = context.createResource(IntResource.class); + res.setValue(value); + return res; + } } diff --git a/teavm-platform/src/main/java/org/teavm/platform/metadata/BooleanResource.java b/teavm-platform/src/main/java/org/teavm/platform/metadata/BooleanResource.java new file mode 100644 index 000000000..ebf4bf6f7 --- /dev/null +++ b/teavm-platform/src/main/java/org/teavm/platform/metadata/BooleanResource.java @@ -0,0 +1,26 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.platform.metadata; + +/** + * + * @author Alexey Andreev + */ +public interface BooleanResource extends Resource { + boolean getValue(); + + void setValue(boolean value); +} diff --git a/teavm-platform/src/main/java/org/teavm/platform/metadata/ByteResource.java b/teavm-platform/src/main/java/org/teavm/platform/metadata/ByteResource.java new file mode 100644 index 000000000..582cda32b --- /dev/null +++ b/teavm-platform/src/main/java/org/teavm/platform/metadata/ByteResource.java @@ -0,0 +1,26 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.platform.metadata; + +/** + * + * @author Alexey Andreev + */ +public interface ByteResource extends Resource { + byte getValue(); + + void setValue(byte value); +} diff --git a/teavm-platform/src/main/java/org/teavm/platform/metadata/DoubleResource.java b/teavm-platform/src/main/java/org/teavm/platform/metadata/DoubleResource.java new file mode 100644 index 000000000..2d728fdf5 --- /dev/null +++ b/teavm-platform/src/main/java/org/teavm/platform/metadata/DoubleResource.java @@ -0,0 +1,26 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.platform.metadata; + +/** + * + * @author Alexey Andreev + */ +public interface DoubleResource extends Resource { + double getValue(); + + void setValue(double value); +} diff --git a/teavm-platform/src/main/java/org/teavm/platform/metadata/FloatResource.java b/teavm-platform/src/main/java/org/teavm/platform/metadata/FloatResource.java new file mode 100644 index 000000000..4c159052c --- /dev/null +++ b/teavm-platform/src/main/java/org/teavm/platform/metadata/FloatResource.java @@ -0,0 +1,26 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.platform.metadata; + +/** + * + * @author Alexey Andreev + */ +public interface FloatResource extends Resource { + float getValue(); + + void setValue(float value); +} diff --git a/teavm-platform/src/main/java/org/teavm/platform/metadata/IntResource.java b/teavm-platform/src/main/java/org/teavm/platform/metadata/IntResource.java new file mode 100644 index 000000000..574b84192 --- /dev/null +++ b/teavm-platform/src/main/java/org/teavm/platform/metadata/IntResource.java @@ -0,0 +1,26 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.platform.metadata; + +/** + * + * @author Alexey Andreev + */ +public interface IntResource extends Resource { + int getValue(); + + void setValue(int value); +} diff --git a/teavm-platform/src/main/java/org/teavm/platform/metadata/MetadataGenerator.java b/teavm-platform/src/main/java/org/teavm/platform/metadata/MetadataGenerator.java index 8a5a0e918..92c0ce73a 100644 --- a/teavm-platform/src/main/java/org/teavm/platform/metadata/MetadataGenerator.java +++ b/teavm-platform/src/main/java/org/teavm/platform/metadata/MetadataGenerator.java @@ -41,12 +41,10 @@ import org.teavm.model.MethodReference; *

The valid resource types are the following:

* *
    - *
  • Valid interfaces, marked with the {@link Resource} annotation. Read the description of this annotation + *
  • Valid interfaces, extending the {@link Resource} annotation. Read the description of this interface * for detailed description about valid resources interfaces.
  • *
  • {@link ResourceArray} of valid resources.
  • *
  • {@link ResourceMap} of valid resources.
  • - *
  • {@link String}, {@link Integer}, {@link Byte}, {@link Short}, {@link Float}, {@link Double}, - * {@link Boolean}.
  • *
  • The null value.
  • *
* @@ -61,5 +59,5 @@ public interface MetadataGenerator { * @param context context that contains useful compile-time information. * @param method method which will be used to access the generated resources at run time. */ - Object generateMetadata(MetadataGeneratorContext context, MethodReference method); + Resource generateMetadata(MetadataGeneratorContext context, MethodReference method); } diff --git a/teavm-platform/src/main/java/org/teavm/platform/metadata/MetadataGeneratorContext.java b/teavm-platform/src/main/java/org/teavm/platform/metadata/MetadataGeneratorContext.java index 7003d9344..88f1bca57 100644 --- a/teavm-platform/src/main/java/org/teavm/platform/metadata/MetadataGeneratorContext.java +++ b/teavm-platform/src/main/java/org/teavm/platform/metadata/MetadataGeneratorContext.java @@ -45,15 +45,15 @@ public interface MetadataGeneratorContext { * Creates a new resource of the given type. The description of valid resources * is available in documentation for {@link Resource}. */ - T createResource(Class resourceType); + T createResource(Class resourceType); /** * Creates a new resource array. */ - ResourceArray createResourceArray(); + ResourceArray createResourceArray(); /** * Creates a new resource map. */ - ResourceMap createResourceMap(); + ResourceMap createResourceMap(); } diff --git a/teavm-platform/src/main/java/org/teavm/platform/metadata/Resource.java b/teavm-platform/src/main/java/org/teavm/platform/metadata/Resource.java index 3524b52d9..b58ee600f 100644 --- a/teavm-platform/src/main/java/org/teavm/platform/metadata/Resource.java +++ b/teavm-platform/src/main/java/org/teavm/platform/metadata/Resource.java @@ -15,8 +15,6 @@ */ package org.teavm.platform.metadata; -import java.lang.annotation.*; - /** *

Marks a valid resource interface. Resource interface is an interface, that has get* and set* methods, * according the default convention for JavaBeans. Each property must have both getter and setter. @@ -28,9 +26,5 @@ import java.lang.annotation.*; * * @author Alexey Andreev */ -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.TYPE) -@Inherited -@Documented -public @interface Resource { +public interface Resource { } diff --git a/teavm-platform/src/main/java/org/teavm/platform/metadata/ResourceArray.java b/teavm-platform/src/main/java/org/teavm/platform/metadata/ResourceArray.java index 98a85ca2a..059c4ea0d 100644 --- a/teavm-platform/src/main/java/org/teavm/platform/metadata/ResourceArray.java +++ b/teavm-platform/src/main/java/org/teavm/platform/metadata/ResourceArray.java @@ -19,7 +19,7 @@ package org.teavm.platform.metadata; * * @author Alexey Andreev */ -public interface ResourceArray { +public interface ResourceArray extends Resource { int size(); T get(int i); diff --git a/teavm-platform/src/main/java/org/teavm/platform/metadata/ResourceMap.java b/teavm-platform/src/main/java/org/teavm/platform/metadata/ResourceMap.java index e798f94cc..3ce3f8bfe 100644 --- a/teavm-platform/src/main/java/org/teavm/platform/metadata/ResourceMap.java +++ b/teavm-platform/src/main/java/org/teavm/platform/metadata/ResourceMap.java @@ -19,7 +19,7 @@ package org.teavm.platform.metadata; * * @author Alexey Andreev */ -public interface ResourceMap { +public interface ResourceMap extends Resource { boolean has(String key); T get(String key); diff --git a/teavm-platform/src/main/java/org/teavm/platform/metadata/ShortResource.java b/teavm-platform/src/main/java/org/teavm/platform/metadata/ShortResource.java new file mode 100644 index 000000000..c0a31ba68 --- /dev/null +++ b/teavm-platform/src/main/java/org/teavm/platform/metadata/ShortResource.java @@ -0,0 +1,26 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.platform.metadata; + +/** + * + * @author Alexey Andreev + */ +public interface ShortResource extends Resource { + short getValue(); + + void setValue(short value); +} diff --git a/teavm-platform/src/main/java/org/teavm/platform/metadata/StringResource.java b/teavm-platform/src/main/java/org/teavm/platform/metadata/StringResource.java new file mode 100644 index 000000000..a48754b30 --- /dev/null +++ b/teavm-platform/src/main/java/org/teavm/platform/metadata/StringResource.java @@ -0,0 +1,26 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.platform.metadata; + +/** + * + * @author Alexey Andreev + */ +public interface StringResource extends Resource { + String getValue(); + + void setValue(String value); +} diff --git a/teavm-platform/src/main/java/org/teavm/platform/plugin/BuildTimeResourceArray.java b/teavm-platform/src/main/java/org/teavm/platform/plugin/BuildTimeResourceArray.java index 43d1ae312..c2e9a9267 100644 --- a/teavm-platform/src/main/java/org/teavm/platform/plugin/BuildTimeResourceArray.java +++ b/teavm-platform/src/main/java/org/teavm/platform/plugin/BuildTimeResourceArray.java @@ -19,13 +19,14 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.teavm.codegen.SourceWriter; +import org.teavm.platform.metadata.Resource; import org.teavm.platform.metadata.ResourceArray; /** * * @author Alexey Andreev */ -class BuildTimeResourceArray implements ResourceArray, ResourceWriter { +class BuildTimeResourceArray implements ResourceArray, ResourceWriter { private List data = new ArrayList<>(); @Override diff --git a/teavm-platform/src/main/java/org/teavm/platform/plugin/BuildTimeResourceMap.java b/teavm-platform/src/main/java/org/teavm/platform/plugin/BuildTimeResourceMap.java index 213b2c479..2e1dba77c 100644 --- a/teavm-platform/src/main/java/org/teavm/platform/plugin/BuildTimeResourceMap.java +++ b/teavm-platform/src/main/java/org/teavm/platform/plugin/BuildTimeResourceMap.java @@ -19,13 +19,14 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.teavm.codegen.SourceWriter; +import org.teavm.platform.metadata.Resource; import org.teavm.platform.metadata.ResourceMap; /** * * @author Alexey Andreev */ -class BuildTimeResourceMap implements ResourceMap, ResourceWriter { +class BuildTimeResourceMap implements ResourceMap, ResourceWriter { private Map data = new HashMap<>(); @Override 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 f288a4cfd..9e962512a 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 @@ -29,8 +29,7 @@ import org.teavm.platform.metadata.ResourceMap; class BuildTimeResourceProxyBuilder { private Map, BuildTimeResourceProxyFactory> factories = new HashMap<>(); private static Set> allowedPropertyTypes = new HashSet<>(Arrays.>asList( - boolean.class, Boolean.class, byte.class, Byte.class, short.class, Short.class, - int.class, Integer.class, float.class, Float.class, double.class, Double.class, + boolean.class, byte.class, short.class, int.class, float.class, double.class, String.class, ResourceArray.class, ResourceMap.class)); private static Map, Object> defaultValues = new HashMap<>(); @@ -89,9 +88,9 @@ class BuildTimeResourceProxyBuilder { } private void scanIface(Class iface) { - if (!iface.isAnnotationPresent(Resource.class)) { + if (!Resource.class.isAssignableFrom(iface)) { throw new IllegalArgumentException("Error creating a new resource of type " + iface.getName() + - ". This type is not marked with the " + Resource.class.getName() + " annotation"); + ". This type does not implement the " + Resource.class.getName() + " interface"); } // Scan methods @@ -136,7 +135,7 @@ class BuildTimeResourceProxyBuilder { String propertyName = property.getKey(); Class propertyType = property.getValue(); if (!allowedPropertyTypes.contains(propertyType)) { - if (!propertyType.isInterface() || !propertyType.isAnnotationPresent(Resource.class)) { + if (!propertyType.isInterface() || !Resource.class.isAssignableFrom(propertyType)) { throw new IllegalArgumentException("Property " + iface.getName() + "." + propertyName + " has an illegal type " + propertyType.getName()); } diff --git a/teavm-platform/src/main/java/org/teavm/platform/plugin/DefaultMetadataGeneratorContext.java b/teavm-platform/src/main/java/org/teavm/platform/plugin/DefaultMetadataGeneratorContext.java index cd38cc840..c68f69911 100644 --- a/teavm-platform/src/main/java/org/teavm/platform/plugin/DefaultMetadataGeneratorContext.java +++ b/teavm-platform/src/main/java/org/teavm/platform/plugin/DefaultMetadataGeneratorContext.java @@ -19,6 +19,7 @@ import java.lang.reflect.Proxy; import java.util.Properties; import org.teavm.model.ListableClassReaderSource; import org.teavm.platform.metadata.MetadataGeneratorContext; +import org.teavm.platform.metadata.Resource; import org.teavm.platform.metadata.ResourceArray; import org.teavm.platform.metadata.ResourceMap; @@ -55,19 +56,19 @@ class DefaultMetadataGeneratorContext implements MetadataGeneratorContext { } @Override - public T createResource(Class resourceType) { + public T createResource(Class resourceType) { return resourceType.cast(Proxy.newProxyInstance(classLoader, new Class[] { resourceType, ResourceWriter.class }, proxyBuilder.buildProxy(resourceType))); } @Override - public ResourceArray createResourceArray() { + public ResourceArray createResourceArray() { return new BuildTimeResourceArray<>(); } @Override - public ResourceMap createResourceMap() { + public ResourceMap createResourceMap() { return new BuildTimeResourceMap<>(); } } diff --git a/teavm-platform/src/main/java/org/teavm/platform/plugin/MetadataProviderNativeGenerator.java b/teavm-platform/src/main/java/org/teavm/platform/plugin/MetadataProviderNativeGenerator.java index 62497207d..d9801bece 100644 --- a/teavm-platform/src/main/java/org/teavm/platform/plugin/MetadataProviderNativeGenerator.java +++ b/teavm-platform/src/main/java/org/teavm/platform/plugin/MetadataProviderNativeGenerator.java @@ -24,6 +24,7 @@ import org.teavm.javascript.ni.GeneratorContext; import org.teavm.model.*; import org.teavm.platform.metadata.MetadataGenerator; import org.teavm.platform.metadata.MetadataProvider; +import org.teavm.platform.metadata.Resource; /** * @@ -73,7 +74,7 @@ public class MetadataProviderNativeGenerator implements Generator { context.getClassLoader(), context.getProperties()); // Generate resource loader - Object resource = generator.generateMetadata(metadataContext, methodRef); + Resource resource = generator.generateMetadata(metadataContext, methodRef); writer.append("if (!window.hasOwnProperty(\"").appendMethodBody(methodRef).append("$$resource\")) {") .indent().softNewLine(); writer.appendMethodBody(methodRef).append("$$resource = "); diff --git a/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceAccessor.java b/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceAccessor.java index 7c399174b..5002ec399 100644 --- a/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceAccessor.java +++ b/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceAccessor.java @@ -15,6 +15,8 @@ */ package org.teavm.platform.plugin; +import org.teavm.platform.metadata.Resource; + /** * * @author Alexey Andreev @@ -24,9 +26,9 @@ final class ResourceAccessor { public static native void put(Object obj, String propertyName, Object elem); - public static native Object get(Object obj, int index); + public static native Resource get(Object obj, int index); - public static native void add(Object obj, Object elem); + public static native void add(Object obj, Resource elem); public static native boolean has(Object obj, String key); @@ -34,53 +36,29 @@ final class ResourceAccessor { public static native int castToInt(Object obj); - public static native Integer castToIntWrapper(Object obj); - public static native short castToShort(Object obj); - public static native Short castToShortWrapper(Object obj); - public static native byte castToByte(Object obj); - public static native Byte castToByteWrapper(Object obj); - public static native boolean castToBoolean(Object obj); - public static native Boolean castToBooleanWrapper(Object obj); - public static native float castToFloat(Object obj); - public static native Float castToFloatWrapper(Object obj); - public static native double castToDouble(Object obj); - public static native Double castToDoubleWrapper(Object obj); - public static native String castToString(Object obj); public static native Object castFromInt(int value); - public static native Object castFromIntWrapper(Integer value); - public static native Object castFromShort(short value); - public static native Object castFromShortWrapper(Short value); - public static native Object castFromByte(byte value); - public static native Object castFromByteWrapper(Byte value); - public static native Object castFromBoolean(boolean value); - public static native Object castFromBooleanWrapper(Boolean value); - public static native Object castFromFloat(float value); - public static native Object castFromFloatWrapper(Float value); - public static native Object castFromDouble(double value); - public static native Object castFromDoubleWrapper(Double value); - public static native Object castFromString(String value); } diff --git a/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceAccessorDependencyListener.java b/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceAccessorDependencyListener.java index 98345dbd1..72c572674 100644 --- a/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceAccessorDependencyListener.java +++ b/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceAccessorDependencyListener.java @@ -19,7 +19,6 @@ import org.teavm.dependency.DependencyAgent; import org.teavm.dependency.DependencyListener; import org.teavm.dependency.FieldDependency; import org.teavm.dependency.MethodDependency; -import org.teavm.model.MethodReference; /** * @@ -37,58 +36,12 @@ class ResourceAccessorDependencyListener implements DependencyListener { @Override public void methodAchieved(DependencyAgent agent, MethodDependency method) { switch (method.getReference().getName()) { - case "castToIntWrapper": - castToWrapper(agent, method, Integer.class, int.class); - break; - case "castToShortWrapper": - castToWrapper(agent, method, Short.class, short.class); - break; - case "castToByteWrapper": - castToWrapper(agent, method, Byte.class, byte.class); - break; - case "castToBooleanWrapper": - castToWrapper(agent, method, Boolean.class, boolean.class); - break; - case "castToFloatWrapper": - castToWrapper(agent, method, Float.class, float.class); - break; - case "castToDoubleWrapper": - castToWrapper(agent, method, Double.class, double.class); - break; - case "castFromIntegerWrapper": - castFromWrapper(agent, method, Integer.class, int.class); - break; - case "castFromShortWrapper": - castFromWrapper(agent, method, Short.class, short.class); - break; - case "castFromByteWrapper": - castFromWrapper(agent, method, Byte.class, byte.class); - break; - case "castFromBooleanWrapper": - castFromWrapper(agent, method, Boolean.class, boolean.class); - break; - case "castFromFloatWrapper": - castFromWrapper(agent, method, Float.class, float.class); - break; - case "castFromDoubleWrapper": - castFromWrapper(agent, method, Double.class, double.class); - break; case "castToString": method.getResult().propagate("java.lang.String"); break; } } - private void castToWrapper(DependencyAgent agent, MethodDependency method, Class wrapper, Class primitive) { - method.getResult().propagate(wrapper.getName()); - agent.linkMethod(new MethodReference(wrapper, "valueOf", primitive, wrapper), method.getStack()).use(); - } - - private void castFromWrapper(DependencyAgent agent, MethodDependency method, Class wrapper, Class primitive) { - String primitiveName = primitive.getName(); - agent.linkMethod(new MethodReference(wrapper, primitiveName + "Value", primitive), method.getStack()).use(); - } - @Override public void fieldAchieved(DependencyAgent agent, FieldDependency field) { } 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 746e3e9b7..73e142c5c 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 @@ -86,42 +86,6 @@ class ResourceAccessorGenerator implements Injector { case "castFromDouble": context.writeExpr(context.getArgument(0)); break; - case "castToIntWrapper": - castToWrapper(context, Integer.class, int.class); - break; - case "castToShortWrapper": - castToWrapper(context, Short.class, short.class); - break; - case "castToByteWrapper": - castToWrapper(context, Byte.class, byte.class); - break; - case "castToBooleanWrapper": - castToWrapper(context, Boolean.class, boolean.class); - break; - case "castToFloatWrapper": - castToWrapper(context, Float.class, float.class); - break; - case "castToDoubleWrapper": - castToWrapper(context, Double.class, double.class); - break; - case "castFromIntWrapper": - castFromWrapper(context, Integer.class, int.class); - break; - case "castFromShortWrapper": - castFromWrapper(context, Short.class, short.class); - break; - case "castFromByteWrapper": - castFromWrapper(context, Byte.class, byte.class); - break; - case "castFromBooleanWrapper": - castFromWrapper(context, Boolean.class, boolean.class); - break; - case "castFromFloatWrapper": - castFromWrapper(context, Float.class, float.class); - break; - case "castFromDoubleWrapper": - castFromWrapper(context, Double.class, double.class); - break; case "castToString": context.getWriter().append("$rt_str("); context.writeExpr(context.getArgument(0)); @@ -135,28 +99,6 @@ class ResourceAccessorGenerator implements Injector { } } - 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(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(0)); - context.getWriter().append("))"); - } - private void writePropertyAccessor(InjectorContext context, Expr property) throws IOException { if (property instanceof ConstantExpr) { String str = (String)((ConstantExpr)property).getValue(); diff --git a/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceProgramTransformer.java b/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceProgramTransformer.java index 4b542a767..014433545 100644 --- a/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceProgramTransformer.java +++ b/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceProgramTransformer.java @@ -14,7 +14,6 @@ import org.teavm.platform.metadata.ResourceMap; class ResourceProgramTransformer { private ClassReaderSource innerSource; private Program program; - private Set arrayItemsVars = new HashSet<>(); public ResourceProgramTransformer(ClassReaderSource innerSource, Program program) { this.innerSource = innerSource; @@ -25,11 +24,6 @@ class ResourceProgramTransformer { for (int i = 0; i < program.basicBlockCount(); ++i) { transformBasicBlock(program.basicBlockAt(i)); } - if (!arrayItemsVars.isEmpty()) { - for (int i = 0; i < program.basicBlockCount(); ++i) { - postProcessBasicBlock(program.basicBlockAt(i)); - } - } } private void transformBasicBlock(BasicBlock block) { @@ -48,56 +42,6 @@ class ResourceProgramTransformer { } } - private void postProcessBasicBlock(BasicBlock block) { - List instructions = block.getInstructions(); - for (int i = 0; i < instructions.size(); ++i) { - Instruction insn = instructions.get(i); - if (!(insn instanceof CastInstruction)) { - continue; - } - CastInstruction cast = (CastInstruction)insn; - if (!arrayItemsVars.contains(cast.getReceiver())) { - continue; - } - if (!(cast.getTargetType() instanceof ValueType.Object)) { - continue; - } - String targetTypeName = ((ValueType.Object)cast.getTargetType()).getClassName(); - Variable var = cast.getValue(); - Variable recv = cast.getReceiver(); - switch (targetTypeName) { - case "java.lang.Integer": - instructions.set(i, castToWrapper(var, recv, int.class, Integer.class)); - break; - case "java.lang.Boolean": - instructions.set(i, castToWrapper(var, recv, boolean.class, Boolean.class)); - break; - case "java.lang.Byte": - instructions.set(i, castToWrapper(var, recv, byte.class, Byte.class)); - break; - case "java.lang.Short": - instructions.set(i, castToWrapper(var, recv, short.class, Short.class)); - break; - case "java.lang.Float": - instructions.set(i, castToWrapper(var, recv, float.class, Float.class)); - break; - case "java.lang.Double": - instructions.set(i, castToWrapper(var, recv, double.class, Double.class)); - break; - case "java.lang.String": { - InvokeInstruction castInvoke = new InvokeInstruction(); - castInvoke.setType(InvocationType.SPECIAL); - castInvoke.setMethod(new MethodReference(ResourceAccessor.class, "castToString", - Object.class, String.class)); - castInvoke.getArguments().add(var); - castInvoke.setReceiver(recv); - instructions.set(i, castInvoke); - break; - } - } - } - } - private List transformInvoke(InvokeInstruction insn) { if (insn.getType() != InvocationType.VIRTUAL) { return null; @@ -105,9 +49,6 @@ class ResourceProgramTransformer { MethodReference method = insn.getMethod(); if (method.getClassName().equals(ResourceArray.class.getName()) || method.getClassName().equals(ResourceMap.class.getName())) { - if (method.getName().equals("get")) { - arrayItemsVars.add(insn.getReceiver()); - } InvokeInstruction accessInsn = new InvokeInstruction(); accessInsn.setType(InvocationType.SPECIAL); ValueType[] types = new ValueType[method.getDescriptor().parameterCount() + 2]; @@ -172,24 +113,6 @@ class ResourceProgramTransformer { } } else if (type instanceof ValueType.Object) { switch (((ValueType.Object)type).getClassName()) { - case "java.lang.Boolean": - getAndCastPropertyToWrapper(insn, property, instructions, boolean.class, Boolean.class); - return instructions; - case "java.lang.Byte": - getAndCastPropertyToWrapper(insn, property, instructions, byte.class, Byte.class); - return instructions; - case "java.lang.Short": - getAndCastPropertyToWrapper(insn, property, instructions, short.class, Short.class); - return instructions; - case "java.lang.Integer": - getAndCastPropertyToWrapper(insn, property, instructions, int.class, Integer.class); - return instructions; - case "java.lang.Float": - getAndCastPropertyToWrapper(insn, property, instructions, float.class, Float.class); - return instructions; - case "java.lang.Double": - getAndCastPropertyToWrapper(insn, property, instructions, double.class, Double.class); - return instructions; case "java.lang.String": { Variable resultVar = insn.getProgram().createVariable(); getProperty(insn, property, instructions, resultVar); @@ -250,35 +173,6 @@ class ResourceProgramTransformer { instructions.add(castInvoke); } - private Instruction castToWrapper(Variable var, Variable receiver, Class primitive, Class wrapper) { - InvokeInstruction castInvoke = new InvokeInstruction(); - castInvoke.setType(InvocationType.SPECIAL); - String primitiveCapitalized = primitive.getName(); - primitiveCapitalized = Character.toUpperCase(primitiveCapitalized.charAt(0)) + - primitiveCapitalized.substring(1); - castInvoke.setMethod(new MethodReference(ResourceAccessor.class, "castTo" + primitiveCapitalized + "Wrapper", - Object.class, wrapper)); - castInvoke.getArguments().add(var); - castInvoke.setReceiver(receiver); - return castInvoke; - } - - private void getAndCastPropertyToWrapper(InvokeInstruction insn, String property, List instructions, - Class primitive, Class wrapper) { - Variable resultVar = program.createVariable(); - getProperty(insn, property, instructions, resultVar); - InvokeInstruction castInvoke = new InvokeInstruction(); - castInvoke.setType(InvocationType.SPECIAL); - String primitiveCapitalized = primitive.getName(); - primitiveCapitalized = Character.toUpperCase(primitiveCapitalized.charAt(0)) + - primitiveCapitalized.substring(1); - castInvoke.setMethod(new MethodReference(ResourceAccessor.class, "castTo" + primitiveCapitalized + "Wrapper", - Object.class, wrapper)); - castInvoke.getArguments().add(resultVar); - castInvoke.setReceiver(insn.getReceiver()); - instructions.add(castInvoke); - } - private List transformSetterInvocation(InvokeInstruction insn, String property) { return null; }