Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2008 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;
import com.google.gwt.core.client.UnsafeNativeLong;
import com.google.gwt.core.client.impl.WeakMapping;
import com.google.gwt.core.ext.GeneratorContext;
import com.google.gwt.core.ext.GeneratorContextExt;
import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.core.ext.typeinfo.JArrayType;
import com.google.gwt.core.ext.typeinfo.JClassType;
import com.google.gwt.core.ext.typeinfo.JConstructor;
import com.google.gwt.core.ext.typeinfo.JEnumType;
import com.google.gwt.core.ext.typeinfo.JField;
import com.google.gwt.core.ext.typeinfo.JMethod;
import com.google.gwt.core.ext.typeinfo.JPrimitiveType;
import com.google.gwt.core.ext.typeinfo.JType;
import com.google.gwt.core.ext.typeinfo.JTypeParameter;
import com.google.gwt.core.ext.typeinfo.TypeOracle;
import com.google.gwt.user.client.rpc.SerializationException;
import com.google.gwt.user.client.rpc.SerializationStreamReader;
import com.google.gwt.user.client.rpc.SerializationStreamWriter;
import com.google.gwt.user.client.rpc.core.java.lang.Object_Array_CustomFieldSerializer;
import com.google.gwt.user.client.rpc.impl.ReflectionHelper;
import com.google.gwt.user.client.rpc.impl.TypeHandler;
import com.google.gwt.user.rebind.ClassSourceFileComposerFactory;
import com.google.gwt.user.rebind.SourceWriter;
import java.io.PrintWriter;
/**
* Creates a field serializer for a class that implements
* {@link com.google.gwt.user.client.rpc.IsSerializable IsSerializable} or
* {@link java.io.Serializable Serializable}. The field serializer is emitted
* into the same package as the class that it serializes.
*
* TODO(mmendez): Need to make the generated field serializers final
* TODO(mmendez): Would be nice to be able to have imports, rather than using
* fully qualified type names everywhere
*/
public class FieldSerializerCreator {
/* NB: FieldSerializerCreator generates two different sets of code for
* DevMode and ProdMode. In ProdMode, the generated code uses the JSNI
* violator pattern to access private class members. In DevMode, the generated
* code uses ReflectionHelper instead of JSNI to avoid the many JSNI
* boundary-crossings which are slow in DevMode.
*/
private static final String WEAK_MAPPING_CLASS_NAME = WeakMapping.class.getName();
private final GeneratorContextExt context;
private final JClassType customFieldSerializer;
private final boolean customFieldSerializerHasInstantiate;
private final String fieldSerializerName;
private final boolean isJRE;
private final boolean isProd;
private final String methodEnd;
private final String methodStart;
private final JClassType serializableClass;
private final JField[] serializableFields;
private SourceWriter sourceWriter;
private final SerializableTypeOracle typesSentFromBrowser;
private final SerializableTypeOracle typesSentToBrowser;
private final TypeOracle typeOracle;
/**
* Constructs a field serializer for the class.
*/
public FieldSerializerCreator(GeneratorContextExt context,
SerializableTypeOracle typesSentFromBrowser,
SerializableTypeOracle typesSentToBrowser, JClassType requestedClass,
JClassType customFieldSerializer) {
this.context = context;
this.isProd = context.isProdMode();
methodStart = isProd ? "/*-{" : "{";
methodEnd = isProd ? "}-*/;" : "}";
this.customFieldSerializer = customFieldSerializer;
assert (requestedClass != null);
assert (requestedClass.isClass() != null || requestedClass.isArray() != null);
this.typeOracle = context.getTypeOracle();
this.typesSentFromBrowser = typesSentFromBrowser;
this.typesSentToBrowser = typesSentToBrowser;
serializableClass = requestedClass;
serializableFields = SerializationUtils.getSerializableFields(typeOracle,
requestedClass);
this.fieldSerializerName = SerializationUtils.getStandardSerializerName(serializableClass);
this.isJRE = SerializableTypeOracleBuilder.isInStandardJavaPackage(serializableClass.getQualifiedSourceName());
this.customFieldSerializerHasInstantiate = (customFieldSerializer != null && CustomFieldSerializerValidator.hasInstantiationMethod(
customFieldSerializer, serializableClass));
}
public String realize(TreeLogger logger, GeneratorContext ctx) {
assert (ctx != null);
assert (typesSentFromBrowser.isSerializable(serializableClass) || typesSentToBrowser.isSerializable(serializableClass));
logger = logger.branch(TreeLogger.DEBUG,
"Generating a field serializer for type '"
+ serializableClass.getQualifiedSourceName() + "'", null);
sourceWriter = getSourceWriter(logger, ctx);
if (sourceWriter == null) {
return fieldSerializerName;
}
assert sourceWriter != null;
writeFieldAccessors();
writeDeserializeMethod();
maybeWriteInstatiateMethod();
writeSerializeMethod();
maybeWriteTypeHandlerImpl();
sourceWriter.commit(logger);
return fieldSerializerName;
}
private boolean classIsAccessible() {
JClassType testClass = serializableClass;
while (testClass != null) {
if (testClass.isPrivate() || (isJRE && !testClass.isPublic())) {
return false;
}
testClass = testClass.getEnclosingType();
}
return true;
}
private String createArrayInstantiationExpression(JArrayType array) {
StringBuilder sb = new StringBuilder();
sb.append("new ");
sb.append(array.getLeafType().getQualifiedSourceName());
sb.append("[size]");
for (int i = 0; i < array.getRank() - 1; ++i) {
sb.append("[]");
}
return sb.toString();
}
private boolean ctorIsAccessible() {
JConstructor ctor = serializableClass.findConstructor(new JType[0]);
if (ctor.isPrivate() || (isJRE && !ctor.isPublic())) {
return false;
}
return true;
}
/**
* Returns the depth of the given class in the class hierarchy
* (where the depth of java.lang.Object == 0).
*/
private int getDepth(JClassType clazz) {
int depth = 0;
while ((clazz = clazz.getSuperclass()) != null) {
depth++;
}
return depth;
}
private SourceWriter getSourceWriter(TreeLogger logger, GeneratorContext ctx) {
int packageNameEnd = fieldSerializerName.lastIndexOf('.');
String className;
String packageName;
if (packageNameEnd != -1) {
className = fieldSerializerName.substring(packageNameEnd + 1);
packageName = fieldSerializerName.substring(0, packageNameEnd);
} else {
className = fieldSerializerName;
packageName = "";
}
PrintWriter printWriter = ctx.tryCreate(logger, packageName, className);
if (printWriter == null) {
return null;
}
ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(
packageName, className);
composerFactory.addImport(SerializationException.class.getCanonicalName());
composerFactory.addImport(SerializationStreamReader.class.getCanonicalName());
composerFactory.addImport(SerializationStreamWriter.class.getCanonicalName());
composerFactory.addImport(ReflectionHelper.class.getCanonicalName());
composerFactory.addAnnotationDeclaration("@SuppressWarnings(\"deprecation\")");
if (needsTypeHandler()) {
composerFactory.addImplementedInterface(TypeHandler.class.getCanonicalName());
}
return composerFactory.createSourceWriter(ctx, printWriter);
}
private String getTypeSig(JMethod deserializationMethod) {
JTypeParameter[] typeParameters = deserializationMethod.getTypeParameters();
String typeSig = "";
if (typeParameters.length > 0) {
StringBuilder sb = new StringBuilder();
sb.append('<');
for (JTypeParameter typeParameter : typeParameters) {
sb.append(typeParameter.getFirstBound().getQualifiedSourceName());
sb.append(',');
}
sb.setCharAt(sb.length() - 1, '>');
typeSig = sb.toString();
}
return typeSig;
}
private void maybeSuppressLongWarnings(JType fieldType) {
if (fieldType == JPrimitiveType.LONG) {
/**
* Accessing long from JSNI causes a error, but field serializers need to
* be able to do just that in order to bypass java accessibility
* restrictions.
*/
sourceWriter.println("@" + UnsafeNativeLong.class.getName());
}
}
/**
* Writes an instantiate method. Examples:
*
*
Class
*
*
* public static com.google.gwt.sample.client.Student instantiate(
* SerializationStreamReader streamReader) throws SerializationException {
* return new com.google.gwt.sample.client.Student();
* }
*
*/
private void maybeWriteInstatiateMethod() {
if (serializableClass.isEnum() == null
&& (serializableClass.isAbstract() || !serializableClass.isDefaultInstantiable())) {
/*
* Field serializers are shared by all of the RemoteService proxies in a
* compilation. Therefore, we have to generate an instantiate method even
* if the type is not instantiable relative to the RemoteService which
* caused this field serializer to be created. If the type is not
* instantiable relative to any of the RemoteService proxies, dead code
* optimizations will cause the method to be removed from the compiled
* output.
*
* Enumerated types require an instantiate method even if they are
* abstract. You will have an abstract enum in cases where the enum type
* is sub-classed. Non-default instantiable classes cannot have
* instantiate methods.
*/
return;
}
if (customFieldSerializerHasInstantiate) {
// The custom field serializer already defined it.
return;
}
JArrayType isArray = serializableClass.isArray();
JEnumType isEnum = serializableClass.isEnum();
JClassType isClass = serializableClass.isClass();
boolean useViolator = false;
boolean isAccessible = true;
if (isEnum == null && isClass != null) {
isAccessible = classIsAccessible() && ctorIsAccessible();
useViolator = !isAccessible && isProd;
}
sourceWriter.print("public static" + (useViolator ? " native " : " "));
String qualifiedSourceName = serializableClass.getQualifiedSourceName();
sourceWriter.print(qualifiedSourceName);
sourceWriter.println(" instantiate(SerializationStreamReader streamReader) throws SerializationException "
+ (useViolator ? "/*-{" : "{"));
sourceWriter.indent();
if (isArray != null) {
sourceWriter.println("int size = streamReader.readInt();");
sourceWriter.println("return "
+ createArrayInstantiationExpression(isArray) + ";");
} else if (isEnum != null) {
sourceWriter.println("int ordinal = streamReader.readInt();");
sourceWriter.println(qualifiedSourceName + "[] values = "
+ qualifiedSourceName + ".values();");
sourceWriter.println("assert (ordinal >= 0 && ordinal < values.length);");
sourceWriter.println("return values[ordinal];");
} else if (!isAccessible) {
if (isProd) {
sourceWriter.println("return @" + qualifiedSourceName + "::new()();");
} else {
sourceWriter.println(
"return ReflectionHelper.newInstance(" + qualifiedSourceName
+ ".class);");
}
} else {
sourceWriter.println("return new " + qualifiedSourceName + "();");
}
sourceWriter.outdent();
sourceWriter.println(useViolator ? "}-*/;" : "}");
sourceWriter.println();
}
/**
* Implement {@link TypeHandler} for the class, used by Java.
*
*