Example usage for com.google.gwt.user.rebind SourceWriter println

List of usage examples for com.google.gwt.user.rebind SourceWriter println

Introduction

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

Prototype

void println();

Source Link

Usage

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;//from www . j  av  a2 s.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());

    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;/*ww w  .  ja  va 2  s.com*/
    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;//w  w  w  .j  av  a2  s  .  com
    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.example.gwt.dagger2.rebind.VersionGenerator.java

License:Apache License

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    String result = null;//from   w w w .ja v a 2s  . c  o  m
    try {
        String version = findVersion(logger, context);
        JClassType classType = context.getTypeOracle().getType(typeName);
        String packageName = packageNameFrom(classType);
        String simpleName = simpleNameFrom(classType);
        result = packageName + '.' + simpleName;
        SourceWriter source = getSourceWriter(logger, context, classType);
        if (source != null) { //? Otherwise, work needs to be done.
            source.println();
            source.println("private String value;");
            source.println();
            source.println("public " + simpleName + "() {");
            populateInstanceFactory(logger, context, typeName, source, version);
            source.println("}");
            source.println();
            source.println("@Override");
            source.println("public String getValue() {");
            source.println(" return value;");
            source.println("}");
            source.println();
            source.commit(logger);
            //emitVersionArtifact(logger, context, version);
        }
    } catch (NotFoundException nfe) {
        logger.log(Type.ERROR, "Could not find extension point type '" + typeName + "'!", nfe);
        throw new UnableToCompleteException();
    }
    return result;
}

From source file:com.google.gwt.testing.easygwtmock.rebind.MocksControlGenerator.java

License:Apache License

/**
 * Generates the concrete MocksControl implementation of the {@code typeName} interface and 
 * delegates generation of mock classes to
 * {@link com.google.gwt.testing.easygwtmock.rebind.MocksGenerator}
 */// w  w  w .  j ava 2s  . c om
@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {

    TypeOracle typeOracle = context.getTypeOracle();
    JClassType mockControlInterface = typeOracle.findType(typeName);

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

    if (mockControlInterface.isInterface() == null) {
        logger.log(TreeLogger.ERROR, mockControlInterface.getQualifiedSourceName() + " is not an interface",
                null);
        throw new UnableToCompleteException();
    }

    JPackage interfacePackage = mockControlInterface.getPackage();
    String packageName = interfacePackage == null ? "" : interfacePackage.getName();
    String newClassName = mockControlInterface.getName().replace(".", "_") + "Impl";
    String fullNewClassName = packageName + "." + newClassName;

    PrintWriter printWriter = context.tryCreate(logger, packageName, newClassName);
    if (printWriter == null) {
        // We generated this before.
        return fullNewClassName;
    }

    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, newClassName);
    composer.setSuperclass(MocksControlBase.class.getCanonicalName());
    composer.addImplementedInterface(mockControlInterface.getQualifiedSourceName());

    SourceWriter writer = composer.createSourceWriter(context, printWriter);
    writer.println();

    MocksGenerator mocksGenerator = new MocksGenerator(context, logger);
    JClassType markerInterface = typeOracle.findType(MocksControl.class.getCanonicalName());

    Set<String> reservedNames = getMethodNames(composer.getSuperclassName(), logger, typeOracle);

    // Report one error per method in the control interface. They are likely to be independent,
    // so it's a bit nicer for the user.
    boolean failed = false;
    for (JMethod method : mockControlInterface.getOverridableMethods()) {
        if (method.getEnclosingType().equals(markerInterface)) {
            // Method is implemented in MocksControlBase
            continue;
        }

        if (reservedNames.contains(method.getName())) {
            // Method name is already used in base class and method should not be overwritten!
            logger.log(TreeLogger.ERROR,
                    method.getName()
                            + " is a reserved name. Do not use it in the extended MocksControl interface",
                    null);
            failed = true;
            continue;
        }

        JClassType typeToMock = method.getReturnType().isClassOrInterface();

        if (typeToMock == null) {
            logger.log(TreeLogger.ERROR,
                    method.getReturnType().getQualifiedSourceName() + " is not an interface or a class", null);
            failed = true;
            continue;
        }

        if (typeToMock.isInterface() != null) {

            if (method.getParameterTypes().length != 0) {
                String methodName = mockControlInterface.getSimpleSourceName() + "." + method.getName();
                logger.log(TreeLogger.ERROR,
                        "This method should not have parameters because it creates Ua mock of an interface: "
                                + methodName,
                        null);
                failed = true;
                continue;
            }

        } else {

            JConstructor constructorToCall = typeToMock.findConstructor(method.getParameterTypes());
            if (constructorToCall == null) {
                String methodName = mockControlInterface.getSimpleSourceName() + "." + method.getName();
                logger.log(TreeLogger.ERROR, "Cannot find matching constructor to call for " + methodName,
                        null);
                failed = true;
                continue;
            }
        }

        String mockClassName = mocksGenerator.generateMock(typeToMock);

        printFactoryMethod(writer, method, mockControlInterface, mockClassName);
    }

    if (failed) {
        throw new UnableToCompleteException();
    }

    writer.commit(logger);
    return fullNewClassName;
}

From source file:com.google.gwt.testing.easygwtmock.rebind.MocksGenerator.java

License:Apache License

/**
 * Generates a mock class for {@code interfaceToMock}.
 *//*from w ww.j av  a  2 s  . c o  m*/
String generateMock(JClassType typeToMock) throws UnableToCompleteException {

    JPackage interfacePackage = typeToMock.getPackage();
    String packageName = interfacePackage == null ? "" : interfacePackage.getName();
    String newClassName = typeToMock.getName().replace(".", "_") + "Mock";

    // GenericType<Integer> has to generate a different mock implementation than
    // GenericType<String>, that's what we check and do here
    if (typeToMock.isParameterized() != null) {
        StringBuilder typeList = new StringBuilder();
        for (JClassType genericArg : typeToMock.isParameterized().getTypeArgs()) {
            typeList.append(genericArg.getParameterizedQualifiedSourceName());
        }
        newClassName += Integer.toHexString(typeList.toString().hashCode());
    }

    String fullNewClassName = packageName + "." + newClassName;

    PrintWriter printWriter = this.context.tryCreate(this.logger, packageName, newClassName);
    if (printWriter == null) {
        // We generated this before.
        return fullNewClassName;
    }

    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, newClassName);
    composer.addImport(MocksControlBase.class.getCanonicalName());
    composer.addImport(Method.class.getCanonicalName());
    composer.addImport(Call.class.getCanonicalName());
    composer.addImport(UndeclaredThrowableException.class.getCanonicalName());
    if (typeToMock.isInterface() != null) {
        composer.addImplementedInterface(typeToMock.getParameterizedQualifiedSourceName());
    } else {
        composer.setSuperclass(typeToMock.getParameterizedQualifiedSourceName());
    }

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

    JMethod[] overridableMethods = typeToMock.getOverridableMethods();

    List<JMethod> methodsToMock = new ArrayList<JMethod>();
    Set<String> needsDefaultImplementation = new HashSet<String>();
    for (JMethod method : overridableMethods) {
        if (isSpecialMethodOfObject(method)) {
            needsDefaultImplementation.add(method.getName());
        } else if (method.getParameterTypes().length == 0 && method.getName().equals("getClass")) {
            // ignore, Bug 5026788 in GWT
        } else {
            methodsToMock.add(method);
        }
    }

    printFields(sourceWriter, methodsToMock);
    printConstructors(sourceWriter, newClassName, typeToMock.getConstructors());
    printMockMethods(sourceWriter, methodsToMock, newClassName);
    printDefaultMethods(sourceWriter, typeToMock, needsDefaultImplementation);

    sourceWriter.commit(this.logger);

    return fullNewClassName;
}

From source file:com.googlecode.mgwt.useragent.rebind.UserAgentGenerator.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  a v a 2 s .  c  om
    try {
        userType = typeOracle.getType(typeName);
    } catch (NotFoundException e) {
        logger.log(TreeLogger.ERROR, "Unable to find metadata for type: " + typeName, e);
        throw new UnableToCompleteException();
    }
    String packageName = userType.getPackage().getName();
    String className = userType.getName();
    className = className.replace('.', '_');

    if (userType.isInterface() == null) {
        logger.log(TreeLogger.ERROR, userType.getQualifiedSourceName() + " is not an interface", null);
        throw new UnableToCompleteException();
    }

    PropertyOracle propertyOracle = context.getPropertyOracle();

    String userAgentValue;
    SelectionProperty selectionProperty;
    try {
        selectionProperty = propertyOracle.getSelectionProperty(logger, PROPERTY_USER_AGENT);
        userAgentValue = selectionProperty.getCurrentValue();
    } catch (BadPropertyValueException e) {
        logger.log(TreeLogger.ERROR, "Unable to find value for '" + PROPERTY_USER_AGENT + "'", e);
        throw new UnableToCompleteException();
    }

    String userAgentValueInitialCap = userAgentValue.substring(0, 1).toUpperCase(Locale.ENGLISH)
            + userAgentValue.substring(1);
    className = className + "Impl" + userAgentValueInitialCap;

    ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(packageName, className);
    composerFactory.addImplementedInterface(userType.getQualifiedSourceName());

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

        sw.println();
        sw.println("public native String getRuntimeValue() /*-{");
        sw.indent();
        UserAgentPropertyGenerator.writeUserAgentPropertyJavaScript(sw, selectionProperty.getPossibleValues(),
                null);
        sw.outdent();
        sw.println("}-*/;");
        sw.println();

        sw.println();
        sw.println("public String getCompileTimeValue() {");
        sw.indent();
        sw.println("return \"" + userAgentValue.trim() + "\";");
        sw.outdent();
        sw.println("}");

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

From source file:com.gwtplatform.mvp.rebind.GinjectorGenerator.java

License:Apache License

private void writeBundleGetters(SourceWriter sourceWriter, Map<String, List<JClassType>> bundles,
        GeneratorContext generatorContext) throws UnableToCompleteException {
    for (Entry<String, List<JClassType>> entry : bundles.entrySet()) {
        providerBundleGenerator.setPresenters(entry.getValue());
        providerBundleGenerator.setPackageName(getPackageName());
        String bundleName = providerBundleGenerator.generate(getTreeLogger(), generatorContext, entry.getKey());
        sourceWriter.println();
        sourceWriter.println(String.format(GETTER_PROVIDER_METHOD, AsyncProvider.class.getSimpleName(),
                bundleName, getSimpleNameFromTypeName(bundleName)));
    }/*  www . j a v  a  2 s. co  m*/
}

From source file:com.gwtplatform.mvp.rebind.GinjectorGenerator.java

License:Apache License

private void writeGatekeeperGetterFromList(SourceWriter sourceWriter, Collection<JClassType> gatekeepers) {
    for (JClassType gatekeeper : gatekeepers) {
        String name = gatekeeper.getQualifiedSourceName();

        sourceWriter.println();
        if (gatekeeper.isAnnotationPresent(DefaultGatekeeper.class)) {
            sourceWriter.println(String.format(DEFAULT_GATEKEEPER, DefaultGatekeeper.class.getCanonicalName()));
        }//from   w ww .ja v a2  s. c  o  m

        sourceWriter.println(String.format(GETTER_METHOD, name, name.replaceAll("\\.", "")));
    }
}

From source file:com.gwtplatform.mvp.rebind.GinjectorGenerator.java

License:Apache License

private void writePresenterGettersFromList(SourceWriter sourceWriter, Collection<JClassType> presenters,
        String providerTypeName) {
    for (JClassType presenter : presenters) {
        String name = presenter.getQualifiedSourceName();

        sourceWriter.println();
        sourceWriter.println(//ww  w .  j a  v a2s. co  m
                String.format(GETTER_PROVIDER_METHOD, providerTypeName, name, name.replaceAll("\\.", "")));
    }
}