Example usage for com.google.gwt.core.ext.typeinfo JField isTransient

List of usage examples for com.google.gwt.core.ext.typeinfo JField isTransient

Introduction

In this page you can find the example usage for com.google.gwt.core.ext.typeinfo JField isTransient.

Prototype

boolean isTransient();

Source Link

Usage

From source file:ch.unifr.pai.twice.comm.serverPush.rebind.RemoteEventSerializerGenerator.java

License:Apache License

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    // Build a new class, that implements a "paintScreen" method
    JClassType classType;//www.j  ava 2 s .  c o  m

    try {
        classType = context.getTypeOracle().getType(typeName);
        JClassType superClass = classType.getSuperclass();
        JClassType[] generics = superClass.isParameterized().getTypeArgs();
        JClassType eventHandlerClass = generics[0];

        // Here you would retrieve the metadata based on typeName for this
        // Screen
        SourceWriter src = getSourceWriter(classType, context, logger);
        if (src != null) {
            src.println("@Override");
            src.println("public " + String.class.getName() + " getEventType(){");
            src.println("return " + classType.getQualifiedSourceName() + ".class.getName();");
            src.println("}");

            if (superClass.getQualifiedSourceName().equals(RemoteEventWrapper.class.getName())
                    || superClass.getQualifiedSourceName().equals(UndoableRemoteEventWrapper.class.getName())) {
                JClassType eventClass = generics[1];
                src.println("@Override");
                src.println("public void wrap(" + eventClass.getQualifiedSourceName() + " event){");
                for (JMethod method : classType.getMethods()) {
                    String realMethodName = method.getName().replaceAll("_", "().");
                    src.println("setProperty(\"" + method.getName() + "\", " + String.class.getName()
                            + ".valueOf(event." + realMethodName + "()));");
                }
                src.println("}");

                for (JMethod method : classType.getMethods()) {
                    if (method.isAbstract()) {
                        src.println();
                        src.println("@Override");
                        src.println("public " + String.class.getName() + " " + method.getName() + "(){");
                        src.println(
                                JSONValue.class.getName() + " value = json.get(\"" + method.getName() + "\");");
                        src.println(
                                "return value!=null && value.isString()!=null ? value.isString().stringValue() : null;");
                        src.println("}");
                    }
                }

                src.println();
            }

            src.println("@Override");
            src.println("public " + GwtEvent.class.getName() + "." + Type.class.getSimpleName() + "<"
                    + eventHandlerClass.getQualifiedSourceName() + "> getAssociatedType() {");
            src.println("\treturn " + classType.getQualifiedSourceName() + ".TYPE;");
            src.println("}");

            src.println();

            src.println("@Override");
            src.println(
                    "protected void dispatch(" + eventHandlerClass.getQualifiedSourceName() + " handler) {");
            // for (JMethod m : eventHandlerClass.getMethods()) {
            // if(!m.getName().equals("undo")){
            boolean undoable = classType
                    .isAssignableTo(context.getTypeOracle().getType(UndoableRemoteEvent.class.getName()));
            if (undoable) {
                src.println("if(isUndo())");
                src.println("handler.undo(this);");
                src.println("else {");
                src.println("handler.saveState(this);");
            }
            src.println("\t handler.onEvent(this);");
            if (undoable) {
                src.println("}");
            }

            // }
            // }
            src.println("}");
            src.println();
            src.println("@Override");
            src.println("public String serialize(" + TWICESecurityManager.class.getName() + " security) throws "
                    + MessagingException.class.getName() + "{");
            for (JField field : classType.getFields()) {
                if (!field.isStatic() && !field.isTransient()) {
                    src.println("if(" + field.getName() + "!=null){");
                    src.println("setProperty(\"" + field.getName() + "\", String.valueOf(" + field.getName()
                            + "));}");

                }
            }
            src.println("return super.serialize(security);");
            src.println("}");
            src.println();
            src.println("@Override");
            src.println(
                    "public " + RemoteEvent.class.getName() + "<" + eventHandlerClass.getQualifiedSourceName()
                            + "> deserialize(String string, " + TWICESecurityManager.class.getName()
                            + " security) throws " + MessagingException.class.getName() + "{");
            src.println(RemoteEvent.class.getName() + " result = super.deserialize(string, security);");
            for (JField field : classType.getFields()) {
                if (!field.isStatic()) {
                    if (String.class.getName().equals(field.getType().getQualifiedSourceName()))
                        src.println(field.getName() + " = getProperty(\"" + field.getName() + "\");");
                    else {
                        src.println("String " + field.getName() + "Tmp = getProperty(\"" + field.getName()
                                + "\");");
                        src.println(field.getName() + " = " + field.getName() + "Tmp!=null ? "
                                + field.getType().getQualifiedSourceName() + ".valueOf(" + field.getName()
                                + "Tmp) : null;");
                    }
                }
            }
            src.println("return result;");
            src.println("}");
            src.commit(logger);

        }
        return typeName + "Impl";

    } catch (NotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.artemis.gwtref.gen.ReflectionCacheSourceCreator.java

License:Apache License

private String createTypeGenerator(JType t) {
    buffer.setLength(0);//from  www  . jav a2s . com
    String varName = "t";
    if (t instanceof JPrimitiveType)
        varName = "p";
    int id = nextId();
    typeNames2typeIds.put(t.getErasedType().getQualifiedSourceName(), id);
    pb("Type " + varName + " = new Type();");
    pb(varName + ".name = \"" + t.getErasedType().getQualifiedSourceName() + "\";");
    pb(varName + ".id = " + id + ";");
    pb(varName + ".clazz = " + t.getErasedType().getQualifiedSourceName() + ".class;");
    if (t instanceof JClassType) {
        JClassType c = (JClassType) t;
        if (isVisible(c.getSuperclass()))
            pb(varName + ".superClass = " + c.getSuperclass().getErasedType().getQualifiedSourceName()
                    + ".class;");
        if (c.getFlattenedSupertypeHierarchy() != null) {
            pb("Set<Class> " + varName + "Assignables = new HashSet<Class>();");
            for (JType i : c.getFlattenedSupertypeHierarchy()) {
                if (!isVisible(i))
                    continue;
                pb(varName + "Assignables.add(" + i.getErasedType().getQualifiedSourceName() + ".class);");
            }
            pb(varName + ".assignables = " + varName + "Assignables;");
        }
        if (c.isInterface() != null)
            pb(varName + ".isInterface = true;");
        if (c.isEnum() != null)
            pb(varName + ".isEnum = true;");
        if (c.isArray() != null)
            pb(varName + ".isArray = true;");
        if (c.isMemberType())
            pb(varName + ".isMemberClass = true;");
        pb(varName + ".isStatic = " + c.isStatic() + ";");
        pb(varName + ".isAbstract = " + c.isAbstract() + ";");

        if (c.getFields() != null) {
            pb(varName + ".fields = new Field[] {");
            for (JField f : c.getFields()) {
                String enclosingType = getType(c);
                String fieldType = getType(f.getType());
                int setter = nextId();
                int getter = nextId();
                String elementType = getElementTypes(f);
                String annotations = getAnnotations(f.getDeclaredAnnotations());

                pb("new Field(\"" + f.getName() + "\", " + enclosingType + ", " + fieldType + ", " + f.isFinal()
                        + ", " + f.isDefaultAccess() + ", " + f.isPrivate() + ", " + f.isProtected() + ", "
                        + f.isPublic() + ", " + f.isStatic() + ", " + f.isTransient() + ", " + f.isVolatile()
                        + ", " + getter + ", " + setter + ", " + elementType + ", " + annotations + "), ");

                SetterGetterStub stub = new SetterGetterStub();
                stub.name = f.getName();
                stub.enclosingType = enclosingType;
                stub.type = fieldType;
                stub.isStatic = f.isStatic();
                stub.isFinal = f.isFinal();
                if (enclosingType != null && fieldType != null) {
                    stub.getter = getter;
                    stub.setter = setter;
                }
                setterGetterStubs.add(stub);
            }
            pb("};");
        }

        printMethods(c, varName, "Method", c.getMethods());
        if (c.isPublic() && !c.isAbstract() && (c.getEnclosingType() == null || c.isStatic())) {
            printMethods(c, varName, "Constructor", c.getConstructors());
        } else {
            logger.log(Type.INFO, c.getName() + " can't be instantiated. Constructors not generated");
        }

        if (c.isArray() != null) {
            pb(varName + ".componentType = " + getType(c.isArray().getComponentType()) + ";");
        }
        if (c.isEnum() != null) {
            JEnumConstant[] enumConstants = c.isEnum().getEnumConstants();
            if (enumConstants != null) {
                pb(varName + ".enumConstants = new Object[" + enumConstants.length + "];");
                for (int i = 0; i < enumConstants.length; i++) {
                    pb(varName + ".enumConstants[" + i + "] = " + c.getErasedType().getQualifiedSourceName()
                            + "." + enumConstants[i].getName() + ";");
                }
            }
        }

        Annotation[] annotations = c.getDeclaredAnnotations();
        if (annotations != null && annotations.length > 0) {
            pb(varName + ".annotations = " + getAnnotations(annotations) + ";");
        }
    } else if (t.isAnnotation() != null) {
        pb(varName + ".isAnnotation = true;");
    } else {
        pb(varName + ".isPrimitive = true;");
    }

    pb("types.put(\"" + t.getErasedType().getQualifiedSourceName() + "\", " + varName + ");");
    return buffer.toString();
}

From source file:com.badlogic.gwtref.gen.ReflectionCacheSourceCreator.java

License:Apache License

private String createTypeGenerator(JType t) {
    buffer.setLength(0);//from   w ww. ja va 2 s .co  m
    int id = nextTypeId++;
    typeNames2typeIds.put(t.getErasedType().getQualifiedSourceName(), id);
    JClassType c = t.isClass();

    String name = t.getErasedType().getQualifiedSourceName();
    String superClass = null;
    if (c != null && (isVisible(c.getSuperclass())))
        superClass = c.getSuperclass().getErasedType().getQualifiedSourceName() + ".class";
    String assignables = null;
    String interfaces = null;

    if (c != null && c.getFlattenedSupertypeHierarchy() != null) {
        assignables = "new HashSet<Class>(Arrays.asList(";
        boolean used = false;
        for (JType i : c.getFlattenedSupertypeHierarchy()) {
            if (!isVisible(i) || i.equals(t)
                    || "java.lang.Object".equals(i.getErasedType().getQualifiedSourceName()))
                continue;
            if (used)
                assignables += ", ";
            assignables += i.getErasedType().getQualifiedSourceName() + ".class";
            used = true;
        }
        if (used)
            assignables += "))";
        else
            assignables = null;
    }

    if (c == null) {
        // if it's not a class, it may be an interface instead
        c = t.isInterface();
    }

    if (c != null && c.getImplementedInterfaces() != null) {
        interfaces = "new HashSet<Class>(Arrays.asList(";
        boolean used = false;
        for (JType i : c.getImplementedInterfaces()) {
            if (!isVisible(i) || i.equals(t))
                continue;
            if (used)
                interfaces += ", ";
            interfaces += i.getErasedType().getQualifiedSourceName() + ".class";
            used = true;
        }
        if (used)
            interfaces += "))";
        else
            interfaces = null;
    }

    String varName = "c" + id;
    pb("private static Type " + varName + ";");
    pb("private static Type " + varName + "() {");
    pb("if(" + varName + "!=null) return " + varName + ";");
    pb(varName + " = new Type(\"" + name + "\", " + id + ", " + name + ".class, " + superClass + ", "
            + assignables + ", " + interfaces + ");");

    if (c != null) {
        if (c.isEnum() != null)
            pb(varName + ".isEnum = true;");
        if (c.isArray() != null)
            pb(varName + ".isArray = true;");
        if (c.isMemberType())
            pb(varName + ".isMemberClass = true;");
        if (c.isInterface() != null) {
            pb(varName + ".isInterface = true;");
        } else {
            pb(varName + ".isStatic = " + c.isStatic() + ";");
            pb(varName + ".isAbstract = " + c.isAbstract() + ";");
        }

        if (c.getFields() != null && c.getFields().length > 0) {
            pb(varName + ".fields = new Field[] {");
            for (JField f : c.getFields()) {
                String enclosingType = getType(c);
                String fieldType = getType(f.getType());
                int setterGetter = nextSetterGetterId++;
                String elementType = getElementTypes(f);
                String annotations = getAnnotations(f.getDeclaredAnnotations());

                pb("    new Field(\"" + f.getName() + "\", " + enclosingType + ", " + fieldType + ", "
                        + f.isFinal() + ", " + f.isDefaultAccess() + ", " + f.isPrivate() + ", "
                        + f.isProtected() + ", " + f.isPublic() + ", " + f.isStatic() + ", " + f.isTransient()
                        + ", " + f.isVolatile() + ", " + setterGetter + ", " + setterGetter + ", " + elementType
                        + ", " + annotations + "), ");

                SetterGetterStub stub = new SetterGetterStub();
                stub.name = f.getName();
                stub.enclosingType = enclosingType;
                stub.type = fieldType;
                stub.isStatic = f.isStatic();
                stub.isFinal = f.isFinal();
                if (enclosingType != null && fieldType != null) {
                    stub.getter = setterGetter;
                    stub.setter = setterGetter;
                }
                setterGetterStubs.add(stub);
            }
            pb("};");
        }

        createTypeInvokables(c, varName, "Method", c.getMethods());
        if (c.isPublic() && !c.isAbstract() && (c.getEnclosingType() == null || c.isStatic())) {
            createTypeInvokables(c, varName, "Constructor", c.getConstructors());
        } else {
            logger.log(Type.INFO, c.getName() + " can't be instantiated. Constructors not generated");
        }

        if (c.isArray() != null) {
            pb(varName + ".componentType = " + getType(c.isArray().getComponentType()) + ";");
        }
        if (c.isEnum() != null) {
            JEnumConstant[] enumConstants = c.isEnum().getEnumConstants();
            if (enumConstants != null) {
                pb(varName + ".enumConstants = new Object[" + enumConstants.length + "];");
                for (int i = 0; i < enumConstants.length; i++) {
                    pb(varName + ".enumConstants[" + i + "] = " + c.getErasedType().getQualifiedSourceName()
                            + "." + enumConstants[i].getName() + ";");
                }
            }
        }

        Annotation[] annotations = c.getDeclaredAnnotations();
        if (annotations != null && annotations.length > 0) {
            pb(varName + ".annotations = " + getAnnotations(annotations) + ";");
        }
    } else if (t.isAnnotation() != null) {
        pb(varName + ".isAnnotation = true;");
    } else {
        pb(varName + ".isPrimitive = true;");
    }

    pb("return " + varName + ";");
    pb("}");
    return buffer.toString();
}

From source file:com.didactilab.gwt.phprpc.rebind.phpgen.PhpClass.java

License:Apache License

@Override
protected void getContents(TreeLogger logger, StringBuffer buffer) throws UnableToCompleteException {
    JClassType type = getJavaType();//from   www .ja  v a  2 s  . c om
    buffer.append("/**\n");
    buffer.append(" * @gwtname ").append(type.getQualifiedBinaryName()).append("\n");
    if (type.getEnclosingType() != null) {
        buffer.append(" * @enclosing ").append(type.getEnclosingType().getQualifiedBinaryName()).append("\n");
    }
    buffer.append(" */\n");
    buffer.append("class ").append(PhpTools.typeToString(type, true));
    JClassType superClass = type.getSuperclass();
    if ((superClass != null) && (!superClass.getQualifiedBinaryName().equals("java.lang.Object"))) {
        buffer.append(" extends ").append(PhpTools.typeToString(superClass, true));
    }
    buffer.append(" implements IsSerializable {\n");
    if (serializable) {
        for (JField field : type.getFields()) {
            if (field.isStatic())
                continue;
            if (field.isTransient())
                continue;
            buffer.append("\t/** @var ").append(PhpTools.typeToString(field.getType(), false)).append(" */\n");
            buffer.append("\tpublic $").append(field.getName()).append(";\n\n");
        }
    }
    buffer.append("}\n");
}

From source file:com.guit.rebind.jsorm.JsonSerializerUtil.java

License:Apache License

private static void printJsniGettersAndSetters(SourceWriter writer, JClassType pojoType) {

    for (JField f : pojoType.getFields()) {
        // Non static, final or transient
        if (f.isStatic() || f.isFinal() || f.isTransient()) {
            continue;
        }/*from   w  w  w .j a  va  2  s.  c o m*/

        String fieldName = f.getName();
        String getterName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
        JType fieldType = f.getType();

        // Print getters and setters
        String fieldTypeQualifiedType = fieldType.getQualifiedSourceName();
        String pojoQualifiedName = pojoType.getQualifiedSourceName();
        writer.println("private static native " + fieldTypeQualifiedType + " get" + getterName + "("
                + pojoQualifiedName + " instance) /*-{\n" + "    return instance.@" + pojoQualifiedName + "::"
                + fieldName + ";\n" + "  }-*/;\n" + "  \n" + "  private static native void  set" + getterName
                + "(" + pojoQualifiedName + " instance, " + fieldTypeQualifiedType + " value) /*-{\n"
                + "    instance.@" + pojoQualifiedName + "::" + fieldName + " = value;\n" + "  }-*/;");
    }

    JClassType superclass = pojoType.getSuperclass();
    if (superclass != null && !superclass.getQualifiedSourceName().equals(Object.class.getCanonicalName())) {
        printJsniGettersAndSetters(writer, superclass);
    }
}

From source file:com.guit.rebind.jsorm.JsonSerializerUtil.java

License:Apache License

private static void getFields(List<JField> fields, JClassType pojoType) {
    for (JField f : pojoType.getFields()) {
        // Non static, final or transient
        if (f.isStatic() || f.isFinal() || f.isTransient()) {
            continue;
        }/*w w  w .ja v  a2s  .c  o m*/

        fields.add(f);
    }

    JClassType superclass = pojoType.getSuperclass();
    if (superclass != null && !superclass.getQualifiedSourceName().equals(Object.class.getCanonicalName())) {
        getFields(fields, superclass);
    }
}

From source file:com.hiramchirino.restygwt.rebind.JsonEncoderDecoderClassCreator.java

License:Apache License

public void generate() throws UnableToCompleteException {

    locator = new JsonEncoderDecoderInstanceLocator(context, logger);

    JClassType soruceClazz = source.isClass();
    if (soruceClazz == null) {
        error("Type is not a class");
    }/*w  ww.j  a  v a 2  s  .  c o  m*/
    if (!soruceClazz.isDefaultInstantiable()) {
        error("No default constuctor");
    }

    Json jsonAnnotation = source.getAnnotation(Json.class);
    final Style classStyle = jsonAnnotation != null ? jsonAnnotation.style() : Style.DEFAULT;

    p();
    p("public static final " + shortName + " INSTANCE = new " + shortName + "();");
    p();

    p("public " + JSON_VALUE_CLASS + " encode(" + source.getParameterizedQualifiedSourceName() + " value) {")
            .i(1);
    {
        p(JSON_OBJECT_CLASS + " rc = new " + JSON_OBJECT_CLASS + "();");

        for (final JField field : getFields(source)) {

            final String getterName = getGetterName(field);

            // If can ignore some fields right off the back..
            if (getterName == null && (field.isStatic() || field.isFinal() || field.isTransient())) {
                continue;
            }

            branch("Processing field: " + field.getName(), new Branch<Void>() {
                public Void execute() throws UnableToCompleteException {
                    // TODO: try to get the field with a setter or JSNI
                    if (getterName != null || field.isDefaultAccess() || field.isProtected()
                            || field.isPublic()) {

                        String name = field.getName();

                        String fieldExpr = "value." + name;
                        if (getterName != null) {
                            fieldExpr = "value." + getterName + "()";
                        }

                        Json jsonAnnotation = field.getAnnotation(Json.class);
                        Style style = jsonAnnotation != null ? jsonAnnotation.style() : classStyle;

                        String expression = locator.encodeExpression(field.getType(), fieldExpr, style);

                        p("{").i(1);
                        {
                            p(JSON_VALUE_CLASS + " v=" + expression + ";");
                            p("if( v!=null ) {").i(1);
                            {
                                if (field.isAnnotationPresent(ExcludeNull.class))
                                    p("if (v != " + JSONNull.class.getCanonicalName() + ".getInstance())");
                                p("rc.put(" + wrap(name) + ", v);");
                            }
                            i(-1).p("}");
                        }
                        i(-1).p("}");

                    } else {
                        error("field must not be private: " + field.getEnclosingType().getQualifiedSourceName()
                                + "." + field.getName());
                    }
                    return null;
                }
            });

        }

        p("return rc;");
    }
    i(-1).p("}");
    p();
    p("public " + source.getName() + " decode(" + JSON_VALUE_CLASS + " value) {").i(1);
    {
        p(JSON_OBJECT_CLASS + " object = toObject(value);");
        p("" + source.getParameterizedQualifiedSourceName() + " rc = new "
                + source.getParameterizedQualifiedSourceName() + "();");
        for (final JField field : getFields(source)) {

            final String setterName = getSetterName(field);

            // If can ignore some fields right off the back..
            if (setterName == null && (field.isStatic() || field.isFinal() || field.isTransient())) {
                continue;
            }

            branch("Processing field: " + field.getName(), new Branch<Void>() {
                public Void execute() throws UnableToCompleteException {

                    // TODO: try to set the field with a setter or JSNI
                    if (setterName != null || field.isDefaultAccess() || field.isProtected()
                            || field.isPublic()) {

                        Json jsonAnnotation = field.getAnnotation(Json.class);
                        Style style = jsonAnnotation != null ? jsonAnnotation.style() : classStyle;

                        String name = field.getName();
                        String expression = locator.decodeExpression(field.getType(),
                                "object.get(" + wrap(name) + ")", style);

                        if (setterName != null) {
                            p("rc." + setterName + "(" + expression + ");");
                        } else {
                            p("rc." + name + "=" + expression + ";");
                        }

                    } else {
                        error("field must not be private.");
                    }
                    return null;
                }
            });
        }

        p("return rc;");
    }
    i(-1).p("}");
    p();
}

From source file:com.kk_electronic.gwt.rebind.JsonEncoderGenerator.java

License:Open Source License

private void processFields(JClassType jct) {
    JField[] fields = jct.getFields();//from www  . ja  va 2  s.  c  o m
    for (JField jField : fields) {
        JClassType jClass = jField.getType().isClass();
        processGenerics(jField.getType());
        if (jClass != null && !jField.isTransient()) {
            processClass(jClass);
        }
    }
}

From source file:com.totsp.gwt.freezedry.rebind.SerializableTypeOracleBuilder.java

License:Apache License

private void checkFields(TreeLogger logger, JClassType classOrInterface) {
    TreeLogger localLogger = logger;//from w  ww  .  j a  v  a2 s  .c o  m
    JField[] fields = classOrInterface.getFields();
    if (fields.length > 0) {
        localLogger = localLogger.branch(TreeLogger.DEBUG, "Analyzing Fields:", null);

        contexts.push(classOrInterface);

        for (int i = 0; i < fields.length; ++i) {
            JField field = fields[i];

            if (field.isStatic() || field.isTransient()) {
                continue;
            }

            if (field.isFinal()) {
                if (!suppressNonStaticFinalFieldWarnings) {
                    localLogger.branch(TreeLogger.WARN,
                            "Field '" + field.toString() + "' will not be serialized because it is final",
                            null);
                }
                continue;
            }

            TreeLogger fieldLogger = localLogger.branch(TreeLogger.DEBUG, field.toString(), null);
            JType fieldType = field.getType();
            checkForUnparameterizedType(fieldLogger, fieldType);
            checkType(fieldLogger, fieldType, true);
        }

        contexts.pop();

    } else {
        localLogger.branch(TreeLogger.DEBUG, "No fields to analyze", null);
    }
}

From source file:com.totsp.gwt.freezedry.rebind.SerializableTypeOracleImpl.java

License:Apache License

/**
 * Returns the fields which qualify for serialization.
 *///from   w  w  w. j  a  v  a 2 s.co  m
public JField[] getSerializableFields(JClassType classType) {
    List fields = new ArrayList();
    JField[] declFields = classType.getFields();
    for (int iField = 0; iField < declFields.length; ++iField) {
        JField field = declFields[iField];
        // TODO(mmendez): this is shared with the serializable type oracle
        // builder, join with that
        if (field.isStatic() || field.isTransient() || field.isFinal()) {
            continue;
        }

        fields.add(field);
    }

    Collections.sort(fields, FIELD_COMPARATOR);
    return (JField[]) fields.toArray(new JField[fields.size()]);
}