Search in sources :

Example 1 with Scopes

use of org.infinispan.factories.scopes.Scopes in project infinispan by infinispan.

the class Generator method writeComponentAccessor.

private void writeComponentAccessor(PrintWriter writer, Model.AnnotatedType annotatedType) {
    CharSequence simpleClassName = annotatedType.qualifiedName.substring(annotatedType.packageName.length() + 1);
    CharSequence binaryName = annotatedType.binaryName;
    Model.Component c = annotatedType.component;
    Scopes scope = c.scope.value();
    String scopeLiteral = String.valueOf(scope.ordinal());
    boolean survivesRestarts = c.survivesRestarts;
    boolean autoInstantiable = c.autoInstantiable;
    CharSequence superAccessor = stringLiteral(c.superBinaryName);
    CharSequence eagerDependencies = listLiteral(getEagerDependencies(c));
    CharSequence factoryComponents = listLiteral(c.factoryComponentNames);
    writer.printf("      builder.registerComponentAccessor(\"%s\",%n", binaryName);
    writer.printf("         %s,%n", factoryComponents);
    if (!c.hasDependenciesOrLifecycle() && !autoInstantiable) {
        // Component doesn't need an anonymous class, eagerDependencies is always empty
        writer.printf("         new ComponentAccessor<%s>(\"%s\",%n" + "            %s, %s, %s,%n" + "            %s));%n", simpleClassName, binaryName, scopeLiteral, survivesRestarts, superAccessor, eagerDependencies);
        return;
    }
    writer.printf("         new ComponentAccessor<%s>(\"%s\",%n" + "            %s, %s, %s,%n" + "            %s) {%n", simpleClassName, binaryName, scopeLiteral, survivesRestarts, superAccessor, eagerDependencies);
    if (!c.injectFields.isEmpty() || !c.injectMethods.isEmpty()) {
        writer.printf("         protected void wire(%s instance, WireContext context, boolean start) {%n", simpleClassName);
        for (Model.InjectField injectField : c.injectFields) {
            String componentType = injectField.typeName;
            CharSequence componentName = injectField.componentName;
            String lazy = injectField.isComponentRef ? "Lazy" : "";
            writer.printf("            instance.%s = context.get%s(\"%s\", %s.class, start);%n", injectField.name, lazy, componentName, componentType);
        }
        for (Model.InjectMethod injectMethod : c.injectMethods) {
            writer.printf("            instance.%s(%n", injectMethod.name);
            List<Model.InjectField> parameters = injectMethod.parameters;
            for (int i = 0; i < parameters.size(); i++) {
                Model.InjectField parameter = parameters.get(i);
                String componentType = parameter.typeName;
                CharSequence componentName = parameter.componentName;
                String lazy = parameter.isComponentRef ? "Lazy" : "";
                writer.printf("               context.get%s(\"%s\", %s.class, start)%s%n", lazy, componentName, componentType, optionalComma(i, parameters.size()));
            }
            writer.printf("            );%n");
        }
        writer.printf("         }%n");
        writer.printf("%n");
    }
    if (!c.startMethods.isEmpty() || !c.postStartMethods.isEmpty()) {
        writer.printf("         protected void start(%s instance) throws Exception {%n", simpleClassName);
        writeLifecycleMethodInvocations(writer, c.startMethods);
        writeLifecycleMethodInvocations(writer, c.postStartMethods);
        writer.printf("         }%n");
        writer.printf("%n");
    }
    if (!c.stopMethods.isEmpty()) {
        writer.printf("         protected void stop(%s instance) throws Exception {%n", simpleClassName);
        writeLifecycleMethodInvocations(writer, c.stopMethods);
        writer.printf("         }%n");
        writer.printf("%n");
    }
    if (autoInstantiable) {
        writer.printf("         protected %s newInstance() {%n", simpleClassName);
        writer.printf("            return new %s();%n", simpleClassName);
        writer.printf("         }%n");
        writer.printf("%n");
    }
    writer.printf("      });%n");
}
Also used : Scopes(org.infinispan.factories.scopes.Scopes)

Aggregations

Scopes (org.infinispan.factories.scopes.Scopes)1