Example usage for com.google.gwt.user.rebind ClassSourceFileComposerFactory addImplementedInterface

List of usage examples for com.google.gwt.user.rebind ClassSourceFileComposerFactory addImplementedInterface

Introduction

In this page you can find the example usage for com.google.gwt.user.rebind ClassSourceFileComposerFactory addImplementedInterface.

Prototype

public void addImplementedInterface(String intfName) 

Source Link

Usage

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

License:Apache License

private void writeAnnotations(TreeLogger logger, GeneratorContext context, List<JAnnotationType> jAnns,
        ClassSourceFileComposerFactory crf, boolean initial) throws Exception {
    for (JAnnotationType type : jAnns) {
        Class<? extends Annotation> annClass = forName(type);
        String implementationName = type.getName() + "_Impl";
        ann2impl.put(annClass, type.getPackage().getName() + "." + implementationName);
    }//from  www.jav a 2  s. c o  m
    for (JAnnotationType type : jAnns) {
        Class<? extends Annotation> annClass = forName(type);
        String implementationName = type.getName() + "_Impl";
        String implFQN = type.getPackage().getName() + "." + implementationName;
        crf.addImport(implFQN);
        ClassSourceFileComposerFactory annf = new ClassSourceFileComposerFactory(type.getPackage().getName(),
                implementationName);
        annf.addImport(Annotation.class.getCanonicalName());
        annf.addImport(type.getQualifiedSourceName());
        annf.addImplementedInterface(type.getName());
        List<Method> declaredMethods = new ArrayList<Method>(Arrays.asList(annClass.getDeclaredMethods()));
        Collections.sort(declaredMethods, ToStringComparator.INSTANCE);
        StringBuffer constrParams = new StringBuffer();
        boolean first = true;
        for (Method method : declaredMethods) {
            Class<?> returnType = method.getReturnType();
            addImport(annf, returnType);
            addImport(crf, returnType);
        }
        PrintWriter printWriter = context.tryCreate(logger, packageName, implementationName);
        // if calling from a non-initial module, we just want to add imports
        // without rewriting (indeed, we can't...) the annotation impls
        if (printWriter != null) {
            SourceWriter sw = createWriter(annf, printWriter);
            for (Method method : declaredMethods) {
                Class returnType = method.getReturnType();
                String rn = returnType.getSimpleName();
                String mn = method.getName();
                StringBuffer sb = new StringBuffer();
                writeLiteral(method.getDefaultValue(), returnType, sb, true);
                sw.println(String.format("private %s %s = %s;", rn, mn, sb.toString()));
                sw.println(String.format("public %s %s(){return %s;}", rn, mn, mn));
                sw.println(String.format("public %s _set%s(%s %s){this.%s = %s;return this;}",
                        implementationName, mn, rn, mn, mn, mn));
                sw.println();
            }
            sw.println();
            sw.println("public Class<? extends Annotation> annotationType() {");
            sw.indentln(String.format("return %s.class;", annClass.getSimpleName()));
            sw.println("}");
            sw.println();
            sw.println(String.format("public %s (){}", implementationName));
            sw.outdent();
            sw.println("}");
            commit(context, logger, printWriter);
        }
    }
}

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

License:Apache License

/**
 * Define the class to be generated./*from w  ww . j  a  v  a 2  s.  co  m*/
 * 
 * @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.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;/*  w  w w.j  a v a2 s .  c o m*/
    try {
        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.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;/*www .  j a  v  a 2s. c  om*/
    try {
        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;/*  w  w w  . j  ava 2  s  . co m*/
    try {
        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;/*w w w  . j  av a2 s.c om*/
    try {
        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();
}

From source file:com.allen_sauer.gwt.voices.crowd.rebind.GwtUserAgentProviderGenerator.java

License:Apache License

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

    JClassType userType;//from   w  ww. j  av a2s  .  c  o  m
    try {
        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 userAgent;
        try {
            SelectionProperty userAgentProperty = propertyOracle.getSelectionProperty(logger,
                    PROPERTY_USER_AGENT);

            // ALWAYS RETURNS 'gecko'
            userAgent = userAgentProperty.getCurrentValue();
        } catch (BadPropertyValueException e) {
            logger.log(TreeLogger.ERROR, "Unable to find value for '" + PROPERTY_USER_AGENT + "'", e);
            throw new UnableToCompleteException();
        }

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

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

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

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

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

License:Apache License

public String create() {
    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, simpleName);
    composer.addImplementedInterface("com.artemis.gwtref.client.IReflectionCache");
    imports(composer);// w  ww.  j a v a2 s.co  m
    PrintWriter printWriter = context.tryCreate(logger, packageName, simpleName);
    if (printWriter == null) {
        return packageName + "." + simpleName;
    }
    sw = composer.createSourceWriter(context, printWriter);

    generateLookups();

    getKnownTypesC();
    forNameC();
    newArrayC();

    getArrayLengthT();
    getArrayElementT();
    setArrayElementT();

    getF();
    setF();

    invokeM();

    sw.commit(logger);
    createProxy(type);
    return packageName + "." + simpleName;
}

From source file:com.badlogic.gdx.backends.gwt.preloader.PreloaderBundleGenerator.java

License:Apache License

private String createDummyClass(TreeLogger logger, GeneratorContext context) {
    String packageName = "com.badlogic.gdx.backends.gwt.preloader";
    String className = "PreloaderBundleImpl";
    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, className);
    composer.addImplementedInterface(packageName + ".PreloaderBundle");
    PrintWriter printWriter = context.tryCreate(logger, packageName, className);
    if (printWriter == null) {
        return packageName + "." + className;
    }//ww  w  . jav a  2 s  . c om
    SourceWriter sourceWriter = composer.createSourceWriter(context, printWriter);
    sourceWriter.commit(logger);
    return packageName + "." + className;
}

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

License:Apache License

public String create() {
    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, simpleName);
    composer.addImplementedInterface("com.badlogic.gwtref.client.IReflectionCache");
    imports(composer);/*  www .j  a v  a 2s  .  co  m*/
    PrintWriter printWriter = context.tryCreate(logger, packageName, simpleName);
    if (printWriter == null) {
        return packageName + "." + simpleName;
    }
    sw = composer.createSourceWriter(context, printWriter);

    generateLookups();

    forNameC();
    newArrayC();

    getArrayLengthT();
    getArrayElementT();
    setArrayElementT();

    getF();
    setF();

    invokeM();

    sw.commit(logger);
    createProxy(type);
    return packageName + "." + simpleName;
}