Search in sources :

Example 31 with Named

use of javax.inject.Named in project xwiki-platform by xwiki.

the class AbstractHibernateDataMigration method getName.

@Override
public String getName() {
    String hint = null;
    Named named = this.getClass().getAnnotation(Named.class);
    if (named != null) {
        hint = named.value();
    }
    return hint;
}
Also used : Named(javax.inject.Named)

Example 32 with Named

use of javax.inject.Named in project portfolio by buchen.

the class OpenBookmarksHandler method execute.

@Execute
public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart part, @Named(IServiceConstants.ACTIVE_SHELL) Shell shell, ESelectionService selectionService) {
    Object object = selectionService.getSelection();
    if (!(object instanceof SecuritySelection))
        return;
    SecuritySelection selection = (SecuritySelection) object;
    if (selection.getClient().getSettings().getBookmarks().isEmpty())
        return;
    List<Bookmark> bookmarks = selection.getClient().getSettings().getBookmarks().stream().filter(b -> !b.isSeparator()).collect(Collectors.toList());
    BookmarkPopup<Bookmark> popup = new // 
    BookmarkPopup<>(// 
    shell, // 
    selection.getSecurity().getName(), // 
    bookmarks, // 
    Bookmark::getLabel, bm -> DesktopAPI.browse(bm.constructURL(selection.getClient(), selection.getSecurity())));
    popup.open();
}
Also used : IServiceConstants(org.eclipse.e4.ui.services.IServiceConstants) TableViewer(org.eclipse.jface.viewers.TableViewer) Execute(org.eclipse.e4.core.di.annotations.Execute) Function(java.util.function.Function) TableColumnLayout(org.eclipse.jface.layout.TableColumnLayout) ColumnPixelData(org.eclipse.jface.viewers.ColumnPixelData) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) MPart(org.eclipse.e4.ui.model.application.ui.basic.MPart) ESelectionService(org.eclipse.e4.ui.workbench.modeling.ESelectionService) Composite(org.eclipse.swt.widgets.Composite) Named(javax.inject.Named) Shell(org.eclipse.swt.widgets.Shell) GridDataFactory(org.eclipse.jface.layout.GridDataFactory) ArrayContentProvider(org.eclipse.jface.viewers.ArrayContentProvider) ColumnLabelProvider(org.eclipse.jface.viewers.ColumnLabelProvider) DesktopAPI(name.abuchen.portfolio.ui.util.DesktopAPI) Collectors(java.util.stream.Collectors) TableViewerColumn(org.eclipse.jface.viewers.TableViewerColumn) GridLayoutFactory(org.eclipse.jface.layout.GridLayoutFactory) Consumer(java.util.function.Consumer) PopupDialog(org.eclipse.jface.dialogs.PopupDialog) List(java.util.List) Bookmark(name.abuchen.portfolio.model.Bookmark) SWT(org.eclipse.swt.SWT) SecuritySelection(name.abuchen.portfolio.ui.selection.SecuritySelection) SelectionEvent(org.eclipse.swt.events.SelectionEvent) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Control(org.eclipse.swt.widgets.Control) SelectionListener(org.eclipse.swt.events.SelectionListener) SecuritySelection(name.abuchen.portfolio.ui.selection.SecuritySelection) Bookmark(name.abuchen.portfolio.model.Bookmark) Execute(org.eclipse.e4.core.di.annotations.Execute)

Example 33 with Named

use of javax.inject.Named in project unleash-maven-plugin by shillner.

the class AbstractUnleashMojo method getUnleashOutputFolder.

@MojoProduces
@Named("unleashOutputFolder")
private File getUnleashOutputFolder() {
    File folder = new File(this.project.getBuild().getDirectory(), "unleash");
    folder.mkdirs();
    return folder;
}
Also used : File(java.io.File) Named(javax.inject.Named) MojoProduces(com.itemis.maven.plugins.cdi.annotations.MojoProduces)

Example 34 with Named

use of javax.inject.Named in project fabric8 by fabric8io.

the class KubernetesModelProcessorProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    CompilationTaskFactory compilationTaskFactory = new CompilationTaskFactory(processingEnv);
    Set<TypeElement> processors = new HashSet<>();
    // 1st pass collect classes to compile.
    for (Element element : roundEnv.getElementsAnnotatedWith(KubernetesModelProcessor.class)) {
        processors.add(getClassElement(element));
    }
    if (processors.isEmpty()) {
        return true;
    }
    StringWriter writer = new StringWriter();
    try {
        Callable<Boolean> compileTask = compilationTaskFactory.create(processors, writer);
        if (!compileTask.call()) {
            processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Failed to compile provider classes. See output below.");
            printCompileErrors(compilationTaskFactory);
            return false;
        }
    } catch (Exception e) {
        processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error to compile provider classes, due to: " + e.getMessage() + ". See output below.");
        return false;
    } finally {
        String output = writer.toString();
        if (Strings.isNullOrBlank(output)) {
            output = "success";
        }
        processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Fabric8 model generator compiler output:" + output);
    }
    // 2nd pass generate json.
    for (Element element : roundEnv.getElementsAnnotatedWith(KubernetesModelProcessor.class)) {
        KubernetesModelProcessor annotation = element.getAnnotation(KubernetesModelProcessor.class);
        String kubernetesJsonFileName = annotation.value();
        KubernetesResource json = readJson(kubernetesJsonFileName);
        Builder<? extends KubernetesResource> builder;
        if (json instanceof KubernetesList) {
            builder = new KubernetesListBuilder((KubernetesList) json);
        } else if (json instanceof Template) {
            builder = new TemplateBuilder((Template) json);
        } else if (json != null) {
            processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Unknown Kubernetes json type:" + json.getClass());
            return false;
        } else {
            return false;
        }
        try {
            if (element instanceof TypeElement) {
                for (ExecutableElement methodElement : ElementFilter.methodsIn(element.getEnclosedElements())) {
                    TypeElement classElement = getClassElement(element);
                    Class<?> cls = Class.forName(classElement.getQualifiedName().toString());
                    final Object instance = cls.newInstance();
                    final String methodName = methodElement.getSimpleName().toString();
                    if (builder instanceof Visitable) {
                        ((Visitable) builder).accept(new Visitor() {

                            @Override
                            public void visit(Object o) {
                                for (Method m : findMethods(instance, methodName, o.getClass())) {
                                    Named named = m.getAnnotation(Named.class);
                                    if (named != null && !Strings.isNullOrBlank(named.value())) {
                                        String objectName = getName(o);
                                        // If a name has been explicitly specified check if there is a match
                                        if (!named.value().equals(objectName)) {
                                            processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "Named method:" + m.getName() + " with name:" + named.value() + " doesn't match: " + objectName + ", ignoring");
                                            return;
                                        }
                                    }
                                    try {
                                        m.invoke(instance, o);
                                    } catch (IllegalAccessException e) {
                                        processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error invoking visitor method:" + m.getName() + " on:" + instance + "with argument:" + o);
                                    } catch (InvocationTargetException e) {
                                        processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error invoking visitor method:" + m.getName() + " on:" + instance + "with argument:" + o);
                                    }
                                }
                            }
                        });
                    } else {
                        processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Json type is not visitable.");
                    }
                }
            }
            json = builder.build();
            generateJson(kubernetesJsonFileName, json);
        } catch (Exception ex) {
            processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error creating Kubernetes configuration:" + ex.getMessage());
        }
    }
    return true;
}
Also used : Visitor(io.fabric8.kubernetes.api.builder.Visitor) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TemplateBuilder(io.fabric8.openshift.api.model.TemplateBuilder) ExecutableElement(javax.lang.model.element.ExecutableElement) Visitable(io.fabric8.kubernetes.api.builder.Visitable) KubernetesModelProcessor(io.fabric8.kubernetes.generator.annotation.KubernetesModelProcessor) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) Template(io.fabric8.openshift.api.model.Template) StringWriter(java.io.StringWriter) KubernetesResource(io.fabric8.kubernetes.api.model.KubernetesResource) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) Named(javax.inject.Named) TypeElement(javax.lang.model.element.TypeElement) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 35 with Named

use of javax.inject.Named in project mule by mulesoft.

the class SimpleRegistry method injectInto.

private <T> T injectInto(T object) {
    for (Field field : getAllFields(object.getClass(), withAnnotation(Inject.class))) {
        Class<?> dependencyType = field.getType();
        boolean nullToOptional = false;
        if (dependencyType.equals(Optional.class)) {
            Type type = ((ParameterizedType) (field.getGenericType())).getActualTypeArguments()[0];
            if (type instanceof ParameterizedType) {
                dependencyType = (Class<?>) ((ParameterizedType) type).getRawType();
            } else {
                dependencyType = (Class<?>) type;
            }
            nullToOptional = true;
        }
        Named nameAnnotation = field.getAnnotation(Named.class);
        try {
            field.setAccessible(true);
            Object dependency = resolveObjectToInject(dependencyType, nameAnnotation != null ? nameAnnotation.value() : null, nullToOptional);
            if (dependency != null) {
                field.set(object, dependency);
            }
        } catch (Exception e) {
            throw new RuntimeException(format("Could not inject dependency on field %s of type %s", field.getName(), object.getClass().getName()), e);
        }
    }
    for (Method method : getAllMethods(object.getClass(), withAnnotation(Inject.class))) {
        if (method.getParameters().length == 1) {
            Class<?> dependencyType = method.getParameterTypes()[0];
            boolean nullToOptional = false;
            if (dependencyType.equals(Optional.class)) {
                Type type = ((ParameterizedType) (method.getGenericParameterTypes()[0])).getActualTypeArguments()[0];
                if (type instanceof ParameterizedType) {
                    dependencyType = (Class<?>) ((ParameterizedType) type).getRawType();
                } else {
                    dependencyType = (Class<?>) type;
                }
                nullToOptional = true;
            }
            Named nameAnnotation = method.getAnnotation(Named.class);
            try {
                Object dependency = resolveObjectToInject(dependencyType, nameAnnotation != null ? nameAnnotation.value() : null, nullToOptional);
                if (dependency != null) {
                    method.invoke(object, dependency);
                }
            } catch (Exception e) {
                throw new RuntimeException(format("Could not inject dependency on method %s of type %s", method.getName(), object.getClass().getName()), e);
            }
        }
    }
    return object;
}
Also used : Inject(javax.inject.Inject) ParameterizedType(java.lang.reflect.ParameterizedType) Field(java.lang.reflect.Field) Named(javax.inject.Named) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Method(java.lang.reflect.Method) RegistrationException(org.mule.runtime.core.privileged.registry.RegistrationException) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) MuleException(org.mule.runtime.api.exception.MuleException)

Aggregations

Named (javax.inject.Named)126 Produces (javax.enterprise.inject.Produces)36 Test (org.junit.Test)29 ApplicationScoped (javax.enterprise.context.ApplicationScoped)26 Provides (com.google.inject.Provides)22 Singleton (javax.inject.Singleton)17 Properties (java.util.Properties)16 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)15 Api (com.google.api.server.spi.config.Api)15 SimpleLevelOverridingApi (com.google.api.server.spi.testing.SimpleLevelOverridingApi)15 JsonNode (com.fasterxml.jackson.databind.JsonNode)14 PropertiesComponent (org.apache.camel.component.properties.PropertiesComponent)12 Annotation (java.lang.annotation.Annotation)11 Provides (dagger.Provides)9 ArrayList (java.util.ArrayList)8 List (java.util.List)7 Inject (javax.inject.Inject)7 HashMap (java.util.HashMap)6 ApiConfigAnnotationReader (com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader)4 Type (java.lang.reflect.Type)4