Example usage for com.google.gwt.core.ext.typeinfo JClassType getErasedType

List of usage examples for com.google.gwt.core.ext.typeinfo JClassType getErasedType

Introduction

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

Prototype

JClassType getErasedType();

Source Link

Usage

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

License:Apache License

private void generateLookups() {
    p("Map<String, Type> types = new HashMap<String, Type>();");

    TypeOracle typeOracle = context.getTypeOracle();
    JPackage[] packages = typeOracle.getPackages();

    // gather all types from wanted packages
    for (JPackage p : packages) {
        for (JClassType t : p.getTypes()) {
            gatherTypes(t.getErasedType(), types);
        }// w  w w. j ava 2 s  .  co m
    }

    gatherTypes(typeOracle.findType("java.util.List").getErasedType(), types);
    gatherTypes(typeOracle.findType("java.util.ArrayList").getErasedType(), types);
    gatherTypes(typeOracle.findType("java.util.HashMap").getErasedType(), types);
    gatherTypes(typeOracle.findType("java.util.Map").getErasedType(), types);
    gatherTypes(typeOracle.findType("java.lang.String").getErasedType(), types);
    gatherTypes(typeOracle.findType("java.lang.Boolean").getErasedType(), types);
    gatherTypes(typeOracle.findType("java.lang.Byte").getErasedType(), types);
    gatherTypes(typeOracle.findType("java.lang.Long").getErasedType(), types);
    gatherTypes(typeOracle.findType("java.lang.Character").getErasedType(), types);
    gatherTypes(typeOracle.findType("java.lang.Short").getErasedType(), types);
    gatherTypes(typeOracle.findType("java.lang.Integer").getErasedType(), types);
    gatherTypes(typeOracle.findType("java.lang.Float").getErasedType(), types);
    gatherTypes(typeOracle.findType("java.lang.CharSequence").getErasedType(), types);
    gatherTypes(typeOracle.findType("java.lang.Double").getErasedType(), types);
    gatherTypes(typeOracle.findType("java.lang.Object").getErasedType(), types);

    // sort the types so the generated output will be stable between runs
    Collections.sort(types, new Comparator<JType>() {
        public int compare(JType o1, JType o2) {
            return o1.getQualifiedSourceName().compareTo(o2.getQualifiedSourceName());
        }
    });

    // generate Type lookup generator methods.
    int id = 0;
    for (JType t : types) {
        String typeGen = createTypeGenerator(t);
        p("private void c" + (id++) + "() {");
        p(typeGen);
        p("}\n");
    }

    // generate constructor that calls all the type generators
    // that populate the map.
    p("public " + simpleName + "() {");
    for (int i = 0; i < id; i++) {
        p("c" + i + "();");
    }
    p("}");

    // sort the stubs so the generated output will be stable between runs
    Collections.sort(setterGetterStubs, new Comparator<SetterGetterStub>() {
        @Override
        public int compare(SetterGetterStub o1, SetterGetterStub o2) {
            return new Integer(o1.setter).compareTo(o2.setter);
        }
    });

    // generate field setters/getters
    for (SetterGetterStub stub : setterGetterStubs) {
        String stubSource = generateSetterGetterStub(stub);
        if (stubSource.equals(""))
            stub.unused = true;
        p(stubSource);
    }

    // sort the stubs so the generated output will be stable between runs
    Collections.sort(methodStubs, new Comparator<MethodStub>() {
        @Override
        public int compare(MethodStub o1, MethodStub o2) {
            return new Integer(o1.methodId).compareTo(o2.methodId);
        }
    });

    // generate methods
    for (MethodStub stub : methodStubs) {
        String stubSource = generateMethodStub(stub);
        if (stubSource.equals(""))
            stub.unused = true;
        p(stubSource);
    }

    logger.log(Type.INFO, types.size() + " types reflected");
}

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

License:Apache License

private void gatherTypes(JType type, List<JType> types) {
    nesting++;/*from www.  j  a v a  2s.co  m*/
    // came here from a type that has no super class
    if (type == null) {
        nesting--;
        return;
    }
    // package info
    if (type.getQualifiedSourceName().contains("-")) {
        nesting--;
        return;
    }

    // not visible
    if (!isVisible(type)) {
        nesting--;
        return;
    }

    // filter reflection scope based on configuration in gwt xml module
    boolean keep = false;
    String name = type.getQualifiedSourceName();
    try {
        ConfigurationProperty prop;
        keep |= !name.contains(".");
        prop = context.getPropertyOracle().getConfigurationProperty("artemis.reflect.include");
        for (String s : prop.getValues())
            keep |= name.contains(s);
        prop = context.getPropertyOracle().getConfigurationProperty("artemis.reflect.exclude");
        for (String s : prop.getValues())
            keep &= !name.equals(s);
    } catch (BadPropertyValueException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (!keep) {
        nesting--;
        return;
    }

    // already visited this type
    if (types.contains(type.getErasedType())) {
        nesting--;
        return;
    }
    types.add(type.getErasedType());
    out(type.getErasedType().getQualifiedSourceName(), nesting);

    if (type instanceof JPrimitiveType) {
        // nothing to do for a primitive type
        nesting--;
        return;
    } else {
        // gather fields
        JClassType c = (JClassType) type;
        JField[] fields = c.getFields();
        if (fields != null) {
            for (JField field : fields) {
                gatherTypes(field.getType().getErasedType(), types);
            }
        }

        // gather super types & interfaces
        gatherTypes(c.getSuperclass(), types);
        JClassType[] interfaces = c.getImplementedInterfaces();
        if (interfaces != null) {
            for (JClassType i : interfaces) {
                gatherTypes(i.getErasedType(), types);
            }
        }

        // gather method parameter & return types
        JMethod[] methods = c.getMethods();
        if (methods != null) {
            for (JMethod m : methods) {
                gatherTypes(m.getReturnType().getErasedType(), types);
                if (m.getParameterTypes() != null) {
                    for (JType p : m.getParameterTypes()) {
                        gatherTypes(p.getErasedType(), types);
                    }
                }
            }
        }

        // gather inner classes
        JClassType[] inner = c.getNestedTypes();
        if (inner != null) {
            for (JClassType i : inner) {
                gatherTypes(i.getErasedType(), types);
            }
        }
    }
    nesting--;
}

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

License:Apache License

private String createTypeGenerator(JType t) {
    buffer.setLength(0);/*from   www  .ja v  a  2 s .  co  m*/
    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 void generateLookups() {
    TypeOracle typeOracle = context.getTypeOracle();
    JPackage[] packages = typeOracle.getPackages();

    // gather all types from wanted packages
    for (JPackage p : packages) {
        for (JClassType t : p.getTypes()) {
            gatherTypes(t.getErasedType(), types);
        }//from w  w w . java  2 s . c o m
    }

    // gather all types from explicitly requested packages
    try {
        ConfigurationProperty prop = context.getPropertyOracle()
                .getConfigurationProperty("gdx.reflect.include");
        for (String s : prop.getValues()) {
            JClassType type = typeOracle.findType(s);
            if (type != null)
                gatherTypes(type.getErasedType(), types);
        }
    } catch (BadPropertyValueException e) {
    }

    gatherTypes(typeOracle.findType("java.util.List").getErasedType(), types);
    gatherTypes(typeOracle.findType("java.util.ArrayList").getErasedType(), types);
    gatherTypes(typeOracle.findType("java.util.HashMap").getErasedType(), types);
    gatherTypes(typeOracle.findType("java.util.Map").getErasedType(), types);
    gatherTypes(typeOracle.findType("java.lang.String").getErasedType(), types);
    gatherTypes(typeOracle.findType("java.lang.Boolean").getErasedType(), types);
    gatherTypes(typeOracle.findType("java.lang.Byte").getErasedType(), types);
    gatherTypes(typeOracle.findType("java.lang.Long").getErasedType(), types);
    gatherTypes(typeOracle.findType("java.lang.Character").getErasedType(), types);
    gatherTypes(typeOracle.findType("java.lang.Short").getErasedType(), types);
    gatherTypes(typeOracle.findType("java.lang.Integer").getErasedType(), types);
    gatherTypes(typeOracle.findType("java.lang.Float").getErasedType(), types);
    gatherTypes(typeOracle.findType("java.lang.CharSequence").getErasedType(), types);
    gatherTypes(typeOracle.findType("java.lang.Double").getErasedType(), types);
    gatherTypes(typeOracle.findType("java.lang.Object").getErasedType(), types);

    // sort the types so the generated output will be stable between runs
    Collections.sort(types, new Comparator<JType>() {
        public int compare(JType o1, JType o2) {
            return o1.getQualifiedSourceName().compareTo(o2.getQualifiedSourceName());
        }
    });

    // generate Type lookup generator methods.
    for (JType t : types) {
        p(createTypeGenerator(t));
    }

    // generate reusable parameter objects
    parameterInitialization();

    // sort the stubs so the generated output will be stable between runs
    Collections.sort(setterGetterStubs, new Comparator<SetterGetterStub>() {
        @Override
        public int compare(SetterGetterStub o1, SetterGetterStub o2) {
            return new Integer(o1.setter).compareTo(o2.setter);
        }
    });

    // generate field setters/getters
    for (SetterGetterStub stub : setterGetterStubs) {
        String stubSource = generateSetterGetterStub(stub);
        if (stubSource.equals(""))
            stub.unused = true;
        p(stubSource);
    }

    // sort the stubs so the generated output will be stable between runs
    Collections.sort(methodStubs, new Comparator<MethodStub>() {
        @Override
        public int compare(MethodStub o1, MethodStub o2) {
            return new Integer(o1.methodId).compareTo(o2.methodId);
        }
    });

    // generate methods
    for (MethodStub stub : methodStubs) {
        String stubSource = generateMethodStub(stub);
        if (stubSource.equals(""))
            stub.unused = true;
        p(stubSource);
    }

    logger.log(Type.INFO, types.size() + " types reflected");
}

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

License:Apache License

private void gatherTypes(JType type, List<JType> types) {
    nesting++;/*from ww  w. j  a v a2  s. c om*/
    // came here from a type that has no super class
    if (type == null) {
        nesting--;
        return;
    }
    // package info
    if (type.getQualifiedSourceName().contains("-")) {
        nesting--;
        return;
    }

    // not visible
    if (!isVisible(type)) {
        nesting--;
        return;
    }

    // filter reflection scope based on configuration in gwt xml module
    boolean keep = false;
    String name = type.getQualifiedSourceName();
    try {
        ConfigurationProperty prop;
        keep |= !name.contains(".");
        prop = context.getPropertyOracle().getConfigurationProperty("gdx.reflect.include");
        for (String s : prop.getValues())
            keep |= name.contains(s);
        prop = context.getPropertyOracle().getConfigurationProperty("gdx.reflect.exclude");
        for (String s : prop.getValues())
            keep &= !name.equals(s);
    } catch (BadPropertyValueException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (!keep) {
        nesting--;
        return;
    }

    // already visited this type
    if (types.contains(type.getErasedType())) {
        nesting--;
        return;
    }
    types.add(type.getErasedType());
    out(type.getErasedType().getQualifiedSourceName(), nesting);

    if (type instanceof JPrimitiveType) {
        // nothing to do for a primitive type
        nesting--;
        return;
    } else {
        // gather fields
        JClassType c = (JClassType) type;
        JField[] fields = c.getFields();
        if (fields != null) {
            for (JField field : fields) {
                gatherTypes(field.getType().getErasedType(), types);
            }
        }

        // gather super types & interfaces
        gatherTypes(c.getSuperclass(), types);
        JClassType[] interfaces = c.getImplementedInterfaces();
        if (interfaces != null) {
            for (JClassType i : interfaces) {
                gatherTypes(i.getErasedType(), types);
            }
        }

        // gather method parameter & return types
        JMethod[] methods = c.getMethods();
        if (methods != null) {
            for (JMethod m : methods) {
                gatherTypes(m.getReturnType().getErasedType(), types);
                if (m.getParameterTypes() != null) {
                    for (JType p : m.getParameterTypes()) {
                        gatherTypes(p.getErasedType(), types);
                    }
                }
            }
        }

        // gather inner classes
        JClassType[] inner = c.getNestedTypes();
        if (inner != null) {
            for (JClassType i : inner) {
                gatherTypes(i.getErasedType(), types);
            }
        }
    }
    nesting--;
}

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

License:Apache License

private String createTypeGenerator(JType t) {
    buffer.setLength(0);// w  ww .  j  ava  2s .  c  o  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.gwtent.gen.reflection.ReflectAllInOneCreator.java

License:Apache License

private void processRelationClasses(JClassType classType, Reflectable reflectable) {
    if (classType == null)
        return;//from   w  w  w .  j  a  v a  2s  .  c  o  m

    if (classType.isParameterized() != null)
        classType = classType.isParameterized().getBaseType();

    if (classType.isRawType() != null || classType.isWildcard() != null || classType.isTypeParameter() != null)
        classType = classType.getErasedType();

    if (relationClassesProcessed.contains(classType))
        return;

    processAnnotationClasses(classType, reflectable);

    if (reflectable.superClasses()) {
        if (classType.getSuperclass() != null) {
            processRelationClass(classType.getSuperclass(), reflectable);
        }
    }

    if (reflectable.relationTypes()) {
        for (JClassType type : classType.getImplementedInterfaces()) {
            processRelationClass(type, reflectable);
        }
    }

    relationClassesProcessed.add(classType);

    processFields(classType, reflectable);

    processMethods(classType, reflectable);
}

From source file:com.gwtent.gen.reflection.ReflectAllInOneCreator.java

License:Apache License

private boolean addClassIfNotExists(JClassType classType, Reflectable setting) {
    //Add next line we can make sure we just append normal class type, always get from TypeOracle
    //not JParameterizedType or JTypeParameter etc...
    //RC2 we support ParameterizedType now.
    if (classType != null && classType.isParameterized() == null) {
        //         System.out.println("addClassIfNotExists: " + classType.getQualifiedSourceName());
        classType = this.typeOracle.findType(classType.getQualifiedSourceName());

    }//from  w  ww.  j a va2s  . c o m

    //we just process public classes
    if ((classType == null) || (classType.isPrivate()) || (classType.isProtected())
            || (GeneratorHelper.isSystemClass(classType) && !classType.isPublic()))
        return false;

    //no need java.lang.class
    if (classType.getQualifiedSourceName().equals("java.lang.Class"))
        return false;

    if (candidateList.indexOf(classType.getErasedType()) < 0) {
        candidateList.add(classType.getErasedType());
        candidates.put(classType.getErasedType(), setting);
        return true;
    }

    return false;
}

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

License:Open Source License

private void generateClass(TreeLogger logger, GeneratorContext context)
        throws UnableToCompleteException, NotFoundException {
    PrintWriter printWriter = context.tryCreate(logger, packageName, className);

    if (printWriter == null) {
        return;/*from ww w.j a  v a2  s  . c o m*/
    }

    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, className);

    composer.addImport(HashMap.class.getCanonicalName());

    composer.addImplementedInterface(classType.getQualifiedSourceName());
    JClassType target = typeOracle.getType(ClassMap.class.getCanonicalName());
    for (JClassType interfaze : classType.getImplementedInterfaces()) {
        if (interfaze.getErasedType().equals(target.getErasedType())) {
            JClassType[] genericTypes = interfaze.isParameterized().getTypeArgs();
            keyType = genericTypes[0];
            valueType = genericTypes[1];
        }
    }

    //      if (!"java.lang.String".equals(keyType.getQualifiedSourceName())){
    //         logger.log(TreeLogger.ERROR, "keyType must be a String for now");
    //         throw new UnableToCompleteException();
    //      }

    composer.addImport(keyType.getQualifiedSourceName());
    composer.addImport(valueType.getQualifiedSourceName());

    SourceWriter sourceWriter = composer.createSourceWriter(context, printWriter);

    writeMethods(sourceWriter);
    writeData(sourceWriter);

    sourceWriter.outdent();
    sourceWriter.println("}");

    context.commit(logger, printWriter);
}

From source file:de.csenk.gwt.ws.rebind.filter.serialization.GWTSerializerGenerator.java

License:Open Source License

/**
 * @param logger//  w w w. ja  va2  s .  co m
 * @param ctx
 * @param serializerInterface
 * @return
 */
private SourceWriter getSourceWriter(TreeLogger logger, GeneratorContext ctx, JClassType serializerInterface) {
    JPackage serializerIntfPkg = serializerInterface.getPackage();
    String packageName = serializerIntfPkg == null ? "" : serializerIntfPkg.getName();

    PrintWriter printWriter = ctx.tryCreate(logger, packageName,
            getImplementationSimpleName(serializerInterface));
    if (printWriter == null) {
        return null;
    }

    ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(packageName,
            getImplementationSimpleName(serializerInterface));

    String[] imports = new String[] { ClientGWTSerializer.class.getCanonicalName(),
            GWT.class.getCanonicalName() };
    for (String imp : imports) {
        composerFactory.addImport(imp);
    }

    composerFactory.setSuperclass(ClientGWTSerializer.class.getSimpleName());
    composerFactory.addImplementedInterface(serializerInterface.getErasedType().getQualifiedSourceName());

    return composerFactory.createSourceWriter(ctx, printWriter);
}