Permalink
Browse files

Added support for GWT -- including Reflection wrapper

  • Loading branch information...
1 parent 8a572a9 commit 78b0065651b40545f8ff288acf8756ee517b8d5a @Flet Flet committed Feb 13, 2014
Showing with 4,144 additions and 70 deletions.
  1. +58 −0 artemis-gwt/pom.xml
  2. +14 −0 artemis-gwt/src/main/java/com/artemis/backends/artemis_backends_gwt.gwt.xml
  3. +44 −0 ...mis-gwt/src/main/java/com/artemis/backends/gwt/emu/com/artemis/utils/reflect/ArrayReflection.java
  4. +188 −0 ...mis-gwt/src/main/java/com/artemis/backends/gwt/emu/com/artemis/utils/reflect/ClassReflection.java
  5. +60 −0 artemis-gwt/src/main/java/com/artemis/backends/gwt/emu/com/artemis/utils/reflect/Constructor.java
  6. +138 −0 artemis-gwt/src/main/java/com/artemis/backends/gwt/emu/com/artemis/utils/reflect/Field.java
  7. +122 −0 artemis-gwt/src/main/java/com/artemis/backends/gwt/emu/com/artemis/utils/reflect/Method.java
  8. +917 −0 artemis-gwt/src/main/java/com/artemis/backends/gwt/emu/java/util/BitSet.java
  9. +112 −0 artemis-gwt/src/main/java/com/artemis/backends/gwt/emu/java/util/UUID.java
  10. +25 −0 artemis-gwt/src/main/java/com/artemis/gwtref/ArtemisReflect.gwt.xml
  11. +33 −0 artemis-gwt/src/main/java/com/artemis/gwtref/client/Constructor.java
  12. +130 −0 artemis-gwt/src/main/java/com/artemis/gwtref/client/Field.java
  13. +42 −0 artemis-gwt/src/main/java/com/artemis/gwtref/client/IReflectionCache.java
  14. +161 −0 artemis-gwt/src/main/java/com/artemis/gwtref/client/Method.java
  15. +46 −0 artemis-gwt/src/main/java/com/artemis/gwtref/client/Parameter.java
  16. +88 −0 artemis-gwt/src/main/java/com/artemis/gwtref/client/ReflectionCache.java
  17. +99 −0 artemis-gwt/src/main/java/com/artemis/gwtref/client/Test.java
  18. +246 −0 artemis-gwt/src/main/java/com/artemis/gwtref/client/Type.java
  19. +46 −0 artemis-gwt/src/main/java/com/artemis/gwtref/gen/ReflectionCacheGenerator.java
  20. +895 −0 artemis-gwt/src/main/java/com/artemis/gwtref/gen/ReflectionCacheSourceCreator.java
  21. +18 −0 artemis/pom.xml
  22. +6 −0 artemis/src/main/java/com/artemis.gwt.xml
  23. +6 −4 artemis/src/main/java/com/artemis/BasicComponentMapper.java
  24. +5 −7 artemis/src/main/java/com/artemis/ComponentManager.java
  25. +4 −2 artemis/src/main/java/com/artemis/ComponentPool.java
  26. +5 −4 artemis/src/main/java/com/artemis/ComponentType.java
  27. +2 −1 artemis/src/main/java/com/artemis/Entity.java
  28. +3 −1 artemis/src/main/java/com/artemis/InvalidComponentException.java
  29. +7 −6 artemis/src/main/java/com/artemis/PackedComponentMapper.java
  30. +11 −13 artemis/src/main/java/com/artemis/World.java
  31. +6 −3 artemis/src/main/java/com/artemis/utils/FastMath.java
  32. +0 −28 artemis/src/main/java/com/artemis/utils/Utils.java
  33. +43 −0 artemis/src/main/java/com/artemis/utils/reflect/ArrayReflection.java
  34. +187 −0 artemis/src/main/java/com/artemis/utils/reflect/ClassReflection.java
  35. +64 −0 artemis/src/main/java/com/artemis/utils/reflect/Constructor.java
  36. +151 −0 artemis/src/main/java/com/artemis/utils/reflect/Field.java
  37. +118 −0 artemis/src/main/java/com/artemis/utils/reflect/Method.java
  38. +42 −0 artemis/src/main/java/com/artemis/utils/reflect/ReflectionException.java
  39. +1 −1 artemis/src/test/java/com/artemis/ComponentPoolTest.java
  40. +1 −0 pom.xml
View
58 artemis-gwt/pom.xml
@@ -0,0 +1,58 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>net.onedaybeard.artemis</groupId>
+ <artifactId>artemis-parent</artifactId>
+ <version>0.5.1-SNAPSHOT</version>
+ </parent>
+ <artifactId>artemis-gwt</artifactId>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>artemis-odb</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>com.google.gwt</groupId>
+ <artifactId>gwt-user</artifactId>
+ <version>2.5.1</version>
+ </dependency>
+
+ <dependency>
+ <groupId>com.google.gwt</groupId>
+ <artifactId>gwt-dev</artifactId>
+ <version>2.5.1</version>
+ <scope>provided</scope>
+ </dependency>
+
+ </dependencies>
+
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ </plugin>
+
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-source-plugin</artifactId>
+ <executions>
+ <execution>
+ <id>attach-sources</id>
+ <phase>generate-resources</phase>
+ <goals>
+ <goal>jar-no-fork</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+
+</project>
View
14 artemis-gwt/src/main/java/com/artemis/backends/artemis_backends_gwt.gwt.xml
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.5.1//EN" "http://google-web-toolkit.googlecode.com/svn/tags/2.5.1/distro-source/core/src/gwt-module.dtd">
+<module rename-to='com.artemis.backends.gwt'>
+ <inherits name='com.google.gwt.user.User' />
+
+ <inherits name="com.artemis" />
+ <inherits name="com.artemis.gwtref.ArtemisReflect"/>
+
+ <super-source path="gwt/emu" />
+ <source path="gwt">
+ <exclude name="**/emu/**" />
+ </source>
+
+</module>
View
44 ...src/main/java/com/artemis/backends/gwt/emu/com/artemis/utils/reflect/ArrayReflection.java
@@ -0,0 +1,44 @@
+/*******************************************************************************
+ * Copyright 2011 See AUTHORS file.
+ *
+ * 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 com.artemis.utils.reflect;
+
+import com.artemis.gwtref.client.ReflectionCache;
+
+/** Utilities for Array reflection.
+ * @author nexsoftware */
+public final class ArrayReflection {
+
+ /** Creates a new array with the specified component type and length. */
+ static public Object newInstance (Class c, int size) {
+ return ReflectionCache.instance.newArray(c, size);
+ }
+
+ /** Returns the length of the supplied array. */
+ static public int getLength (Object array) {
+ return ReflectionCache.instance.getArrayLength(ReflectionCache.getType(array.getClass()), array);
+ }
+
+ /** Returns the value of the indexed component in the supplied array. */
+ static public Object get (Object array, int index) {
+ return ReflectionCache.instance.getArrayElement(ReflectionCache.getType(array.getClass()), array, index);
+ }
+
+ /** Sets the value of the indexed component in the supplied array to the supplied value. */
+ static public void set (Object array, int index, Object value) {
+ ReflectionCache.instance.setArrayElement(ReflectionCache.getType(array.getClass()), array, index, value);
+ }
+}
View
188 ...src/main/java/com/artemis/backends/gwt/emu/com/artemis/utils/reflect/ClassReflection.java
@@ -0,0 +1,188 @@
+/*******************************************************************************
+ * Copyright 2011 See AUTHORS file.
+ *
+ * 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 com.artemis.utils.reflect;
+
+import com.artemis.gwtref.client.ReflectionCache;
+import com.artemis.gwtref.client.Type;
+import com.artemis.utils.reflect.ReflectionException;
+
+/** Utilities for Class reflection.
+ * @author nexsoftware */
+public final class ClassReflection {
+
+ /** Returns the Class object associated with the class or interface with the supplied string name. */
+ static public Class forName (String name) throws ReflectionException {
+ try {
+ return ReflectionCache.forName(name).getClassOfType();
+ } catch (ClassNotFoundException e) {
+ throw new ReflectionException("Class not found: " + name);
+ }
+ }
+
+ /** Returns the simple name of the underlying class as supplied in the source code. */
+ static public String getSimpleName (Class c) {
+ return c.getName();
+ }
+
+ /** Determines if the supplied Object is assignment-compatible with the object represented by supplied Class. */
+ static public boolean isInstance (Class c, Object obj) {
+ return isAssignableFrom(c, obj.getClass());
+ }
+
+ /** Determines if the class or interface represented by first Class parameter is either the same as, or is a superclass or
+ * superinterface of, the class or interface represented by the second Class parameter. */
+ static public boolean isAssignableFrom (Class c1, Class c2) {
+ Type c1Type = ReflectionCache.getType(c1);
+ Type c2Type = ReflectionCache.getType(c2);
+ return c1Type.isAssignableFrom(c2Type);
+ }
+
+ /** Returns true if the class or interface represented by the supplied Class is a member class. */
+ static public boolean isMemberClass (Class c) {
+ return ReflectionCache.getType(c).isMemberClass();
+ }
+
+ /** Returns true if the class or interface represented by the supplied Class is a static class. */
+ static public boolean isStaticClass (Class c) {
+ return ReflectionCache.getType(c).isStatic();
+ }
+
+ /** Creates a new instance of the class represented by the supplied Class. */
+ static public <T> T newInstance (Class<T> c) throws ReflectionException {
+ try {
+ return (T)ReflectionCache.getType(c).newInstance();
+ } catch (NoSuchMethodException e) {
+ throw new ReflectionException("Could not use default constructor of " + c.getName(), e);
+ }
+ }
+
+ /** Returns an array of {@link Constructor} containing the public constructors of the class represented by the supplied Class. */
+ static public Constructor[] getConstructors (Class c) {
+ com.artemis.gwtref.client.Constructor[] constructors = ReflectionCache.getType(c).getConstructors();
+ Constructor[] result = new Constructor[constructors.length];
+ for (int i = 0, j = constructors.length; i < j; i++) {
+ result[i] = new Constructor(constructors[i]);
+ }
+ return result;
+ }
+
+ /** Returns a {@link Constructor} that represents the public constructor for the supplied class which takes the supplied
+ * parameter types. */
+ static public Constructor getConstructor (Class c, Class... parameterTypes) throws ReflectionException {
+ try {
+ return new Constructor(ReflectionCache.getType(c).getConstructor(parameterTypes));
+ } catch (SecurityException e) {
+ throw new ReflectionException("Security violation while getting constructor for class: " + c.getName(), e);
+ } catch (NoSuchMethodException e) {
+ throw new ReflectionException("Constructor not found for class: " + c.getName(), e);
+ }
+ }
+
+ /** Returns a {@link Constructor} that represents the constructor for the supplied class which takes the supplied parameter
+ * types. */
+ static public Constructor getDeclaredConstructor (Class c, Class... parameterTypes) throws ReflectionException {
+ try {
+ return new Constructor(ReflectionCache.getType(c).getDeclaredConstructor(parameterTypes));
+ } catch (SecurityException e) {
+ throw new ReflectionException("Security violation while getting constructor for class: " + c.getName(), e);
+ } catch (NoSuchMethodException e) {
+ throw new ReflectionException("Constructor not found for class: " + c.getName(), e);
+ }
+ }
+
+ /** Returns an array of {@link Method} containing the public member methods of the class represented by the supplied Class. */
+ static public Method[] getMethods (Class c) {
+ com.artemis.gwtref.client.Method[] methods = ReflectionCache.getType(c).getMethods();
+ Method[] result = new Method[methods.length];
+ for (int i = 0, j = methods.length; i < j; i++) {
+ result[i] = new Method(methods[i]);
+ }
+ return result;
+ }
+
+ /** Returns a {@link Method} that represents the public member method for the supplied class which takes the supplied parameter
+ * types. */
+ static public Method getMethod (Class c, String name, Class... parameterTypes) throws ReflectionException {
+ try {
+ return new Method(ReflectionCache.getType(c).getMethod(name, parameterTypes));
+ } catch (SecurityException e) {
+ throw new ReflectionException("Security violation while getting method: " + name + ", for class: " + c.getName(), e);
+ } catch (NoSuchMethodException e) {
+ throw new ReflectionException("Method not found: " + name + ", for class: " + c.getName(), e);
+ }
+ }
+
+ /** Returns an array of {@link Method} containing the methods declared by the class represented by the supplied Class. */
+ static public Method[] getDeclaredMethods (Class c) {
+ com.artemis.gwtref.client.Method[] methods = ReflectionCache.getType(c).getDeclaredMethods();
+ Method[] result = new Method[methods.length];
+ for (int i = 0, j = methods.length; i < j; i++) {
+ result[i] = new Method(methods[i]);
+ }
+ return result;
+ }
+
+ /** Returns a {@link Method} that represents the method declared by the supplied class which takes the supplied parameter types. */
+ static public Method getDeclaredMethod (Class c, String name, Class... parameterTypes) throws ReflectionException {
+ try {
+ return new Method(ReflectionCache.getType(c).getMethod(name, parameterTypes));
+ } catch (SecurityException e) {
+ throw new ReflectionException("Security violation while getting method: " + name + ", for class: " + c.getName(), e);
+ } catch (NoSuchMethodException e) {
+ throw new ReflectionException("Method not found: " + name + ", for class: " + c.getName(), e);
+ }
+ }
+
+ /** Returns an array of {@link Field} containing the public fields of the class represented by the supplied Class. */
+ static public Field[] getFields (Class c) {
+ com.artemis.gwtref.client.Field[] fields = ReflectionCache.getType(c).getFields();
+ Field[] result = new Field[fields.length];
+ for (int i = 0, j = fields.length; i < j; i++) {
+ result[i] = new Field(fields[i]);
+ }
+ return result;
+ }
+
+ /** Returns a {@link Field} that represents the specified public member field for the supplied class. */
+ static public Field getField (Class c, String name) throws ReflectionException {
+ try {
+ return new Field(ReflectionCache.getType(c).getField(name));
+ } catch (SecurityException e) {
+ throw new ReflectionException("Security violation while getting field: " + name + ", for class: " + c.getName(), e);
+ }
+ }
+
+ /** Returns an array of {@link Field} objects reflecting all the fields declared by the supplied class. */
+ static public Field[] getDeclaredFields (Class c) {
+ com.artemis.gwtref.client.Field[] fields = ReflectionCache.getType(c).getDeclaredFields();
+ Field[] result = new Field[fields.length];
+ for (int i = 0, j = fields.length; i < j; i++) {
+ result[i] = new Field(fields[i]);
+ }
+ return result;
+ }
+
+ /** Returns a {@link Field} that represents the specified declared field for the supplied class. */
+ static public Field getDeclaredField (Class c, String name) throws ReflectionException {
+ try {
+ return new Field(ReflectionCache.getType(c).getField(name));
+ } catch (SecurityException e) {
+ throw new ReflectionException("Security violation while getting field: " + name + ", for class: " + c.getName(), e);
+ }
+ }
+
+}
View
60 ...gwt/src/main/java/com/artemis/backends/gwt/emu/com/artemis/utils/reflect/Constructor.java
@@ -0,0 +1,60 @@
+/*******************************************************************************
+ * Copyright 2011 See AUTHORS file.
+ *
+ * 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 com.artemis.utils.reflect;
+
+import com.artemis.utils.reflect.ReflectionException;
+
+/** Provides information about, and access to, a single constructor for a Class.
+ * @author nexsoftware */
+public final class Constructor {
+
+ private final com.artemis.gwtref.client.Constructor constructor;
+
+ Constructor (com.artemis.gwtref.client.Constructor constructor) {
+ this.constructor = constructor;
+ }
+
+ /** Returns an array of Class objects that represent the formal parameter types, in declaration order, of the constructor. */
+ public Class[] getParameterTypes () {
+ return null;
+ }
+
+ /** Returns the Class object representing the class or interface that declares the constructor. */
+ public Class getDeclaringClass () {
+ return constructor.getEnclosingType();
+ }
+
+ public boolean isAccessible () {
+ return constructor.isPublic();
+ }
+
+ public void setAccessible (boolean accessible) {
+ // NOOP in GWT
+ }
+
+ /** Uses the constructor to create and initialize a new instance of the constructor's declaring class, with the supplied
+ * initialization parameters. */
+ public Object newInstance (Object... args) throws ReflectionException {
+ try {
+ return constructor.newInstance(args);
+ } catch (IllegalArgumentException e) {
+ throw new ReflectionException("Illegal argument(s) supplied to constructor for class: " + getDeclaringClass().getName(),
+ e);
+ }
+ }
+
+}
View
138 artemis-gwt/src/main/java/com/artemis/backends/gwt/emu/com/artemis/utils/reflect/Field.java
@@ -0,0 +1,138 @@
+/*******************************************************************************
+ * Copyright 2011 See AUTHORS file.
+ *
+ * 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 com.artemis.utils.reflect;
+
+import java.lang.annotation.Annotation;
+import java.util.Arrays;
+
+import com.artemis.gwtref.client.ReflectionCache;
+import com.artemis.gwtref.client.Type;
+import com.artemis.utils.reflect.ReflectionException;
+
+/** Provides information about, and access to, a single field of a class or interface.
+ * @author nexsoftware */
+public final class Field {
+
+ private final com.artemis.gwtref.client.Field field;
+
+ Field (com.artemis.gwtref.client.Field field) {
+ this.field = field;
+ }
+
+ /** Returns the name of the field. */
+ public String getName () {
+ return field.getName();
+ }
+
+ /** Returns a Class object that identifies the declared type for the field. */
+ public Class getType () {
+ return field.getType().getClassOfType();
+ }
+
+ /** Returns the Class object representing the class or interface that declares the field. */
+ public Class getDeclaringClass () {
+ return field.getEnclosingType().getClassOfType();
+ }
+
+ public boolean isAccessible () {
+ return field.isPublic();
+ }
+
+ public void setAccessible (boolean accessible) {
+ // NOOP in GWT
+ }
+
+ /** Return true if the field does not include any of the {@code private}, {@code protected}, or {@code public} modifiers. */
+ public boolean isDefaultAccess () {
+ return !isPrivate() && !isProtected() && !isPublic();
+ }
+
+ /** Return true if the field includes the {@code final} modifier. */
+ public boolean isFinal () {
+ return field.isFinal();
+ }
+
+ /** Return true if the field includes the {@code private} modifier. */
+ public boolean isPrivate () {
+ return field.isPrivate();
+ }
+
+ /** Return true if the field includes the {@code protected} modifier. */
+ public boolean isProtected () {
+ return field.isProtected();
+ }
+
+ /** Return true if the field includes the {@code public} modifier. */
+ public boolean isPublic () {
+ return field.isPublic();
+ }
+
+ /** Return true if the field includes the {@code static} modifier. */
+ public boolean isStatic () {
+ return field.isStatic();
+ }
+
+ /** Return true if the field includes the {@code transient} modifier. */
+ public boolean isTransient () {
+ return field.isTransient();
+ }
+
+ /** Return true if the field includes the {@code volatile} modifier. */
+ public boolean isVolatile () {
+ return field.isVolatile();
+ }
+
+ /** Return true if the field is a synthetic field. */
+ public boolean isSynthetic () {
+ return field.isSynthetic();
+ }
+
+ /** If the type of the field is parameterized, returns the Class object representing the parameter type at the specified index,
+ * null otherwise. */
+ public Class getElementType (int index) {
+ Type elementType = field.getElementType(index);
+ return elementType != null ? elementType.getClassOfType() : null;
+ }
+
+ /** Returns the value of the field on the supplied object. */
+ public Object get (Object obj) throws ReflectionException {
+ try {
+ return field.get(obj);
+ } catch (IllegalArgumentException e) {
+ throw new ReflectionException("Could not get " + getDeclaringClass() + "#" + getName() + ": " + e.getMessage(), e);
+ } catch (IllegalAccessException e) {
+ throw new ReflectionException("Illegal access to field " + getName() + ": " + e.getMessage(), e);
+ }
+ }
+
+ /** Sets the value of the field on the supplied object. */
+ public void set (Object obj, Object value) throws ReflectionException {
+ try {
+ field.set(obj, value);
+ } catch (IllegalArgumentException e) {
+ throw new ReflectionException("Could not set " + getDeclaringClass() + "#" + getName() + ": " + e.getMessage(), e);
+ } catch (IllegalAccessException e) {
+ throw new ReflectionException("Illegal access to field " + getName() + ": " + e.getMessage(), e);
+ }
+ }
+
+ @SuppressWarnings("rawtypes")
+ public boolean hasAnnotation(Class annotationClass) {
+ return Arrays.asList(field.getAnnotationClasses()).contains(annotationClass.getName());
+ }
+
+}
View
122 artemis-gwt/src/main/java/com/artemis/backends/gwt/emu/com/artemis/utils/reflect/Method.java
@@ -0,0 +1,122 @@
+/*******************************************************************************
+ * Copyright 2011 See AUTHORS file.
+ *
+ * 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 com.artemis.utils.reflect;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Modifier;
+
+import com.artemis.gwtref.client.Parameter;
+import com.artemis.utils.reflect.ReflectionException;
+
+/** Provides information about, and access to, a single method on a class or interface.
+ * @author nexsoftware */
+public final class Method {
+
+ private final com.artemis.gwtref.client.Method method;
+
+ Method (com.artemis.gwtref.client.Method method) {
+ this.method = method;
+ }
+
+ /** Returns the name of the method. */
+ public String getName () {
+ return method.getName();
+ }
+
+ /** Returns a Class object that represents the formal return type of the method. */
+ public Class getReturnType () {
+ return method.getReturnType();
+ }
+
+ /** Returns an array of Class objects that represent the formal parameter types, in declaration order, of the method. */
+ public Class[] getParameterTypes () {
+ Parameter[] parameters = method.getParameters();
+ Class[] parameterTypes = new Class[parameters.length];
+ for (int i = 0, j = parameters.length; i < j; i++) {
+ parameterTypes[i] = parameters[i].getType();
+ }
+ return parameterTypes;
+ }
+
+ /** Returns the Class object representing the class or interface that declares the method. */
+ public Class getDeclaringClass () {
+ return method.getEnclosingType();
+ }
+
+ public boolean isAccessible () {
+ return method.isPublic();
+ }
+
+ public void setAccessible (boolean accessible) {
+ // NOOP in GWT
+ }
+
+ /** Return true if the method includes the {@code abstract} modifier. */
+ public boolean isAbstract () {
+ return method.isAbstract();
+ }
+
+ /** Return true if the method does not include any of the {@code private}, {@code protected}, or {@code public} modifiers. */
+ public boolean isDefaultAccess () {
+ return !isPrivate() && !isProtected() && !isPublic();
+ }
+
+ /** Return true if the method includes the {@code final} modifier. */
+ public boolean isFinal () {
+ return method.isFinal();
+ }
+
+ /** Return true if the method includes the {@code private} modifier. */
+ public boolean isPrivate () {
+ return method.isPrivate();
+ }
+
+ /** Return true if the method includes the {@code protected} modifier. */
+ public boolean isProtected () {
+ return method.isProtected();
+ }
+
+ /** Return true if the method includes the {@code public} modifier. */
+ public boolean isPublic () {
+ return method.isPublic();
+ }
+
+ /** Return true if the method includes the {@code native} modifier. */
+ public boolean isNative () {
+ return method.isNative();
+ }
+
+ /** Return true if the method includes the {@code static} modifier. */
+ public boolean isStatic () {
+ return method.isStatic();
+ }
+
+ /** Return true if the method takes a variable number of arguments. */
+ public boolean isVarArgs () {
+ return method.isVarArgs();
+ }
+
+ /** Invokes the underlying method on the supplied object with the supplied parameters. */
+ public Object invoke (Object obj, Object... args) throws ReflectionException {
+ try {
+ return method.invoke(obj, args);
+ } catch (IllegalArgumentException e) {
+ throw new ReflectionException("Illegal argument(s) supplied to method: " + getName(), e);
+ }
+ }
+
+}
View
917 artemis-gwt/src/main/java/com/artemis/backends/gwt/emu/java/util/BitSet.java
@@ -0,0 +1,917 @@
+/*
+ * Copyright 2009 Google Inc.
+ *
+ * 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 java.util;
+
+import com.google.gwt.core.client.JavaScriptObject;
+import com.google.gwt.core.client.JsArrayInteger;
+
+/**
+ * This implementation uses bit groups of size 32 to keep track of when bits are
+ * set to true or false. This implementation also uses the sparse nature of
+ * JavaScript arrays to speed up cases when very few bits are set in a large bit
+ * set.
+ *
+ * Since there is no speed advantage to pre-allocating array sizes in JavaScript
+ * the underlying array's length is shrunk to Sun's "logical length" whenever
+ * length() is called. This length is the index of the highest true bit, plus
+ * one, or zero if there are aren't any. This may cause the size() method to
+ * return a different size than in a true Java VM.
+ */
+public class BitSet {
+ // To speed up certain operations this class also uses the index properties
+ // of arrays as described in section 15.4 of "Standard ECMA-262" (June
+ // 1997),
+ // which can currently be found here:
+ // http://www.mozilla.org/js/language/E262.pdf
+ //
+ // 15.4 Array Objects
+ // Array objects give special treatment to a certain class of property
+ // names.
+ // A property name P (in the form of a string value) is an array index if
+ // and
+ // only if ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal
+ // to (2^32)-1.
+
+ // checks the index range
+ private static void checkIndex(int bitIndex) {
+ // we only need to test for negatives, as there is no bit index too
+ // high.
+ if (bitIndex < 0) {
+ throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);
+ }
+ }
+
+ // checks to ensure indexes are not negative and not in reverse order
+ private static void checkRange(int fromIndex, int toIndex) {
+ if (fromIndex < 0) {
+ throw new IndexOutOfBoundsException("fromIndex < 0: " + fromIndex);
+ }
+ if (toIndex < 0) {
+ throw new IndexOutOfBoundsException("toIndex < 0: " + toIndex);
+ }
+ if (fromIndex > toIndex) {
+ throw new IndexOutOfBoundsException("fromIndex: " + fromIndex
+ + " > toIndex: " + toIndex);
+ }
+ }
+
+ // converts from a bit index to a word index
+ private static int wordIndex(int bitIndex) {
+ // 32 bits per index
+ return bitIndex >>> 5;
+ }
+
+ // converts from a word index to a bit index
+ private static int bitIndex(int wordIndex) {
+ // 1 word index for every 32 bit indexes
+ return wordIndex << 5;
+ }
+
+ // gives the word offset for a bit index
+ private static int bitOffset(int bitIndex) {
+ return bitIndex & 0x1f;
+ }
+
+ //
+ // none of the following static method perform any bounds checking
+ //
+
+ // clears one bit
+ private static void clear(JsArrayInteger array, int bitIndex) {
+ int index = wordIndex(bitIndex);
+ int word = getWord(array, index);
+ if (word != 0) {
+ // mask the correct bit out
+ setWord(array, index, word & ~(1 << (bitOffset(bitIndex))));
+ }
+ }
+
+ // clones the JSArrayInteger array
+ private static native JsArrayInteger clone(JsArrayInteger array) /*-{
+ return array.slice(0);
+ }-*/;
+
+ // flips one bit
+ private static void flip(JsArrayInteger array, int bitIndex) {
+ // calculate index and offset
+ int index = wordIndex(bitIndex);
+ int offset = bitOffset(bitIndex);
+
+ // figure out if the bit is on or off
+ int word = getWord(array, index);
+ if (((word >>> offset) & 1) == 1) {
+ // if on, turn it off
+ setWord(array, index, word & ~(1 << offset));
+ } else {
+ // if off, turn it on
+ array.set(index, word | (1 << offset));
+ }
+ };
+
+ // gets one bit
+ private static boolean get(JsArrayInteger array, int bitIndex) {
+ // retrieve the bits for the given index
+ int word = getWord(array, wordIndex(bitIndex));
+
+ // shift and mask the bit out
+ return ((word >>> (bitOffset(bitIndex))) & 1) == 1;
+ }
+
+ // sets one bit to true
+ private static void set(JsArrayInteger array, int bitIndex) {
+ int index = wordIndex(bitIndex);
+ array.set(index, getWord(array, index) | (1 << (bitOffset(bitIndex))));
+ }
+
+ // sets all bits to true within the given range
+ private static void set(JsArrayInteger array, int fromIndex, int toIndex) {
+ int first = wordIndex(fromIndex);
+ int last = wordIndex(toIndex);
+ int startBit = bitOffset(fromIndex);
+ int endBit = bitOffset(toIndex);
+
+ if (first == last) {
+ // set the bits in between first and last
+ maskInWord(array, first, startBit, endBit);
+
+ } else {
+ // set the bits from fromIndex to the next 32 bit boundary
+ if (startBit != 0) {
+ maskInWord(array, first++, startBit, 32);
+ }
+
+ // set the bits from the last 32 bit boundary to the toIndex
+ if (endBit != 0) {
+ maskInWord(array, last, 0, endBit);
+ }
+
+ //
+ // set everything in between
+ //
+ for (int i = first; i < last; i++) {
+ array.set(i, 0xffffffff);
+ }
+ }
+ }
+
+ // copies a subset of the array
+ private static native JsArrayInteger slice(JsArrayInteger array,
+ int fromIndex, int toIndex) /*-{
+ return array.slice(fromIndex, toIndex);
+ }-*/;
+
+ // trims the array to the minimum size it can without losing data
+ // returns index of the last element in the array, or -1 if empty
+ private static native int trimToSize(JsArrayInteger array) /*-{
+ var length = array.length;
+ if (length === 0) {
+ return -1;
+ }
+
+ // check if the last bit is false
+ var last = length - 1;
+ if (array[last] !== undefined) {
+ return last;
+ }
+
+ // interleave property checks and linear index checks from the end
+ var biggestSeen = -1;
+ for (var property in array) {
+
+ // test the index first
+ if (--last === -1) {
+ return -1;
+ }
+ if (array[last] !== undefined) {
+ return last;
+ }
+
+ // now check the property
+ var number = property >>> 0;
+ if (String(number) == property && number !== 0xffffffff) {
+ if (number > biggestSeen) {
+ biggestSeen = number;
+ }
+ }
+
+ }
+ array.length = biggestSeen + 1
+
+ return biggestSeen;
+ }-*/;
+
+ //
+ // word methods use the literal index into the array, not the bit index
+ //
+
+ // deletes an element from the array
+ private static native void deleteWord(JsArrayInteger array, int index) /*-{
+ delete array[index];
+ }-*/;
+
+ // flips all bits stored at a certain index
+ private static void flipWord(JsArrayInteger array, int index) {
+ int word = getWord(array, index);
+ if (word == 0) {
+ array.set(index, 0xffffffff);
+ } else {
+ word = ~word;
+ setWord(array, index, word);
+ }
+ }
+
+ // flips all bits stored at a certain index within the given range
+ private static void flipMaskedWord(JsArrayInteger array, int index,
+ int from, int to) {
+ if (from == to) {
+ return;
+ }
+ // get the bits
+ int word = getWord(array, index);
+ // adjust "to" so it will shift out those bits
+ to = 32 - to;
+ // create a mask and XOR it in
+ word ^= (((0xffffffff >>> from) << from) << to) >>> to;
+ ;
+ setWord(array, index, word);
+ }
+
+ // returns all bits stored at a certain index
+ private static native int getWord(JsArrayInteger array, int index) /*-{
+ // OR converts an undefined to 0
+ return array[index] | 0;
+ }-*/;
+
+ // sets all bits to true at a certain index within the given bit range
+ private static void maskInWord(JsArrayInteger array, int index, int from,
+ int to) {
+ // shifting by 32 is the same as shifting by 0, this check prevents that
+ // from happening in addition to the obvious avoidance of extra work
+ if (from != to) {
+ // adjust "to" so it will shift out those bits
+ to = 32 - to;
+ // create a mask and OR it in
+ int value = getWord(array, index);
+ value |= ((0xffffffff >>> from) << (from + to)) >>> to;
+ array.set(index, value);
+ }
+ };
+
+ // sets all bits to false at a certain index within the given bit range
+ private static void maskOutWord(JsArrayInteger array, int index, int from,
+ int to) {
+ int word = getWord(array, index);
+ // something only happens if word has bits set
+ if (word != 0) {
+ // create a mask
+ int mask;
+ if (from != 0) {
+ mask = 0xffffffff >>> (32 - from);
+ } else {
+ mask = 0;
+ }
+ // shifting by 32 is the same as shifting by 0
+ if (to != 32) {
+ mask |= 0xffffffff << to;
+ }
+
+ // mask it out
+ word &= mask;
+ setWord(array, index, word);
+ }
+ }
+
+ private static native int nextSetWord(JsArrayInteger array, int index) /*-{
+ // interleave property checks and linear "index" checks
+ var length = array.length;
+ var localMinimum = @java.lang.Integer::MAX_VALUE;
+ for (var property in array) {
+
+ // test the index first
+ if (array[index] !== undefined) {
+ return index;
+ }
+ if (++index >= length) {
+ return -1;
+ }
+
+ // now check the property
+ var number = property >>> 0;
+ if (String(number) == property && number !== 0xffffffff) {
+ if (number >= index && number < localMinimum) {
+ localMinimum = number;
+ }
+ }
+ }
+
+ // if local minimum is what we started at, we found nothing
+ if (localMinimum === @java.lang.Integer::MAX_VALUE) {
+ return -1;
+ }
+
+ return localMinimum;
+ }-*/;
+
+ // sets all bits at a certain index to the given value
+ private static void setWord(JsArrayInteger array, int index, int value) {
+ // keep 0s out of the array
+ if (value == 0) {
+ deleteWord(array, index);
+ } else {
+ array.set(index, value);
+ }
+ }
+
+ // sets the array length
+ private static native void setLengthWords(JsArrayInteger array, int length) /*-{
+ array.length = length;
+ }-*/;
+
+ // our array of bits
+ private JsArrayInteger array;
+
+ public BitSet() {
+ // create a new array
+ array = JavaScriptObject.createArray().cast();
+ }
+
+ public BitSet(int nbits) {
+ this();
+
+ // throw an exception to be consistent
+ // but (do we want to be consistent?)
+ if (nbits < 0) {
+ throw new NegativeArraySizeException("nbits < 0: " + nbits);
+ }
+
+ // even though the array's length is loosely kept to that of Sun's
+ // "logical
+ // length," this might help in some cases where code uses size() to fill
+ // in
+ // bits after constructing a BitSet, or after having one passed in as a
+ // parameter.
+ setLengthWords(array, wordIndex(nbits + 31));
+ }
+
+ private BitSet(JsArrayInteger array) {
+ this.array = array;
+ }
+
+ public void and(BitSet set) {
+ // a & a is just a
+ if (this == set) {
+ return;
+ }
+
+ // trim the second set to avoid extra work
+ trimToSize(set.array);
+
+ // check if the length is longer than otherLength
+ int otherLength = set.array.length();
+ if (array.length() > otherLength) {
+ // shrink the array, effectively ANDing those bits to false
+ setLengthWords(array, otherLength);
+ }
+
+ // truth table
+ //
+ // case | a | b | a & b | change?
+ // 1 | false | false | false | a is already false
+ // 2 | false | true | false | a is already false
+ // 3 | true | false | false | set a to false
+ // 4 | true | true | true | a is already true
+ //
+ // we only need to change something in case 3, so iterate over set a
+ int index = 0;
+ while ((index = nextSetWord(array, index)) != -1) {
+ setWord(array, index, array.get(index) & getWord(set.array, index));
+ index++;
+ }
+ }
+
+ public void andNot(BitSet set) {
+ // a & !a is false
+ if (this == set) {
+ // all falses result in an empty BitSet
+ clear();
+ return;
+ }
+
+ // trim the second set to avoid extra work
+ trimToSize(array);
+ int length = array.length();
+
+ // truth table
+ //
+ // case | a | b | !b | a & !b | change?
+ // 1 | false | false | true | false | a is already false
+ // 2 | false | true | false | false | a is already false
+ // 3 | true | false | true | true | a is already true
+ // 4 | true | true | false | false | set a to false
+ //
+ // we only need to change something in case 4
+ // whenever b is true, a should be false, so iterate over set b
+ int index = 0;
+ while ((index = nextSetWord(set.array, index)) != -1) {
+ setWord(array, index, getWord(array, index) & ~set.array.get(index));
+ if (++index >= length) {
+ // nothing further will affect anything
+ break;
+ }
+ }
+
+ }
+
+ public native int cardinality() /*-{
+ var count = 0;
+ var array = this.@java.util.BitSet::array;
+ for (var property in array) {
+ var number = property >>> 0;
+ if (String(number) == property && number !== 0xffffffff) {
+ count += @java.lang.Integer::bitCount(I)(array[number]);
+ }
+ }
+ return count;
+ }-*/;
+
+ public void clear() {
+ // create a new array
+ array = JavaScriptObject.createArray().cast();
+ }
+
+ public void clear(int bitIndex) {
+ checkIndex(bitIndex);
+ clear(array, bitIndex);
+ }
+
+ public void clear(int fromIndex, int toIndex) {
+ checkRange(fromIndex, toIndex);
+
+ int length = length();
+ if (fromIndex >= length) {
+ // nothing to do
+ return;
+ }
+
+ // check to see if toIndex is greater than our array length
+ if (toIndex >= length) {
+ // truncate the array by setting it's length
+ int newLength = wordIndex(fromIndex + 31);
+ setLengthWords(array, newLength);
+
+ // remove the extra bits off the end
+ if ((bitIndex(newLength)) - fromIndex != 0) {
+ maskOutWord(array, newLength - 1, bitOffset(fromIndex), 32);
+ }
+
+ } else {
+ int first = wordIndex(fromIndex);
+ int last = wordIndex(toIndex);
+ int startBit = bitOffset(fromIndex);
+ int endBit = bitOffset(toIndex);
+
+ if (first == last) {
+ // clear the bits in between first and last
+ maskOutWord(array, first, startBit, endBit);
+
+ } else {
+ // clear the bits from fromIndex to the next 32 bit boundary
+ if (startBit != 0) {
+ maskOutWord(array, first++, startBit, 32);
+ }
+
+ // clear the bits from the last 32 bit boundary to the toIndex
+ if (endBit != 0) {
+ maskOutWord(array, last, 0, endBit);
+ }
+
+ //
+ // delete everything in between
+ //
+ for (int i = first; i < last; i++) {
+ deleteWord(array, i);
+ }
+ }
+ }
+ }
+
+ public Object clone() {
+ return new BitSet(clone(array));
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this != obj) {
+
+ if (!(obj instanceof BitSet)) {
+ return false;
+ }
+
+ BitSet other = (BitSet) obj;
+
+ int last = trimToSize(array);
+ if (last != trimToSize(other.array)) {
+ return false;
+ }
+
+ int index = 0;
+ while ((index = nextSetWord(array, index)) != -1) {
+ if (getWord(array, index) != getWord(other.array, index)) {
+ return false;
+ }
+ index++;
+ }
+ }
+
+ return true;
+ }
+
+ public void flip(int bitIndex) {
+ checkIndex(bitIndex);
+ flip(array, bitIndex);
+ }
+
+ public void flip(int fromIndex, int toIndex) {
+ checkRange(fromIndex, toIndex);
+
+ int length = length();
+
+ // if we are flipping bits beyond our length, we are setting them to
+ // true
+ if (fromIndex >= length) {
+ set(array, fromIndex, toIndex);
+ return;
+ }
+
+ // check to see if toIndex is greater than our array length
+ if (toIndex >= length) {
+ set(array, length, toIndex);
+ toIndex = length;
+ }
+
+ int first = wordIndex(fromIndex);
+ int last = wordIndex(toIndex);
+ int startBit = bitOffset(fromIndex);
+ int end = bitOffset(toIndex);
+
+ if (first == last) {
+ // flip the bits in between first and last
+ flipMaskedWord(array, first, startBit, end);
+
+ } else {
+ // clear the bits from fromIndex to the next 32 bit boundary
+ if (startBit != 0) {
+ flipMaskedWord(array, first++, startBit, 32);
+ }
+
+ // clear the bits from the last 32 bit boundary to the toIndex
+ if (end != 0) {
+ flipMaskedWord(array, last, 0, end);
+ }
+
+ // flip everything in between
+ for (int i = first; i < last; i++) {
+ flipWord(array, i);
+ }
+ }
+ }
+
+ public boolean get(int bitIndex) {
+ checkIndex(bitIndex);
+ return get(array, bitIndex);
+ }
+
+ public BitSet get(int fromIndex, int toIndex) {
+ checkRange(fromIndex, toIndex);
+
+ // no need to go past our length
+ int length = length();
+ if (toIndex >= length) {
+ toIndex = length();
+ }
+
+ // this is the bit shift offset for each group of bits
+ int rightShift = bitOffset(fromIndex);
+
+ if (rightShift == 0) {
+ int subFrom = wordIndex(fromIndex);
+ int subTo = wordIndex(toIndex + 31);
+ JsArrayInteger subSet = slice(array, subFrom, subTo);
+ int leftOvers = bitOffset(toIndex);
+ if (leftOvers != 0) {
+ maskOutWord(subSet, subTo - subFrom - 1, leftOvers, 32);
+ }
+ return new BitSet(subSet);
+ }
+
+ BitSet subSet = new BitSet();
+
+ int first = wordIndex(fromIndex);
+ int last = wordIndex(toIndex);
+
+ if (first == last) {
+ // number of bits to cut from the end
+ int end = 32 - (bitOffset(toIndex));
+ // raw bits
+ int word = getWord(array, first);
+ // shift out those bits
+ word = ((word << end) >>> end) >>> rightShift;
+ // set it
+ if (word != 0) {
+ subSet.set(0, word);
+ }
+
+ } else {
+ // this will hold the newly packed bits
+ int current = 0;
+
+ // this is the raw index into the sub set
+ int subIndex = 0;
+
+ // fence post, carry over initial bits
+ int word = getWord(array, first++);
+ current = word >>> rightShift;
+
+ // a left shift will be used to shift our bits to the top of
+ // "current"
+ int leftShift = 32 - rightShift;
+
+ // loop through everything in the middle
+ for (int i = first; i <= last; i++) {
+ word = getWord(array, i);
+
+ // shift out the bits from the top, OR them into current bits
+ current |= word << leftShift;
+
+ // flush it out
+ if (current != 0) {
+ subSet.array.set(subIndex, current);
+ }
+
+ // keep track of our index
+ subIndex++;
+
+ // carry over the unused bits
+ current = word >>> rightShift;
+ }
+
+ // fence post, flush out the extra bits, but don't go past the "end"
+ int end = 32 - (bitOffset(toIndex));
+ current = (current << (rightShift + end)) >>> (rightShift + end);
+ if (current != 0) {
+ subSet.array.set(subIndex, current);
+ }
+ }
+
+ return subSet;
+ }
+
+ /**
+ * This hash is different than the one described in Sun's documentation. The
+ * described hash uses 64 bit integers and that's not practical in
+ * JavaScript.
+ */
+ @Override
+ public int hashCode() {
+ // FNV constants
+ final int fnvOffset = 0x811c9dc5;
+ final int fnvPrime = 0x1000193;
+
+ // initialize
+ final int last = trimToSize(array);
+ int hash = fnvOffset ^ last;
+
+ // loop over the data
+ for (int i = 0; i <= last; i++) {
+ int value = getWord(array, i);
+ // hash one byte at a time using FNV1
+ hash = (hash * fnvPrime) ^ (value & 0xff);
+ hash = (hash * fnvPrime) ^ ((value >>> 8) & 0xff);
+ hash = (hash * fnvPrime) ^ ((value >>> 16) & 0xff);
+ hash = (hash * fnvPrime) ^ (value >>> 24);
+ }
+
+ return hash;
+ }
+
+ public boolean intersects(BitSet set) {
+ int last = trimToSize(array);
+
+ if (this == set) {
+ // if it has any bits then it intersects itself
+ return last != -1;
+ }
+
+ int length = set.array.length();
+ int index = 0;
+ while ((index = nextSetWord(array, index)) != -1) {
+ if ((array.get(index) & getWord(set.array, index)) != 0) {
+ return true;
+ }
+ if (++index >= length) {
+ // nothing further can intersect
+ break;
+ }
+ }
+
+ return false;
+ }
+
+ public boolean isEmpty() {
+ return length() == 0;
+ }
+
+ public int length() {
+ int last = trimToSize(array);
+ if (last == -1) {
+ return 0;
+ }
+
+ // compute the position of the leftmost bit's index
+ int offsets[] = { 16, 8, 4, 2, 1 };
+ int bitMasks[] = { 0xffff0000, 0xff00, 0xf0, 0xc, 0x2 };
+ int position = bitIndex(last) + 1;
+ int word = getWord(array, last);
+ for (int i = 0; i < offsets.length; i++) {
+ if ((word & bitMasks[i]) != 0) {
+ word >>>= offsets[i];
+ position += offsets[i];
+ }
+ }
+ return position;
+ }
+
+ public int nextClearBit(int fromIndex) {
+ checkIndex(fromIndex);
+ int index = wordIndex(fromIndex);
+
+ // special case for first index
+ int fromBit = fromIndex - (bitIndex(index));
+ int word = getWord(array, index);
+ for (int i = fromBit; i < 32; i++) {
+ if ((word & (1 << i)) == 0) {
+ return (bitIndex(index)) + i;
+ }
+ }
+
+ // loop through the rest
+ while (true) {
+ index++;
+ word = getWord(array, index);
+ if (word != 0xffffffff) {
+ return (bitIndex(index)) + Integer.numberOfTrailingZeros(~word);
+ }
+ }
+ }
+
+ public int nextSetBit(int fromIndex) {
+ checkIndex(fromIndex);
+
+ int index = wordIndex(fromIndex);
+
+ // check the current word
+ int word = getWord(array, index);
+ if (word != 0) {
+ for (int i = bitOffset(fromIndex); i < 32; i++) {
+ if ((word & (1 << i)) != 0) {
+ return (bitIndex(index)) + i;
+ }
+ }
+ }
+ index++;
+
+ // find the next set word
+ trimToSize(array);
+ index = nextSetWord(array, index);
+ if (index == -1) {
+ return -1;
+ }
+
+ // return the next set bit
+ return (bitIndex(index))
+ + Integer.numberOfTrailingZeros(array.get(index));
+ };
+
+ public void or(BitSet set) {
+ // a | a is just a
+ if (this == set) {
+ return;
+ }
+
+ // truth table
+ //
+ // case | a | b | a | b | change?
+ // 1 | false | false | false | a is already false
+ // 2 | false | true | true | set a to true
+ // 3 | true | false | true | a is already true
+ // 4 | true | true | true | a is already true
+ //
+ // we only need to change something in case 2
+ // case 2 only happens when b is true, so iterate over set b
+ int index = 0;
+ while ((index = nextSetWord(set.array, index)) != -1) {
+ setWord(array, index, getWord(array, index) | set.array.get(index));
+ index++;
+ }
+ }
+
+ public void set(int bitIndex) {
+ checkIndex(bitIndex);
+ set(array, bitIndex);
+ }
+
+ public void set(int bitIndex, boolean value) {
+ if (value == true) {
+ set(bitIndex);
+ } else {
+ clear(bitIndex);
+ }
+ }
+
+ public void set(int fromIndex, int toIndex) {
+ checkRange(fromIndex, toIndex);
+ set(array, fromIndex, toIndex);
+ }
+
+ public void set(int fromIndex, int toIndex, boolean value) {
+ if (value == true) {
+ set(fromIndex, toIndex);
+ } else {
+ clear(fromIndex, toIndex);
+ }
+ }
+
+ public int size() {
+ // the number of bytes that can fit without using "more" memory
+ return bitIndex(array.length());
+ }
+
+ @Override
+ public String toString() {
+ // possibly faster if done in JavaScript and all numerical properties
+ // are
+ // put into an array and sorted
+
+ int length = length();
+ if (length == 0) {
+ // a "length" of 0 means there are no bits set to true
+ return "{}";
+ }
+
+ StringBuilder sb = new StringBuilder("{");
+
+ // at this point, there is at least one true bit, nextSetBit can not
+ // fail
+ int next = nextSetBit(0);
+ sb.append(next);
+
+ // loop until nextSetBit returns -1
+ while ((next = nextSetBit(next + 1)) != -1) {
+ sb.append(", ");
+ sb.append(next);
+ }
+
+ sb.append("}");
+ return sb.toString();
+ }
+
+ public void xor(BitSet set) {
+ // a ^ a is false
+ if (this == set) {
+ // this results in an empty BitSet
+ clear();
+ return;
+ }
+
+ // truth table
+ //
+ // case | a | b | a ^ b | change?
+ // 1 | false | false | false | a is already false
+ // 2 | false | true | true | set a to true
+ // 3 | true | false | true | a is already true
+ // 4 | true | true | false | set a to false
+ //
+ // we need to change something in cases 2 and 4
+ // cases 2 and 4 only happen when b is true, so iterate over set b
+ int index = 0;
+ while ((index = nextSetWord(set.array, index)) != -1) {
+ setWord(array, index, getWord(array, index) ^ set.array.get(index));
+ index++;
+ }
+ }
+
+}
View
112 artemis-gwt/src/main/java/com/artemis/backends/gwt/emu/java/util/UUID.java
@@ -0,0 +1,112 @@
+package java.util;
+
+import java.io.Serializable;
+
+
+/**
+ * @author senk.christian@gmail.com
+ *
+ */
+public class UUID implements Serializable, Comparable<UUID> {
+
+ private static final long serialVersionUID = 7373345728974414241L;
+ private static final char[] CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
+
+ private String value;
+
+ /**
+ *
+ */
+ private UUID() {}
+
+ /**
+ * @param uuidString
+ * @return
+ */
+ public static UUID fromString(String uuidString) {
+ //TODO: Validation
+
+ final UUID uuid = new UUID();
+ uuid.value = uuidString;
+
+ return uuid;
+ }
+
+ /**
+ * @return
+ */
+ public static UUID randomUUID() {
+ return fromString(generateUUIDString());
+ }
+
+ /**
+ * Generate a RFC4122, version 4 ID. Example:
+ * "92329D39-6F5C-4520-ABFC-AAB64544E172"
+ */
+ private static String generateUUIDString() {
+ char[] uuid = new char[36];
+ int r;
+
+ // rfc4122 requires these characters
+ uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
+ uuid[14] = '4';
+
+ // Fill in random data. At i==19 set the high bits of clock sequence as
+ // per rfc4122, sec. 4.1.5
+ for (int i = 0; i < 36; i++) {
+ if (uuid[i] == 0) {
+ r = (int) (Math.random()*16);
+ uuid[i] = CHARS[(i == 19) ? (r & 0x3) | 0x8 : r & 0xf];
+ }
+ }
+ return new String(uuid);
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Comparable#compareTo(java.lang.Object)
+ */
+ @Override
+ public int compareTo(UUID arg0) {
+ return value.compareTo(arg0.value);
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#hashCode()
+ */
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((value == null) ? 0 : value.hashCode());
+ return result;
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ UUID other = (UUID) obj;
+ if (value == null) {
+ if (other.value != null)
+ return false;
+ } else if (!value.equals(other.value))
+ return false;
+ return true;
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString() {
+ return value;
+ }
+
+}
View
25 artemis-gwt/src/main/java/com/artemis/gwtref/ArtemisReflect.gwt.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit trunk//EN" "http://google-web-toolkit.googlecode.com/svn/trunk/distro-source/core/src/gwt-module.dtd">
+<module>
+ <inherits name='com.google.gwt.user.User' />
+ <define-configuration-property name="artemis.reflect.include"
+ is-multi-valued="true" />
+ <define-configuration-property name="artemis.reflect.exclude"
+ is-multi-valued="true" />
+
+ <extend-configuration-property name="artemis.reflect.include" value="com.artemis.PackedComponent" />
+ <extend-configuration-property name="artemis.reflect.include" value="com.artemis.PooledComponent" />
+ <extend-configuration-property name="artemis.reflect.include" value="com.artemis.annotations.Mapper" />
+ <extend-configuration-property name="artemis.reflect.include" value="com.artemis.ComponentType" />
+ <extend-configuration-property name="artemis.reflect.include" value="com.artemis.ComponentMapper" />
+ <extend-configuration-property name="artemis.reflect.include" value="com.artemis.Component" />
+ <extend-configuration-property name="artemis.reflect.include" value="com.artemis.EntityManager" />
+ <extend-configuration-property name="artemis.reflect.include" value="com.artemis.managers.GroupManager" />
+ <extend-configuration-property name="artemis.reflect.include" value="com.artemis.managers.PlayerManager" />
+ <extend-configuration-property name="artemis.reflect.include" value="com.artemis.managers.TagManager" />
+ <extend-configuration-property name="artemis.reflect.include" value="com.artemis.managers.TeamManager" />
+
+ <generate-with class="com.artemis.gwtref.gen.ReflectionCacheGenerator">
+ <when-type-assignable class="com.artemis.gwtref.client.IReflectionCache" />
+ </generate-with>
+</module>
View
33 artemis-gwt/src/main/java/com/artemis/gwtref/client/Constructor.java
@@ -0,0 +1,33 @@
+/*******************************************************************************
+ * Copyright 2011 See AUTHORS file.
+ *
+ * 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 com.artemis.gwtref.client;
+
+/** A constructor for the enclosing type.
+ * @author mzechner */
+public class Constructor extends Method {
+ Constructor (String name, Class enclosingType, Class returnType, Parameter[] parameters, boolean isAbstract, boolean isFinal,
+ boolean isStatic, boolean isDefaultAccess, boolean isPrivate, boolean isProtected, boolean isPublic, boolean isNative,
+ boolean isVarArgs, boolean isMethod, boolean isConstructor, int methodId) {
+ super(name, enclosingType, returnType, parameters, isAbstract, isFinal, isStatic, isDefaultAccess, isPrivate, isProtected,
+ isPublic, isNative, isVarArgs, isMethod, isConstructor, methodId);
+ }
+
+ /** @return a new instance of the enclosing type of this constructor. */
+ public Object newInstance (Object... params) {
+ return super.invoke(null, params);
+ }
+}
View
130 artemis-gwt/src/main/java/com/artemis/gwtref/client/Field.java
@@ -0,0 +1,130 @@
+/*******************************************************************************
+ * Copyright 2011 See AUTHORS file.
+ *
+ * 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 com.artemis.gwtref.client;
+
+import java.util.Arrays;
+
+public class Field {
+ final String name;
+ final Class enclosingType;
+ final Class type;
+ final boolean isFinal;
+ final boolean isDefaultAccess;
+ final boolean isPrivate;
+ final boolean isProtected;
+ final boolean isPublic;
+ final boolean isStatic;
+ final boolean isTransient;
+ final boolean isVolatile;
+ final int getter;
+ final int setter;
+ final Class[] elementTypes;
+ final String[] annotationClasses;
+
+ Field (String name, Class enclosingType, Class type, boolean isFinal, boolean isDefaultAccess, boolean isPrivate,
+ boolean isProtected, boolean isPublic, boolean isStatic, boolean isTransient, boolean isVolatile, int getter, int setter,
+ Class[] elementTypes, String[] annotationClasses) {
+ this.name = name;
+ this.enclosingType = enclosingType;
+ this.type = type;
+ this.isFinal = isFinal;
+ this.isDefaultAccess = isDefaultAccess;
+ this.isPrivate = isPrivate;
+ this.isProtected = isProtected;
+ this.isPublic = isPublic;
+ this.isStatic = isStatic;
+ this.isTransient = isTransient;
+ this.isVolatile = isVolatile;
+ this.getter = getter;
+ this.setter = setter;
+ this.elementTypes = elementTypes;
+ this.annotationClasses = annotationClasses;
+ }
+
+ public String[] getAnnotationClasses() {
+ return annotationClasses;
+ }
+
+ public Object get (Object obj) throws IllegalAccessException {
+ return ReflectionCache.instance.get(this, obj);
+ }
+
+ public void set (Object obj, Object value) throws IllegalAccessException {
+ ReflectionCache.instance.set(this, obj, value);
+ }
+
+ public Type getElementType (int index) {
+ if (elementTypes != null && index < elementTypes.length) return ReflectionCache.getType(elementTypes[index]);
+ return null;
+ }
+
+ public String getName () {
+ return name;
+ }
+
+ public Type getEnclosingType () {
+ return ReflectionCache.getType(enclosingType);
+ }
+
+ public Type getType () {
+ return ReflectionCache.getType(type);
+ }
+
+ public boolean isSynthetic () {
+ return false;
+ }
+
+ public boolean isFinal () {
+ return isFinal;
+ }
+
+ public boolean isDefaultAccess () {
+ return isDefaultAccess;
+ }
+
+ public boolean isPrivate () {
+ return isPrivate;
+ }
+
+ public boolean isProtected () {
+ return isProtected;
+ }
+
+ public boolean isPublic () {
+ return isPublic;
+ }
+
+ public boolean isStatic () {
+ return isStatic;
+ }
+
+ public boolean isTransient () {
+ return isTransient;
+ }
+
+ public boolean isVolatile () {
+ return isVolatile;
+ }
+
+ @Override
+ public String toString () {
+ return "Field [name=" + name + ", enclosingType=" + enclosingType + ", type=" + type + ", isFinal=" + isFinal
+ + ", isDefaultAccess=" + isDefaultAccess + ", isPrivate=" + isPrivate + ", isProtected=" + isProtected + ", isPublic="
+ + isPublic + ", isStatic=" + isStatic + ", isTransient=" + isTransient + ", isVolatile=" + isVolatile + ", getter="
+ + getter + ", setter=" + setter + ", elementTypes=" + Arrays.toString(elementTypes) + "]";
+ }
+}
View
42 artemis-gwt/src/main/java/com/artemis/gwtref/client/IReflectionCache.java
@@ -0,0 +1,42 @@
+/*******************************************************************************
+ * Copyright 2011 See AUTHORS file.
+ *
+ * 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 com.artemis.gwtref.client;
+
+import java.util.Collection;
+
+public interface IReflectionCache {
+ // Class level methods
+ public Collection<Type> getKnownTypes ();
+
+ public Type forName (String name);
+
+ public Object newArray (Class componentType, int size);
+
+ public int getArrayLength (Type type, Object obj);
+
+ public Object getArrayElement (Type type, Object obj, int i);
+
+ public void setArrayElement (Type type, Object obj, int i, Object value);
+
+ // Field Methods
+ public Object get (Field field, Object obj) throws IllegalAccessException;
+
+ public void set (Field field, Object obj, Object value) throws IllegalAccessException;
+
+ // Method Methods :p
+ public Object invoke (Method m, Object obj, Object[] params);
+}
View
161 artemis-gwt/src/main/java/com/artemis/gwtref/client/Method.java
@@ -0,0 +1,161 @@
+/*******************************************************************************
+ * Copyright 2011 See AUTHORS file.
+ *
+ * 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 com.artemis.gwtref.client;
+
+import java.util.Arrays;
+
+/** Describes a method of a {@link Type}.
+ * @author mzechner */
+public class Method {
+ private static final Parameter[] EMPTY_PARAMS = new Parameter[0];
+ final String name;
+ final Class enclosingType;
+ final Class returnType;
+ final boolean isAbstract;
+ final boolean isFinal;
+ final boolean isStatic;
+ final boolean isNative;
+ final boolean isDefaultAccess;
+ final boolean isPrivate;
+ final boolean isProtected;
+ final boolean isPublic;
+ final boolean isVarArgs;
+ final boolean isMethod;
+ final boolean isConstructor;
+ final Parameter[] parameters;
+ final int methodId;
+
+ public Method (String name, Class enclosingType, Class returnType, Parameter[] parameters, boolean isAbstract,
+ boolean isFinal, boolean isStatic, boolean isDefaultAccess, boolean isPrivate, boolean isProtected, boolean isPublic,
+ boolean isNative, boolean isVarArgs, boolean isMethod, boolean isConstructor, int methodId) {
+ this.name = name;
+ this.enclosingType = enclosingType;
+ this.parameters = parameters != null ? parameters : EMPTY_PARAMS;
+ this.returnType = returnType;
+ this.isAbstract = isAbstract;
+ this.isFinal = isFinal;
+ this.isStatic = isStatic;
+ this.isNative = isNative;
+ this.isDefaultAccess = isDefaultAccess;
+ this.isPrivate = isPrivate;
+ this.isProtected = isProtected;
+ this.isPublic = isPublic;
+ this.isVarArgs = isVarArgs;
+ this.isMethod = isMethod;
+ this.isConstructor = isConstructor;
+ this.methodId = methodId;
+ }
+
+ /** @return the {@link Class} of the enclosing type. */
+ public Class getEnclosingType () {
+ return enclosingType;
+ }
+
+ /** @return the {@link Class} of the return type or null. */
+ public Class getReturnType () {
+ return returnType;
+ }
+
+ /** @return the list of parameters, can be a zero size array. */
+ public Parameter[] getParameters () {
+ return parameters;
+ }
+
+ /** @return the name of the method. */
+ public String getName () {
+ return name;
+ }
+
+ public boolean isAbstract () {
+ return isAbstract;
+ }
+
+ public boolean isFinal () {
+ return isFinal;
+ }
+
+ public boolean isDefaultAccess () {
+ return isDefaultAccess;
+ }
+
+ public boolean isPrivate () {
+ return isPrivate;
+ }
+
+ public boolean isProtected () {
+ return isProtected;
+ }
+
+ public boolean isPublic () {
+ return isPublic;
+ }
+
+ public boolean isNative () {
+ return isNative;
+ }
+
+ public boolean isVarArgs () {
+ return isVarArgs;
+ }
+
+ public boolean isStatic () {
+ return isStatic;
+ }
+
+ public boolean isMethod () {
+ return isMethod;
+ }
+
+ public boolean isConstructor () {
+ return isConstructor;
+ }
+
+ /** Invokes the method on the given object. Ignores the object if this is a static method. Throws an IllegalArgumentException if
+ * the parameters do not match.
+ * @param obj the object to invoke the method on or null.
+ * @param params the parameters to pass to the method or null.
+ * @return the return value or null if the method does not return anything. */
+ public Object invoke (Object obj, Object... params) {
+ if (parameters.length != (params != null ? params.length : 0)) throw new IllegalArgumentException("Parameter mismatch");
+
+ return ReflectionCache.instance.invoke(this, obj, params);
+ }
+
+ boolean match (String name, Class... types) {
+ return this.name.equals(name) && match(types);
+ }
+
+ boolean match (Class... types) {
+ if (types == null) return parameters.length == 0;
+ if (types.length != parameters.length) return false;
+ for (int i = 0; i < types.length; i++) {
+ Type t1 = ReflectionCache.instance.forName(parameters[i].getType().getName());
+ Type t2 = ReflectionCache.instance.forName(types[i].getName());
+ if (t1 != t2 && !t1.isAssignableFrom(t2)) return false;
+ }
+ return true;
+ }
+
+ @Override
+ public String toString () {
+ return "Method [name=" + name + ", enclosingType=" + enclosingType + ", returnType=" + returnType + ", isAbstract="
+ + isAbstract + ", isFinal=" + isFinal + ", isStatic=" + isStatic + ", isNative=" + isNative + ", isDefaultAccess="
+ + isDefaultAccess + ", isPrivate=" + isPrivate + ", isProtected=" + isProtected + ", isPublic=" + isPublic
+ + ", isVarArgs=" + isVarArgs + ", isMethod=" + isMethod + ", isConstructor=" + isConstructor + ", parameters="
+ + Arrays.toString(parameters) + "]";
+ }
+}
View
46 artemis-gwt/src/main/java/com/artemis/gwtref/client/Parameter.java
@@ -0,0 +1,46 @@
+/*******************************************************************************
+ * Copyright 2011 See AUTHORS file.
+ *
+ * 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 com.artemis.gwtref.client;
+
+public class Parameter {
+ final String name;
+ final Class type;
+ final String jnsi;
+
+ Parameter (String name, Class type, String jnsi) {
+ this.name = name;
+ this.type = type;
+ this.jnsi = jnsi;
+ }
+
+ public String getName () {
+ return name;
+ }
+
+ public Class getType () {
+ return type;
+ }
+
+ public String getJnsi () {
+ return jnsi;
+ }
+
+ @Override
+ public String toString () {
+ return "Parameter [name=" + name + ", type=" + type + ", jnsi=" + jnsi + "]";
+ }
+}
View
88 artemis-gwt/src/main/java/com/artemis/gwtref/client/ReflectionCache.java
@@ -0,0 +1,88 @@
+/*******************************************************************************
+ * Copyright 2011 See AUTHORS file.
+ *
+ * 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 com.artemis.gwtref.client;
+
+import java.util.Collection;
+
+import com.google.gwt.core.client.GWT;
+
+public class ReflectionCache {
+ public static IReflectionCache instance = GWT.create(IReflectionCache.class);
+
+ public static Type forName (String name) throws ClassNotFoundException {
+ Type type = instance.forName(convert(name));
+ if (type == null) {
+ throw new RuntimeException("Couldn't find Type for class '" + name + "'");
+ }
+ return type;
+ }
+
+ public static Type getType (Class clazz) {
+ if (clazz == null) return null;
+ Type type = instance.forName(convert(clazz.getName()));
+ if (type == null) {
+ throw new RuntimeException("Couldn't find Type for class '" + clazz.getName() + "'");
+ }
+ return type;
+ }
+
+ private static String convert (String className) {
+ if (className.startsWith("[")) {
+ int dimensions = 0;
+ char c = className.charAt(0);
+ String suffix = "";
+ while (c == '[') {
+ dimensions++;
+ suffix += "[]";
+ c = className.charAt(dimensions);
+ }
+ char t = className.charAt(dimensions);
+ switch (t) {
+ case 'Z':
+ return "boolean" + suffix;
+ case 'B':
+ return "byte" + suffix;
+ case 'C':
+ return "char" + suffix;
+ case 'L':
+ return className.substring(dimensions + 1, className.length() - 1).replace('$', '.') + suffix;
+ case 'D':
+ return "double" + suffix;
+ case 'F':
+ return "float" + suffix;
+ case 'I':
+ return "int" + suffix;
+ case 'J':
+ return "long" + suffix;
+ case 'S':
+ return "short" + suffix;
+ default:
+ throw new IllegalArgumentException("Couldn't transform '" + className + "' to qualified source name");
+ }
+ } else {
+ return className.replace('$', '.');
+ }
+ }
+
+ public static Object newArray (Class componentType, int size) {
+ return instance.newArray(componentType, size);
+ }
+
+ public static Collection<Type> getKnownTypes () {
+ return instance.getKnownTypes();
+ }
+}
View
99 artemis-gwt/src/main/java/com/artemis/gwtref/client/Test.java
@@ -0,0 +1,99 @@
+/*******************************************************************************
+ * Copyright 2011 See AUTHORS file.
+ *
+ * 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 com.artemis.gwtref.client;
+
+import com.google.gwt.core.client.EntryPoint;
+
+public class Test implements EntryPoint {
+ public static enum Enu {
+ Winter, Summer, Bleh;
+ }
+
+ public static class A {
+ String text;
+ float numberf;
+ int numberi;
+
+ public String getText () {
+ return text;
+ }
+
+ public void setText (String text) {
+ this.text = text;
+ }
+
+ public float getNumberf () {
+ return numberf;
+ }
+
+ public void setNumberf (float numberf) {
+ this.numberf = numberf;
+ }
+
+ public int getNumberi () {
+ return numberi;
+ }
+
+ public void setNumberi (int numberi) {
+ this.numberi = numberi;
+ }
+
+ public float getSum (float a, float b) {
+ return a + b;
+ }
+ }
+
+ public static class B extends A {
+ String text = "This is a string";
+
+ public void testWithPackagePrivate (C c, int a) {
+ }
+
+ public void testWidthPrivate (A c) {
+ }
+
+ public void testVoid () {
+ }
+
+ public native void test (A c) /*-{
+ // this.@com.badlogic.gwtref.client.Test.B::testWidthPrivate(LC;)(c);
+ }-*/;
+ }
+
+ public static class C {
+ }
+
+ @Override
+ public void onModuleLoad () {
+ try {
+ Type ta = ReflectionCache.getType(A.class);
+ Type tb = ReflectionCache.getType(B.class);
+ B b = (B)tb.newInstance();
+ for (Field f : tb.getFields())
+ System.out.println(f);
+ for (Method m : tb.getMethods())
+ System.out.println(m);
+
+ tb.getDeclaredFields()[0].set(b, "Field of B");
+ ta.getDeclaredFields()[0].set(b, "Field of A");
+ System.out.println(ta.getMethod("getText").invoke(b));
+ System.out.println(ta.getMethod("getSum", float.class, float.class).invoke(b, 1, 2));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+}
View
246 artemis-gwt/src/main/java/com/artemis/gwtref/client/Type.java
@@ -0,0 +1,246 @@
+/*******************************************************************************
+ * Copyright 2011 See AUTHORS file.
+ *
+ * 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 com.artemis.gwtref.client;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+/** Describes a type (equivalent to {@link Class}), providing methods to retrieve fields, constructors, methods and super
+ * interfaces of the type. Only types that are visible (public) can be described by this class.
+ * @author mzechner */
+public class Type {
+ private static final Field[] EMPTY_FIELDS = new Field[0];
+ private static final Method[] EMPTY_METHODS = new Method[0];
+ private static final Constructor[] EMPTY_CONSTRUCTORS = new Constructor[0];
+
+ String name;
+ int id;
+ Class clazz;
+ Class superClass;
+ Set<Class> assignables = new HashSet<Class>();
+ boolean isAbstract;
+ boolean isInterface;
+ boolean isPrimitive;