Example usage for com.google.gwt.core.ext.typeinfo JMethod getName

List of usage examples for com.google.gwt.core.ext.typeinfo JMethod getName

Introduction

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

Prototype

String getName();

Source Link

Usage

From source file:cc.alcina.framework.entity.gen.SimpleCssResourceGenerator.java

License:Apache License

@Override
public String createAssignment(TreeLogger logger, ResourceContext context, JMethod method)
        throws UnableToCompleteException {
    try {/*from   w  w w.j  a v  a 2  s.c  o  m*/
        ConfigurationProperty cp = context.getGeneratorContext().getPropertyOracle()
                .getConfigurationProperty(IGNORE_DATA_URLS);
        logMissingUrlResources = !Boolean.valueOf(cp.getValues().get(0));
    } catch (BadPropertyValueException e1) {
        e1.printStackTrace();
    }
    URL[] resources = ResourceGeneratorUtil.findResources(logger, context, method);
    if (resources.length != 1) {
        logger.log(TreeLogger.ERROR, "Exactly one resource must be specified", null);
        throw new UnableToCompleteException();
    }
    URL resource = resources[0];
    SourceWriter sw = new StringSourceWriter();
    // Write the expression to create the subtype.
    sw.println("new " + SimpleCssResource.class.getName() + "() {");
    sw.indent();
    if (!AbstractResourceGenerator.STRIP_COMMENTS) {
        // Convenience when examining the generated code.
        sw.println("// " + resource.toExternalForm());
    }
    sw.println("public String getText() {");
    sw.indent();
    String toWrite = Util.readURLAsString(resource);
    if (context.supportsDataUrls()) {
        try {
            toWrite = replaceWithDataUrls(context, toWrite);
        } catch (Exception e) {
            logger.log(Type.ERROR, "css data url gen", e);
            throw new UnableToCompleteException();
        }
    }
    if (toWrite.length() > MAX_STRING_CHUNK) {
        writeLongString(sw, toWrite);
    } else {
        sw.println("return \"" + Generator.escape(toWrite) + "\";");
    }
    sw.outdent();
    sw.println("}");
    sw.println("public String getName() {");
    sw.indent();
    sw.println("return \"" + method.getName() + "\";");
    sw.outdent();
    sw.println("}");
    sw.outdent();
    sw.println("}");
    return sw.toString();
}

From source file:cc.alcina.framework.entity.gwtsynth.ClientReflectionGenerator.java

License:Apache License

public Set<Annotation> getSuperclassAnnotationsForMethod(JMethod m) {
    if (superMethodAnnotationMap.containsKey(m)) {
        return superMethodAnnotationMap.get(m);
    }//  w  ww .  ja va  2 s  .  c  o  m
    Map<Class, Annotation> uniqueMap = new HashMap<Class, Annotation>();
    JClassType c = m.getEnclosingType();
    Set<? extends JClassType> flattenedSupertypeHierarchy = c.getFlattenedSupertypeHierarchy();
    for (JClassType jct : flattenedSupertypeHierarchy) {
        try {
            JMethod m2 = jct.getMethod(m.getName(), m.getParameterTypes());
            for (Annotation a : getVisibleAnnotations(m2, visibleAnnotationClasses)) {
                if (!uniqueMap.containsKey(a.annotationType())) {
                    uniqueMap.put(a.annotationType(), a);
                }
            }
        } catch (Exception e) {
        }
    }
    HashSet values = new HashSet(uniqueMap.values());
    superMethodAnnotationMap.put(m, values);
    return values;
}

From source file:cc.alcina.framework.entity.gwtsynth.ClientReflectionGenerator.java

License:Apache License

private List<JMethod> getPropertyGetters(JClassType jct) {
    ArrayList<JMethod> methods = new ArrayList<JMethod>();
    JMethod[] jms = jct.getInheritableMethods();
    for (JMethod jm : jms) {
        String name = jm.getName();
        if ((name.startsWith("get") && name.length() > 3) || (name.startsWith("is") && name.length() > 2)) {
            if (jm.getParameters().length == 0) {
                methods.add(jm);/*from   ww w .  j  a v a 2  s  . c  o m*/
            }
        }
    }
    return methods;
}

From source file:cc.alcina.framework.entity.gwtsynth.ClientReflectionGenerator.java

License:Apache License

private String getPropertyNameForReadMethod(JMethod method) {
    String name = method.getName();
    int offset = name.startsWith("is") ? 2 : 3;
    return name.substring(offset, offset + 1).toLowerCase() + name.substring(offset + 1);
}

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;//from   w ww .  j  a  v a2s.co  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.ait.ext4j.rebind.BeanModelGenerator.java

License:Apache License

protected String createFactory(JClassType bean, String beanModelName, TreeLogger logger,
        GeneratorContext context) throws Exception {
    final String genPackageName = BeanModelLookup.class.getPackage().getName();
    final String genClassName = "BeanModel_" + bean.getQualifiedSourceName().replace(".", "_") + "_Factory";

    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(genPackageName, genClassName);
    composer.setSuperclass(BeanModelFactory.class.getCanonicalName());
    PrintWriter pw = context.tryCreate(logger, genPackageName, genClassName);

    if (pw != null) {
        List<JMethod> getters = findGetters(bean);
        String typeName = bean.getQualifiedSourceName();
        SourceWriter sw = composer.createSourceWriter(context, pw);
        sw.println("public BeanModel newInstance() {");
        sw.println("return new " + beanModelName + "();");
        sw.println("}");

        sw.println("public BeanModel createModel(Object bean) {");
        sw.println("BeanModel model = newInstance();");
        for (JMethod method : getters) {
            String s = method.getName();
            String p = lowerFirst(s.substring(s.startsWith("g") ? 3 : 2)); // get
            sw.println("model.set(\"" + p + "\"," + " ((" + typeName + ")bean)." + s + "()" + ");");
        }/*from  ww  w  .  j a  v  a2 s .  c  om*/
        sw.println("model.setBean(bean);");
        sw.println("return model;");
        sw.println("}");
        sw.commit(logger);
    }
    return composer.getCreatedClassName();
}

From source file:com.ait.ext4j.rebind.BeanModelGenerator.java

License:Apache License

protected String createBean(JClassType bean, TreeLogger logger, GeneratorContext context) throws Exception {
    final String genPackageName = bean.getPackage().getName();
    final String genClassName = "BeanModel_" + bean.getQualifiedSourceName().replace(".", "_");

    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(genPackageName, genClassName);
    composer.setSuperclass(BeanModel.class.getCanonicalName());
    composer.addImport(BeanModel.class.getName());
    composer.addImport(NestedModelUtil.class.getName());
    PrintWriter pw = context.tryCreate(logger, genPackageName, genClassName);

    if (pw != null) {
        List<JMethod> getters = findGetters(bean);
        List<JMethod> setters = findSetters(bean);
        SourceWriter sw = composer.createSourceWriter(context, pw);

        sw.println("public " + genClassName + "(){");
        for (JMethod method : getters) {
            String s = method.getName();
            String p = lowerFirst(s.substring(s.startsWith("g") ? 3 : 2)); // get
                                                                           // or
                                                                           // is
            sw.println("beanProperties.add(\"" + p + "\");");
        }//from  w ww .jav  a2  s. com
        sw.println("}");

        createGetMethods(getters, sw, bean.getQualifiedSourceName());
        createSetMethods(setters, sw, bean.getQualifiedSourceName());

        // delegate equals to bean
        sw.println("public boolean equals(Object obj) {");
        sw.println("  if (obj instanceof " + "BeanModel" + ") {");
        sw.println("    obj = ((BeanModel)obj).getBean();");
        sw.println("  }");
        sw.println("  return bean.equals(obj);");
        sw.println("}");

        // delegate hashCode to bean
        sw.println("public int hashCode(){");
        sw.println("  return bean.hashCode();");
        sw.println("}");

        sw.commit(logger);
    }
    return composer.getCreatedClassName();
}

From source file:com.ait.ext4j.rebind.BeanModelGenerator.java

License:Apache License

protected void createGetMethods(List<JMethod> getters, SourceWriter sw, String typeName) {
    sw.println("public <X> X getPropertyAsString(String s) {");

    sw.println("if (allowNestedValues && NestedModelUtil.isNestedProperty(s)) {");
    sw.indentln("return (X)NestedModelUtil.getNestedValue(this, s);");
    sw.println("}");

    for (JMethod method : getters) {
        JClassType returnType = method.getReturnType().isClassOrInterface();
        String s = method.getName();
        String p = lowerFirst(s.substring(s.startsWith("g") ? 3 : 2)); // get
                                                                       // or

        sw.println("if (s.equals(\"" + p + "\")) {");
        sw.println("Object value = ((" + typeName + ")bean)." + s + "();");

        try {//from w  ww.ja  v a 2  s.c o m
            if (returnType != null && returnType.isAssignableTo(oracle.getType(List.class.getName()))
                    && returnType.isParameterized() != null) {
                JParameterizedType type = returnType.isParameterized();
                JClassType[] params = type.getTypeArgs();
                if (beans.contains(params[0])) {
                    sw.println("if (value != null) {");
                    sw.indent();
                    sw.println("java.util.List list = (java.util.List)value;");
                    sw.println("java.util.List list2 = " + BeanModelLookup.class.getCanonicalName()
                            + ".get().getFactory(" + params[0].getQualifiedSourceName()
                            + ".class).createModel((java.util.Collection) list);");
                    sw.outdent();
                    sw.println("return (X) list2;");
                    sw.println("}");
                }
            } else {
                // swap returnType as generic types were not matching
                // (beans.contains(returnType))
                if (returnType != null) {
                    String t = returnType.getQualifiedSourceName();
                    if (t.indexOf("extends") == -1) {
                        returnType = oracle.getType(t);
                    }
                }
                if (beans.contains(returnType)) {
                    sw.println("if (value != null) {");
                    sw.println("    BeanModel nestedModel = nestedModels.get(s);");
                    sw.println("    if (nestedModel != null) {");
                    sw.println("      Object bean = nestedModel.getBean();");
                    sw.println("      if (!bean.equals(value)){");
                    sw.println("        nestedModel = null;");
                    sw.println("      }");
                    sw.println("    }");
                    sw.println("    if (nestedModel == null) {");
                    sw.println("        nestedModel = " + BeanModelLookup.class.getCanonicalName()
                            + ".get().getFactory(" + returnType.getQualifiedSourceName()
                            + ".class).createModel(value);");
                    sw.println("        nestedModels.put(s, nestedModel);");
                    sw.println("    }");
                    sw.println("    return (X)processValue(nestedModel);");
                    sw.println("}");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        sw.println("return (X)processValue(value);");
        sw.println("}");
    }
    sw.println("return super.getFromCache(s);");
    sw.println("}");
}

From source file:com.ait.ext4j.rebind.BeanModelGenerator.java

License:Apache License

protected void createSetMethods(List<JMethod> properties, SourceWriter sw, String typeName) {
    sw.println("public <X> X setProperty(String s, X val) {");
    sw.indent();//from   w w  w.  j  a  v a 2  s . com
    sw.println("Object obj = val;");

    sw.println("if (obj instanceof BeanModel) {");
    sw.println("obj = ((BeanModel) obj).getBean();");
    sw.println("} else if (obj instanceof java.util.List) {");
    sw.println("java.util.List list = new java.util.ArrayList();");
    sw.println("for(Object o : (java.util.List) obj) {");
    sw.println("if(o instanceof BeanModel) {");
    sw.println("list.add(((BeanModel) o).getBean());");
    sw.println("} else {");
    sw.println("list.add(o);");
    sw.println("}");
    sw.println("}");
    sw.println("obj = list;");
    sw.println("}");

    sw.println("if (allowNestedValues && val instanceof BeanModel) {");
    sw.indent();
    sw.println("obj = ((BeanModel)val).getBean();");
    sw.println("if (nestedModels.containsKey(s)) {");
    sw.indent();
    sw.println("nestedModels.put(s, (BeanModel)val);");
    sw.outdent();
    sw.println("}");
    sw.outdent();
    sw.println("}");

    sw.println("if (allowNestedValues && NestedModelUtil.isNestedProperty(s)) {");
    sw.indent();
    sw.println("X old = (X) NestedModelUtil.setNestedValue(this, s, val);");
    sw.println("notifyPropertyChanged(s, val, old);");
    sw.println("return old;");
    sw.outdent();
    sw.println("}");

    for (JMethod method : properties) {
        String s = method.getName();
        String p = lowerFirst(s.substring(3));
        String type = getMethodAttributeType(method);

        if (type.indexOf("extends") != -1) {
            type = "java.lang.Object";
        }

        if (type.equals("byte")) {
            type = "Byte";
        } else if (type.equals("char")) {
            type = "Character";
        } else if (type.equals("short")) {
            type = "Short";
        } else if (type.equals("int")) {
            type = "Integer";
        } else if (type.equals("long")) {
            type = "Long";
        } else if (type.equals("float")) {
            type = "Float";
        } else if (type.equals("double")) {
            type = "Double";
        } else if (type.equals("boolean")) {
            type = "Boolean";
        }

        sw.println("if (s.equals(\"" + p + "\")) {");
        sw.indent();
        sw.println("Object old = get(s);");

        sw.println("((" + typeName + ")bean)." + s + "((" + type + ")obj);");
        sw.println("notifyPropertyChanged(s, val, old);");
        sw.println("return (X)old;");
        sw.outdent();
        sw.println("}");
    }
    sw.println("return super.set(s, val);");
    sw.outdent();
    sw.println("}");
}

From source file:com.ait.ext4j.rebind.BeanModelGenerator.java

License:Apache License

protected void addGetters(JClassType cls, List<JMethod> methods) {
    if (cls == null) {
        return;//from w w w  .  j  a v  a 2s.  c  o m
    }
    // ignore methods of Object
    if (cls.isInterface() != null || cls.getSuperclass() != null) {
        addGetters(cls.getSuperclass(), methods);
        for (JMethod m : cls.getMethods()) {
            if (m.isPublic() || m.isProtected()) {
                String name = m.getName();
                if ((name.matches("get.*") || name.matches("is.*")) && m.getParameters().length == 0) {
                    methods.add(m);
                }
            }
        }
    }

}