Revision: 10645
Author:   rj...@google.com
Date:     Tue Sep 13 11:23:09 2011
Log: Makes the @GwtTransient mechanism work for any annotation with that
simple name.

Review at http://gwt-code-reviews.appspot.com/1544803

Review by: cromwell...@google.com
http://code.google.com/p/google-web-toolkit/source/detail?r=10645

Added:
 /trunk/user/test/com/google/gwt/user/rebind/rpc/GwtTransient.java
Modified:
 /trunk/user/src/com/google/gwt/user/client/rpc/GwtTransient.java
/trunk/user/src/com/google/gwt/user/rebind/rpc/SerializableTypeOracleBuilder.java /trunk/user/src/com/google/gwt/user/server/rpc/impl/SerializabilityUtil.java /trunk/user/test/com/google/gwt/user/rebind/rpc/SerializableTypeOracleBuilderTest.java

=======================================
--- /dev/null
+++ /trunk/user/test/com/google/gwt/user/rebind/rpc/GwtTransient.java Tue Sep 13 11:23:09 2011
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2011 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 com.google.gwt.user.rebind.rpc;
+
+/**
+ * Used to test that any annotation named @GwtTransient gets the job done. Need + * to define both a real class (TypeOracleMediator uses reflection) and a Java
+ * source resource (for TypeOracle itself).
+ */
+public @interface GwtTransient {
+}
=======================================
--- /trunk/user/src/com/google/gwt/user/client/rpc/GwtTransient.java Mon May 18 11:47:32 2009 +++ /trunk/user/src/com/google/gwt/user/client/rpc/GwtTransient.java Tue Sep 13 11:23:09 2011
@@ -27,6 +27,10 @@
  * <code>transient</code> keyword should be used in preference to this
* annotation. However, for types used with multiple serialization systems, it
  * can be useful.
+ * <p>
+ * Note that GWT will actually accept any annotation named GwtTransient for this + * purpose. This is done to allow libraries to support GWT serialization without
+ * creating a direct dependency on the GWT distribution.
  */
 @Documented
 @Retention(RetentionPolicy.RUNTIME)
=======================================
--- /trunk/user/src/com/google/gwt/user/rebind/rpc/SerializableTypeOracleBuilder.java Thu Aug 4 06:02:26 2011 +++ /trunk/user/src/com/google/gwt/user/rebind/rpc/SerializableTypeOracleBuilder.java Tue Sep 13 11:23:09 2011
@@ -451,6 +451,15 @@

     return (JRealClassType) type;
   }
+
+  static boolean hasGwtTransientAnnotation(JField field) {
+    for (Annotation a : field.getAnnotations()) {
+ if (a.annotationType().getSimpleName().equals(GwtTransient.class.getSimpleName())) {
+        return true;
+      }
+    }
+    return false;
+  }

   /**
    * @param type the type to query
@@ -607,7 +616,7 @@
       return false;
     }

-    if (field.isAnnotationPresent(GwtTransient.class)) {
+    if (hasGwtTransientAnnotation(field)) {
       return false;
     }

=======================================
--- /trunk/user/src/com/google/gwt/user/server/rpc/impl/SerializabilityUtil.java Fri Aug 26 07:32:45 2011 +++ /trunk/user/src/com/google/gwt/user/server/rpc/impl/SerializabilityUtil.java Tue Sep 13 11:23:09 2011
@@ -25,6 +25,7 @@
 import com.google.gwt.user.server.rpc.ServerCustomFieldSerializer;

 import java.io.UnsupportedEncodingException;
+import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
 import java.lang.reflect.GenericArrayType;
 import java.lang.reflect.Modifier;
@@ -462,6 +463,18 @@
public static void resolveTypes(Type methodType, DequeMap<TypeVariable<?>, Type> resolvedTypes) { SerializabilityUtil.resolveTypesWorker(methodType, resolvedTypes, true);
   }
+
+  /**
+   * Returns true if this field has an annotation named "GwtTransient".
+   */
+  static boolean hasGwtTransientAnnotation(Field field) {
+    for (Annotation a : field.getAnnotations()) {
+ if (a.annotationType().getSimpleName().equals(GwtTransient.class.getSimpleName())) {
+        return true;
+      }
+    }
+    return false;
+  }

   static boolean isNotStaticTransientOrFinal(Field field) {
     /*
@@ -470,7 +483,7 @@
      */
     int fieldModifiers = field.getModifiers();
return !Modifier.isStatic(fieldModifiers) && !Modifier.isTransient(fieldModifiers) - && !field.isAnnotationPresent(GwtTransient.class) && !Modifier.isFinal(fieldModifiers); + && !hasGwtTransientAnnotation(field) && !Modifier.isFinal(fieldModifiers);
   }

   /**
=======================================
--- /trunk/user/test/com/google/gwt/user/rebind/rpc/SerializableTypeOracleBuilderTest.java Thu Aug 4 06:02:26 2011 +++ /trunk/user/test/com/google/gwt/user/rebind/rpc/SerializableTypeOracleBuilderTest.java Tue Sep 13 11:23:09 2011
@@ -48,7 +48,6 @@

 import junit.framework.TestCase;

-// import java.io.OutputStream;
 import java.io.PrintWriter;
 import java.util.Arrays;
 import java.util.Comparator;
@@ -63,6 +62,7 @@
  * Used to test the {@link SerializableTypeOracleBuilder}.
  */
 public class SerializableTypeOracleBuilderTest extends TestCase {
+
   /**
    * Just enough of a {@code GeneratorContext} to satisfy
    * {@code SerializableTypeOracleBuilder}.
@@ -127,6 +127,19 @@
private static final int EXPOSURE_NONE = TypeParameterExposureComputer.EXPOSURE_NONE;

   private static TypeOracle sTypeOracle;
+
+  /**
+   * Mocks the source of the {@link GwtTransient} type in this package.
+   */
+  private static void addCustomGwtTransient(Set<Resource> resources) {
+    StringBuffer code = new StringBuffer();
+    code.append("package com.google.gwt.user.rebind.rpc;\n");
+    code.append("import java.lang.annotation.Retention;");
+    code.append("import java.lang.annotation.RetentionPolicy;");
+    code.append("@Retention(RetentionPolicy.RUNTIME)");
+    code.append("public @interface GwtTransient { }\n");
+ resources.add(new StaticJavaResource("com.google.gwt.user.rebind.rpc.GwtTransient", code));
+  }

   private static void addGwtTransient(Set<Resource> resources) {
     StringBuffer code = new StringBuffer();
@@ -2077,6 +2090,7 @@
public void testTransient() throws UnableToCompleteException, NotFoundException {
     Set<Resource> resources = new HashSet<Resource>();
     addStandardClasses(resources);
+    addCustomGwtTransient(resources);

     {
       StringBuilder code = new StringBuilder();
@@ -2085,6 +2099,7 @@
       code.append("public class A implements Serializable {\n");
       code.append("  transient ServerOnly1 serverOnly1;\n");
       code.append("  @GwtTransient ServerOnly2 serverOnly2;\n");
+ code.append(" @com.google.gwt.user.rebind.rpc.GwtTransient ServerOnly3 serverOnly3;\n");
       code.append("}\n");
       resources.add(new StaticJavaResource("A", code));
     }
@@ -2104,6 +2119,14 @@
       code.append("}\n");
       resources.add(new StaticJavaResource("ServerOnly2", code));
     }
+
+    {
+      StringBuilder code = new StringBuilder();
+      code.append("import java.io.Serializable;\n");
+      code.append("class ServerOnly3 implements Serializable {\n");
+      code.append("}\n");
+      resources.add(new StaticJavaResource("ServerOnly3", code));
+    }

     TreeLogger logger = createLogger();
TypeOracle to = TypeOracleTestingUtils.buildTypeOracle(logger, resources);
@@ -2111,6 +2134,7 @@
     JClassType a = to.getType("A");
     JClassType serverOnly1 = to.getType("ServerOnly1");
     JClassType serverOnly2 = to.getType("ServerOnly2");
+    JClassType serverOnly3 = to.getType("ServerOnly3");

SerializableTypeOracleBuilder sob = createSerializableTypeOracleBuilder(logger, to);
     sob.addRootType(logger, a);
@@ -2120,6 +2144,7 @@
     assertInstantiable(so, a);
     assertNotFieldSerializable(so, serverOnly1);
     assertNotFieldSerializable(so, serverOnly2);
+    assertNotFieldSerializable(so, serverOnly3);
   }

   /**

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to