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

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

Introduction

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

Prototype

JPackage getPackage();

Source Link

Usage

From source file:ch.unifr.pai.twice.comm.serverPush.rebind.RemoteEventDeSerializerGenerator.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;//w w  w  .  j av a 2 s .c om

    try {
        classType = context.getTypeOracle().getType(typeName);
        // 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 " + RemoteEvent.class.getName() + "<?> deserialize(" + JSONObject.class.getName()
                            + " o, String t, String string, " + TWICESecurityManager.class.getName()
                            + " securityManager) throws " + MessagingException.class.getName() + " {");
            JClassType abstractRemoteEvent = context.getTypeOracle().findType(RemoteEvent.class.getName());
            src.println("if(t==null){");
            src.println("return null;");
            src.println("}");
            for (JClassType subType : abstractRemoteEvent.getSubtypes()) {
                if (!subType.getPackage().getName()
                        .contains(ch.unifr.pai.twice.comm.serverPush.client.RemoteEventDeserializer.class
                                .getPackage().getName())
                        && !subType.getName().endsWith("Impl")) {
                    src.println("else if(t.equals(" + subType.getQualifiedSourceName() + ".class.getName())){");
                    src.println(subType.getQualifiedSourceName() + " event = " + GWT.class.getName()
                            + ".create(" + subType.getQualifiedSourceName() + ".class);");
                    src.println("return event.deserialize(string, securityManager);");
                    src.println("}");
                }
            }
            src.println("return null;}");
            src.commit(logger);
        }
        return typeName + "Impl";
    } catch (NotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

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

License:Apache License

public SourceWriter getSourceWriter(JClassType classType, GeneratorContext context, TreeLogger logger) {
    String packageName = classType.getPackage().getName();
    String simpleName = classType.getSimpleSourceName() + "Impl";
    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, simpleName);
    composer.setSuperclass(classType.getName());

    composer.addImport(classType.getQualifiedSourceName());

    // Need to add whatever imports your generated class needs.
    // composer.addImport("com.google.gwt.user.client.Window");
    // composer.addImport("com.google.gwt.user.client.rpc.AsyncCallback");
    // composer.addImport("com.google.gwt.user.client.ui.Button");
    // composer.addImport("com.google.gwt.user.client.ui.RootPanel");

    PrintWriter printWriter = context.tryCreate(logger, packageName, simpleName);
    if (printWriter == null) {
        return null;
    } else {//from   w w  w .ja  va  2  s. c  om
        SourceWriter sw = composer.createSourceWriter(context, printWriter);
        return sw;
    }
}

From source file:ch.unifr.pai.twice.module.rebind.TWICEModuleGenerator.java

License:Apache License

/**
 * Define the class to be generated./*from   www  . ja  v a 2  s.com*/
 * 
 * @param classType
 * @param context
 * @param logger
 * @return
 */
public SourceWriter getSourceWriter(JClassType classType, GeneratorContext context, TreeLogger logger) {
    String packageName = classType.getPackage().getName();
    String simpleName = classType.getSimpleSourceName() + "Impl";
    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, simpleName);
    composer.setSuperclass(classType.getName());
    composer.addImplementedInterface(TWICEModuleInstantiator.class.getName() + "<"
            + getGenericClass(classType).getQualifiedSourceName() + ">");

    PrintWriter printWriter = context.tryCreate(logger, packageName, simpleName);
    if (printWriter == null) {
        return null;
    } else {
        SourceWriter sw = composer.createSourceWriter(context, printWriter);
        return sw;
    }
}

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 ww w.java  2  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.TemplateGenerator.java

License:Apache License

/**
 * Given a TemplateResource interface, return the path to its .html file,
 * suitable for any classloader to find it as a resource. If the .html does
 * not exist or is empty see if the template was declared inline. If no
 * content is found we throw an exception.
 *//*from   w  ww  .ja v a  2s. c  o m*/
private String getTemplateContent(GeneratorContext context, TreeLogger logger, JClassType interfaceType)
        throws UnableToCompleteException {
    String templateHtmlFile = null;

    TemplateResource annotation = interfaceType.getAnnotation(TemplateResource.class);
    if (annotation == null) {
        // if the interface is defined as a nested class, use the name of
        // the
        // enclosing type
        if (interfaceType.getEnclosingType() != null) {
            interfaceType = interfaceType.getEnclosingType();
        }
        templateHtmlFile = slashify(interfaceType.getQualifiedBinaryName()) + TEMPLATE_SUFFIX;
        logger.log(TreeLogger.INFO, "Template : " + templateHtmlFile);

        InputStream stream = getTemplateResource(context, logger, templateHtmlFile);
        if (stream == null) {
            logger.log(Type.ERROR, "No data could be loaded - no data at path "
                    + interfaceType.getQualifiedBinaryName() + TEMPLATE_SUFFIX);
            throw new UnableToCompleteException();
        }
        return sanitize(Util.readStreamAsString(stream));

    } else {
        // first we look at the HTML File
        templateHtmlFile = annotation.source();

        if (templateHtmlFile.length() > 0) {

            if (!templateHtmlFile.endsWith(TEMPLATE_SUFFIX)) {
                logger.log(TreeLogger.ERROR, "Template file name must end with " + TEMPLATE_SUFFIX);
                throw new UnableToCompleteException();
            }

            if (annotation.value().length() != 0) {
                logger.log(Type.WARN, "Found both source file and inline template, using source file");
            }

            templateHtmlFile = slashify(interfaceType.getPackage().getName()) + "/" + templateHtmlFile;
            InputStream stream = getTemplateResource(context, logger, templateHtmlFile);
            return sanitize(Util.readStreamAsString(stream));

        } else if (annotation.value().length() > 0) {
            return annotation.value();
        } else {
            logger.log(Type.ERROR,
                    "Template annotation found with no contents, cannot generate method , this may cause other failures.");
        }

    }
    return null;
}

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

License:Apache License

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    TypeOracle oracle = context.getTypeOracle();
    this.templatesInterface = oracle.findType(Name.getSourceNameForClass(Template.class));

    JClassType interfaceType;
    try {/* w  w w  . jav a  2s.co  m*/
        interfaceType = oracle.getType(typeName);
    } catch (NotFoundException e) {
        throw new RuntimeException(e);
    }

    if (interfaceType.isInterface() == null) {
        logger.log(TreeLogger.ERROR, typeName + " is not an interface type");
        throw new UnableToCompleteException();
    }
    if (!interfaceType.isAssignableTo(templatesInterface)) {
        logger.log(Type.ERROR, "This isn't a Template subtype...");
        throw new UnableToCompleteException();
    }

    String content = getTemplateContent(context, logger, interfaceType);
    String packageName = interfaceType.getPackage().getName();
    String className = "Template_For_" + interfaceType.getQualifiedSourceName().replace(".", "_");

    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, className);
    composer.addImport(SafeHtml.class.getName());
    composer.addImport(SafeHtmlUtils.class.getName());
    composer.addImplementedInterface(Template.class.getName());

    PrintWriter pw = context.tryCreate(logger, packageName, className);
    SourceWriter sw = composer.createSourceWriter(context, pw);

    sw.println("  public SafeHtml getContent(){");
    sw.println("      return SafeHtmlUtils.fromSafeConstant(\"" + content + "\");");
    sw.println("  }");
    sw.println("");
    sw.println("");
    sw.println("  public SafeHtml getSafeContent(){");
    sw.println("      return SafeHtmlUtils.fromString(\"" + content + "\");");
    sw.println("  }");

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

}

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

License:Open Source License

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

    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(genPackageName, genClassName);
    composer.setSuperclass(Bean.class.getCanonicalName());
    composer.addImport(Bean.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 w  w  . ja va  2s .c o m*/
        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 " + "Bean" + ") {");
        sw.println("    obj = ((Bean)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.toolkit.rebind.TemplateGenerator.java

License:Open Source License

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    TypeOracle oracle = context.getTypeOracle();
    this.templatesInterface = oracle.findType(Name.getSourceNameForClass(Template.class));

    JClassType interfaceType;
    try {/*  www. j  av  a  2s  .c  o m*/
        interfaceType = oracle.getType(typeName);
    } catch (NotFoundException e) {
        throw new RuntimeException(e);
    }

    if (interfaceType.isInterface() == null) {
        logger.log(TreeLogger.ERROR, typeName + " is not an interface type");
        throw new UnableToCompleteException();
    }
    if (!interfaceType.isAssignableTo(templatesInterface)) {
        logger.log(Type.ERROR, "This isn't a Template subtype...");
        throw new UnableToCompleteException();
    }

    String content = getTemplateContent(context, logger, interfaceType);
    String packageName = interfaceType.getPackage().getName();
    String className = "Tpl_For_" + interfaceType.getQualifiedSourceName().replace(".", "_") + "_Generated";

    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, className);
    composer.addImport(SafeHtml.class.getName());
    composer.addImport(SafeHtmlUtils.class.getName());
    composer.addImplementedInterface(Template.class.getName());

    PrintWriter pw = context.tryCreate(logger, packageName, className);

    if (pw != null) {
        SourceWriter sw = composer.createSourceWriter(context, pw);

        sw.println("  public SafeHtml getContent(){");
        sw.println("      return SafeHtmlUtils.fromSafeConstant(\"" + content + "\");");
        sw.println("  }");
        sw.println("");
        sw.println("");
        sw.println("  public SafeHtml getSafeContent(){");
        sw.println("      return SafeHtmlUtils.fromString(\"" + content + "\");");
        sw.println("  }");

        sw.commit(logger);
    }

    return composer.getCreatedClassName();

}

From source file:com.allen_sauer.gwt.log.rebind.LogMessageFormatterGenerator.java

License:Apache License

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    TypeOracle typeOracle = context.getTypeOracle();

    JClassType userType;
    try {//from  w  ww .j  ava 2  s  .co  m
        userType = typeOracle.getType(typeName);
    } catch (NotFoundException e) {
        logger.log(TreeLogger.ERROR, "OOPS", e);
        throw new UnableToCompleteException();
    }
    String packageName = userType.getPackage().getName();
    String className = userType.getName();

    JClassType remoteService = typeOracle.findType(typeName);
    if (remoteService == null) {
        logger.log(TreeLogger.ERROR, "Unable to find metadata for type '" + typeName + "'", null);
        throw new UnableToCompleteException();
    }

    if (remoteService.isInterface() == null) {
        logger.log(TreeLogger.ERROR, remoteService.getQualifiedSourceName() + " is not an interface", null);
        throw new UnableToCompleteException();
    }
    ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(packageName,
            className + "Impl");
    composerFactory.addImplementedInterface(remoteService.getQualifiedSourceName());

    composerFactory.addImport(Date.class.getName());
    composerFactory.addImport(GWT.class.getName());
    composerFactory.addImport(LogUtil.class.getName());
    composerFactory.addImport(Duration.class.getName());

    PrintWriter pw = context.tryCreate(logger, packageName, className + "Impl");
    if (pw != null) {
        SourceWriter sw = composerFactory.createSourceWriter(context, pw);

        PropertyOracle propertyOracle = context.getPropertyOracle();
        String logPattern;
        try {
            ConfigurationProperty logPatternProperty = propertyOracle
                    .getConfigurationProperty(PROPERTY_LOG_PATTERN);
            List<String> values = logPatternProperty.getValues();
            logPattern = values.get(0);
        } catch (BadPropertyValueException e) {
            logger.log(TreeLogger.ERROR, "Unable to find value for '" + PROPERTY_LOG_PATTERN + "'", e);
            throw new UnableToCompleteException();
        }

        sw.println();
        sw.println("private double BIG_BANG = Duration.currentTimeMillis();");

        sw.println();
        sw.println(
                "public String format(String logLevelText, String category, String message, Throwable throwable) {");
        sw.indent();
        sw.println("if (category == null) {");
        sw.indent();
        sw.println("category = \"<null category>\";");
        sw.outdent();
        sw.println("}");
        sw.println("if (message == null) {");
        sw.indent();
        sw.println("message = \"<null message>\";");
        sw.outdent();
        sw.println("}");
        sw.println(logPatternToCode(logPattern));
        sw.outdent();
        sw.println("}");

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

From source file:com.allen_sauer.gwt.log.rebind.RemoteLoggerConfigGenerator.java

License:Apache License

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    TypeOracle typeOracle = context.getTypeOracle();

    JClassType userType;
    try {//from  w ww. j  a  va 2  s.  co m
        userType = typeOracle.getType(typeName);
    } catch (NotFoundException e) {
        logger.log(TreeLogger.ERROR, "OOPS", e);
        throw new UnableToCompleteException();
    }
    String packageName = userType.getPackage().getName();
    String className = userType.getName();

    JClassType remoteService = typeOracle.findType(typeName);
    if (remoteService == null) {
        logger.log(TreeLogger.ERROR, "Unable to find metadata for type '" + typeName + "'", null);
        throw new UnableToCompleteException();
    }

    if (remoteService.isInterface() == null) {
        logger.log(TreeLogger.ERROR, remoteService.getQualifiedSourceName() + " is not an interface", null);
        throw new UnableToCompleteException();
    }
    ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(packageName,
            className + "Impl");
    composerFactory.addImplementedInterface(remoteService.getQualifiedSourceName());

    PrintWriter pw = context.tryCreate(logger, packageName, className + "Impl");
    if (pw != null) {
        SourceWriter sw = composerFactory.createSourceWriter(context, pw);

        PropertyOracle propertyOracle = context.getPropertyOracle();
        String logUrl;
        try {
            ConfigurationProperty logPatternProperty = propertyOracle
                    .getConfigurationProperty(PROPERTY_LOG_URL);
            List<String> values = logPatternProperty.getValues();
            logUrl = values.get(0);
        } catch (BadPropertyValueException e) {
            logger.log(TreeLogger.ERROR, "Unable to find value for '" + PROPERTY_LOG_URL + "'", e);
            throw new UnableToCompleteException();
        }

        sw.println();
        sw.println("public String serviceEntryPointUrl() {");
        sw.indent();

        if (logUrl == null) {
            sw.println("return null;");
        } else {
            sw.println("return \"" + logUrl.trim() + "\";");
        }

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

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