Package com.asakusafw.utils.java.model.syntax

Examples of com.asakusafw.utils.java.model.syntax.ModelFactory


    }

    private Annotation toKey(EmitContext context, ReduceTerm<?> term) {
        assert context != null;
        assert term != null;
        ModelFactory f = context.getModelFactory();
        List<Literal> properties = Lists.create();
        Map<String, PropertySymbol> reverseMapping = Maps.create();
        for (MappingFactor mapping : term.getMappings()) {
            reverseMapping.put(mapping.getTarget().getName().identifier, mapping.getSource());
        }
        for (PropertySymbol property : term.getGrouping()) {
            PropertySymbol origin = reverseMapping.get(property.getName().identifier);
            assert origin != null;
            PropertyDeclaration decl = origin.findDeclaration();
            properties.add(Models.toLiteral(f, context.getFieldName(decl).getToken()));
        }
        return new AttributeBuilder(f)
            .annotation(context.resolve(Key.class),
                    "group", f.newArrayInitializer(properties)) //$NON-NLS-1$
            .toAnnotations()
            .get(0);
    }
View Full Code Here


    }

    private MethodDeclaration createToString(EmitContext context, ModelDeclaration model) {
        assert context != null;
        assert model != null;
        ModelFactory f = context.getModelFactory();
        List<Statement> statements = Lists.create();
        SimpleName buffer = context.createVariableName("result"); //$NON-NLS-1$
        statements.add(new TypeBuilder(f, context.resolve(StringBuilder.class))
            .newObject()
            .toLocalVariableDeclaration(context.resolve(StringBuilder.class), buffer));
        statements.add(new ExpressionBuilder(f, buffer)
            .method("append", Models.toLiteral(f, "{")) //$NON-NLS-1$ //$NON-NLS-2$
            .toStatement());
        statements.add(new ExpressionBuilder(f, buffer)
            .method("append", Models.toLiteral(f, "class=" + model.getName().identifier)) //$NON-NLS-1$ //$NON-NLS-2$
            .toStatement());
        for (PropertyDeclaration property : model.getDeclaredProperties()) {
            statements.add(new ExpressionBuilder(f, buffer)
                .method("append", //$NON-NLS-1$
                        Models.toLiteral(f,
                                MessageFormat.format(", {0}=", context.getFieldName(property)))) //$NON-NLS-1$
                .toStatement());
            statements.add(new ExpressionBuilder(f, buffer)
                .method("append", new ExpressionBuilder(f, f.newThis()) //$NON-NLS-1$
                    .field(context.getFieldName(property))
                    .toExpression())
                .toStatement());
        }
        statements.add(new ExpressionBuilder(f, buffer)
            .method("append", Models.toLiteral(f, "}")) //$NON-NLS-1$ //$NON-NLS-2$
            .toStatement());
        statements.add(new ExpressionBuilder(f, buffer)
            .method("toString") //$NON-NLS-1$
            .toReturnStatement());
        return f.newMethodDeclaration(
                null,
                new AttributeBuilder(f)
                    .annotation(context.resolve(Override.class))
                    .Public()
                    .toAttributes(),
                context.resolve(String.class),
                f.newSimpleName("toString"), //$NON-NLS-1$
                Collections.<FormalParameterDeclaration>emptyList(),
                statements);
    }
View Full Code Here

    }

    private MethodDeclaration createHashCode(EmitContext context, ModelDeclaration model) {
        assert context != null;
        assert model != null;
        ModelFactory f = context.getModelFactory();
        List<Statement> statements = Lists.create();
        SimpleName prime = context.createVariableName("prime"); //$NON-NLS-1$
        SimpleName result = context.createVariableName("result"); //$NON-NLS-1$
        statements.add(new ExpressionBuilder(f, Models.toLiteral(f, 31))
            .toLocalVariableDeclaration(Models.toType(f, int.class), prime));
        statements.add(new ExpressionBuilder(f, Models.toLiteral(f, 1))
            .toLocalVariableDeclaration(Models.toType(f, int.class), result));
        for (PropertyDeclaration property : model.getDeclaredProperties()) {
            SimpleName field = context.getFieldName(property);
            statements.add(new ExpressionBuilder(f, result)
                .assignFrom(new ExpressionBuilder(f, prime)
                    .apply(InfixOperator.TIMES, result)
                    .apply(InfixOperator.PLUS, new ExpressionBuilder(f, field)
                        .method("hashCode") //$NON-NLS-1$
                        .toExpression())
                    .toExpression())
                .toStatement());
        }
        statements.add(f.newReturnStatement(result));

        return f.newMethodDeclaration(
                null,
                new AttributeBuilder(f)
                    .annotation(context.resolve(Override.class))
                    .Public()
                    .toAttributes(),
                Models.toType(f, int.class),
                f.newSimpleName("hashCode"), //$NON-NLS-1$
                Collections.<FormalParameterDeclaration>emptyList(),
                statements);
    }
View Full Code Here

    }

    private MethodDeclaration createEquals(EmitContext context, ModelDeclaration model) {
        assert context != null;
        assert model != null;
        ModelFactory f = context.getModelFactory();
        List<Statement> statements = Lists.create();
        SimpleName obj = context.createVariableName("obj"); //$NON-NLS-1$
        statements.add(f.newIfStatement(
                new ExpressionBuilder(f, f.newThis())
                    .apply(InfixOperator.EQUALS, obj)
                    .toExpression(),
                f.newBlock(f.newReturnStatement(Models.toLiteral(f, true)))));
        statements.add(f.newIfStatement(
                new ExpressionBuilder(f, obj)
                    .apply(InfixOperator.EQUALS, Models.toNullLiteral(f))
                    .toExpression(),
                f.newBlock(f.newReturnStatement(Models.toLiteral(f, false)))));
        statements.add(f.newIfStatement(
                new ExpressionBuilder(f, f.newThis())
                    .method("getClass") //$NON-NLS-1$
                    .apply(InfixOperator.NOT_EQUALS, new ExpressionBuilder(f, obj)
                        .method("getClass") //$NON-NLS-1$
                        .toExpression())
                    .toExpression(),
                f.newBlock(f.newReturnStatement(Models.toLiteral(f, false)))));
        SimpleName other = context.createVariableName("other"); //$NON-NLS-1$
        Type self = context.resolve(context.getQualifiedTypeName());
        statements.add(new ExpressionBuilder(f, obj)
            .castTo(self)
            .toLocalVariableDeclaration(self, other));
        for (PropertyDeclaration property : model.getDeclaredProperties()) {
            SimpleName field = context.getFieldName(property);
            statements.add(f.newIfStatement(
                    new ExpressionBuilder(f, f.newThis())
                        .field(field)
                        .method("equals", new ExpressionBuilder(f, other) //$NON-NLS-1$
                            .field(field)
                            .toExpression())
                        .apply(InfixOperator.EQUALS, Models.toLiteral(f, false))
                        .toExpression(),
                    f.newBlock(f.newReturnStatement(Models.toLiteral(f, false)))));
        }
        statements.add(f.newReturnStatement(Models.toLiteral(f, true)));

        return f.newMethodDeclaration(
                null,
                new AttributeBuilder(f)
                    .annotation(context.resolve(Override.class))
                    .Public()
                    .toAttributes(),
                Models.toType(f, boolean.class),
                f.newSimpleName("equals"), //$NON-NLS-1$
                Collections.singletonList(f.newFormalParameterDeclaration(
                        context.resolve(Object.class),
                        obj)),
                statements);
    }
View Full Code Here

        File outputDirectory = new File(output);
        DmdlSourceRepository source = buildRepository(parseFileList(sourcePaths), sourceEnc);
        ClassLoader serviceLoader = buildPluginLoader(Main.class.getClassLoader(), parseFileList(plugin));

        ModelFactory factory = Models.getModelFactory();
        return new Configuration(
                factory,
                source,
                Models.toName(factory, packageName),
                new Filer(outputDirectory, targetEnc),
View Full Code Here

        }
        return compiler.getClassLoader();
    }

    private List<VolatileJavaFile> emit(String name) {
        ModelFactory factory = Models.getModelFactory();
        DmdlSourceRepository source = collectInput(name);
        VolatileEmitter emitter = new VolatileEmitter();
        Configuration conf = new Configuration(
                factory,
                source,
View Full Code Here

        assert slot != null;
        assert key != null;
        assert value != null;
        assert index >= 0;
        assert requiresReducer(slot);
        ModelFactory f = environment.getModelFactory();
        SimpleName className = f.newSimpleName(Naming.getMapClass(index));
        ImportBuilder importer = new ImportBuilder(
                f,
                f.newPackageDeclaration(environment.getEpiloguePackageName(moduleId)),
                Strategy.TOP_LEVEL);
        importer.resolvePackageMember(className);
        List<Expression> arguments = Lists.create();
        arguments.add(Models.toLiteral(f, index));
        arguments.add(classLiteralOrNull(f, importer, key));
        arguments.add(classLiteralOrNull(f, importer, value));
        return emitConstructorClass(
                className,
                f.newParameterizedType(
                        importer.toType(AbstractDirectOutputMapper.class),
                        importer.toType(slot.valueType)),
                importer,
                arguments);
    }
View Full Code Here

    private Name emitOutputMapper(Slot slot, int index) throws IOException {
        assert slot != null;
        assert index >= 0;
        assert requiresReducer(slot) == false;
        ModelFactory f = environment.getModelFactory();
        SimpleName className = f.newSimpleName(Naming.getMapClass(index));
        ImportBuilder importer = new ImportBuilder(
                f,
                f.newPackageDeclaration(environment.getEpiloguePackageName(moduleId)),
                Strategy.TOP_LEVEL);
        importer.resolvePackageMember(className);
        List<Expression> arguments = Lists.create();
        arguments.add(f.newClassLiteral(importer.toType(slot.valueType)));
        arguments.add(Models.toLiteral(f, slot.basePath));
        arguments.add(Models.toLiteral(f, slot.resourcePath));
        arguments.add(f.newClassLiteral(importer.toType(slot.formatClass)));

        return emitConstructorClass(
                className,
                f.newParameterizedType(
                        importer.toType(AbstractNoReduceDirectOutputMapper.class),
                        importer.toType(slot.valueType)),
                importer,
                arguments);
    }
View Full Code Here

    private Name emitWithSpecs(String classNameString, Class<?> baseClass, List<Slot> slots) throws IOException {
        assert classNameString != null;
        assert baseClass != null;
        assert slots != null;
        ModelFactory f = environment.getModelFactory();
        SimpleName className = f.newSimpleName(classNameString);
        ImportBuilder importer = new ImportBuilder(
                f,
                f.newPackageDeclaration(environment.getEpiloguePackageName(moduleId)),
                Strategy.TOP_LEVEL);
        importer.resolvePackageMember(className);
        List<Expression> elements = Lists.create();
        for (Slot slot : slots) {
            if (requiresReducer(slot)) {
                List<Expression> arguments = Lists.create();
                arguments.add(f.newClassLiteral(importer.toType(slot.valueType)));
                arguments.add(Models.toLiteral(f, slot.basePath));
                arguments.add(f.newClassLiteral(importer.toType(slot.formatClass)));
                arguments.add(f.newClassLiteral(importer.toType(slot.namingClass)));
                arguments.add(f.newClassLiteral(importer.toType(slot.orderClass)));
                elements.add(new TypeBuilder(f, importer.toType(DirectOutputSpec.class))
                    .newObject(arguments)
                    .toExpression());
            } else {
                elements.add(Models.toNullLiteral(f));
            }
        }
        return emitConstructorClass(
                className,
                importer.toType(baseClass),
                importer,
                Collections.singletonList(f.newArrayCreationExpression(
                        (ArrayType) importer.toType(DirectOutputSpec[].class),
                        f.newArrayInitializer(elements))));
    }
View Full Code Here

            Class<?> baseClass,
            Name argumentClassName) throws IOException {
        assert classNameString != null;
        assert baseClass != null;
        assert argumentClassName != null;
        ModelFactory f = environment.getModelFactory();
        SimpleName className = f.newSimpleName(classNameString);
        ImportBuilder importer = new ImportBuilder(
                f,
                f.newPackageDeclaration(environment.getEpiloguePackageName(moduleId)),
                Strategy.TOP_LEVEL);
        importer.resolvePackageMember(className);
        List<Expression> arguments = Lists.create();
        arguments.add(classLiteralOrNull(f, importer, argumentClassName));
        return emitConstructorClass(className, importer.toType(baseClass), importer, arguments);
View Full Code Here

TOP

Related Classes of com.asakusafw.utils.java.model.syntax.ModelFactory

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.