Java Code Examples for javax.inject.Named#value()

The following examples show how to use javax.inject.Named#value() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ComponentInjector.java    From enkan with Eclipse Public License 1.0 6 votes vote down vote up
protected void injectField(Object target, Field f) {
    Named named = f.getAnnotation(Named.class);
    if (named != null) {
        String name = named.value();
        SystemComponent component = components.get(name);
        if (component != null) {
            setValueToField(target, f, component);
        } else {
            Optional<String> correctName = components.entrySet().stream()
                    .filter(c -> f.getType().isAssignableFrom(c.getValue().getClass()))
                    .map(Map.Entry::getKey)
                    .min(Comparator.comparing(n -> levenshteinDistance(n, name)));
            if (correctName.isPresent()) {
                throw new MisconfigurationException("core.INJECT_WRONG_NAMED_COMPONENT", name, correctName.get());
            } else {
                throw new MisconfigurationException("core.INJECT_WRONG_TYPE_COMPONENT", name, f.getType());
            }
        }
    } else {
        components.values().stream()
                .filter(component -> f.getType().isAssignableFrom(component.getClass()))
                .findFirst()
                .ifPresent(c -> setValueToField(target, f, c));
    }
}
 
Example 2
Source File: CallbackDescriptor.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
private CallbackEntry(Class<?> beanClass, Class<? extends Annotation> callbackMarker)
{
    this.targetBeanClass = beanClass;

    Named named = this.targetBeanClass.getAnnotation(Named.class);

    if (named != null && !"".equals(named.value()))
    {
        this.beanName = named.value();
    }
    else
    {
        //fallback to the default (which might exist) -> TODO check meta-data of Bean<T>
        this.beanName = Introspector.decapitalize(targetBeanClass.getSimpleName());
    }

    List<String> processedMethodNames = new ArrayList<String>();

    findMethodWithCallbackMarker(callbackMarker, beanClass, processedMethodNames);
}
 
Example 3
Source File: NamingConventionAwareMetadataFilter.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
public void ensureNamingConvention(@Observes ProcessAnnotatedType processAnnotatedType)
{
    Class<?> beanClass = processAnnotatedType.getAnnotatedType().getJavaClass();

    Named namedAnnotation = beanClass.getAnnotation(Named.class);
    if (namedAnnotation != null &&
            namedAnnotation.value().length() > 0 &&
            Character.isUpperCase(namedAnnotation.value().charAt(0)))
    {
        AnnotatedTypeBuilder builder = new AnnotatedTypeBuilder();
        builder.readFromType(beanClass);

        String beanName = namedAnnotation.value();
        String newBeanName = beanName.substring(0, 1).toLowerCase() + beanName.substring(1);

        builder.removeFromClass(Named.class)
                .addToClass(new NamedLiteral(newBeanName));

        processAnnotatedType.setAnnotatedType(builder.create());
    }
}
 
Example 4
Source File: AbstractInjectableElement.java    From sling-org-apache-sling-models-impl with Apache License 2.0 6 votes vote down vote up
private static String getName(AnnotatedElement element, String defaultName, InjectAnnotationProcessor2 annotationProcessor) {
    String name = null;
    if (annotationProcessor != null) {
        name = annotationProcessor.getName();
    }
    if (name == null) {
        Named namedAnnotation = element.getAnnotation(Named.class);
        if (namedAnnotation != null) {
            name = namedAnnotation.value();
        }
        else {
            name = defaultName;
        }
    }
    return name;
}
 
Example 5
Source File: AnnotationBeanDefinitionReader.java    From festival with Apache License 2.0 6 votes vote down vote up
private InjectedPoint getFieldInjectedPoint(Field field, Type type) {
    if (JSR250 && field.isAnnotationPresent(Resource.class)) {

        Resource resource = AnnotationUtils.getAnnotation(field, Resource.class);

        if (StringUtils.isEmpty(resource.name()) && resource.type() == Object.class) {
            return new InjectedPoint(field.getName(), true);
        }

        if (resource.type() == Object.class) {
            return new InjectedPoint(resource.name(), type, true);
        }

        return new InjectedPoint(resource.name(), resource.type(), true);
    }

    Named named = AnnotationUtils.getMergedAnnotation(field, Named.class);
    if (named != null) {
        return new InjectedPoint(named.value(), true);
    } else {
        return new InjectedPoint(type);
    }
}
 
Example 6
Source File: ReflectiveCapabilityBuilder.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
private String getName(Method method) {
// TODO support only @Named methods?
  Named name = method.getAnnotation(Named.class);
  if(name != null) {
  	return name.value();
  }
  com.google.inject.name.Named googleNamed = method.getAnnotation(com.google.inject.name.Named.class);
  if(googleNamed != null) {
  	return googleNamed.value();
}
  return StringUtils.capitalize(method.getName());
 }
 
Example 7
Source File: AxonCdiExtension.java    From cdi with Apache License 2.0 5 votes vote down vote up
/**
 * Scans for a serializer producer.
 *
 * @param processProducer process producer event.
 */
<T> void processSerializerProducer(
        @Observes final ProcessProducer<T, Serializer> processProducer) {
    // TODO Handle multiple serializer definitions of the same type.

    AnnotatedMember<T> annotatedMember = processProducer.getAnnotatedMember();
    Named named = annotatedMember.getAnnotation(Named.class);

    if (named != null) {
        String namedValue = named.value();
        String serializerName = "".equals(namedValue)
                ? annotatedMember.getJavaMember().getName()
                : namedValue;
        switch (serializerName) {
            case "eventSerializer":
                logger.debug("Producer for event serializer found: {}.",
                        processProducer.getProducer());
                eventSerializerProducer = processProducer.getProducer();
                break;
            case "messageSerializer":
                logger.debug("Producer for message serializer found: {}.",
                        processProducer.getProducer());
                messageSerializerProducer = processProducer.getProducer();
                break;
            case "serializer":
                logger.debug("Producer for serializer found: {}.",
                        processProducer.getProducer());
                this.serializerProducer = processProducer.getProducer();
                break;
            default:
                logger.warn("Unknown named serializer configured: " + serializerName);
        }
    } else {
        logger.debug("Producer for serializer found: {}.", processProducer.getProducer());
        this.serializerProducer = processProducer.getProducer();
    }
}
 
Example 8
Source File: CdiUtilities.java    From cdi with Apache License 2.0 5 votes vote down vote up
static String extractBeanName(AnnotatedMember<?> annotatedMember) {
    Named named = annotatedMember.getAnnotation(Named.class);

    if (named != null && !"".equals(named.value())) {
        return named.value();
    }

    // TODO: Should not try to derive the name of a member that does not
    // have the @Named annotation on it.
    return annotatedMember.getJavaMember().getName();
}
 
Example 9
Source File: ReflectionUtils.java    From sling-whiteboard with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the name of the field to look for in JCR.
 *
 * @param field
 * @return
 */
public static String getFieldName(Field field) {
    Named namedAnnotation = field.getAnnotation(Named.class);
    Via viaAnnotation = field.getAnnotation(Via.class);
    if (namedAnnotation != null) {
        return namedAnnotation.value();
    } else if (viaAnnotation != null && viaAnnotation.value() != null) {
        return viaAnnotation.value();
    } else {
        return field.getName();
    }
}
 
Example 10
Source File: CommonUtils.java    From AngularBeans with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static String getBeanName(Class targetClass) {

      if (targetClass.isAnnotationPresent(Named.class)) {
         Named named = (Named) targetClass.getAnnotation(Named.class);
         if (!named.value().isEmpty()) {
            return named.value();
         }
      }

      String name = Introspector.decapitalize(targetClass.getSimpleName());
      beanNamesHolder.put(name, targetClass);
      return name;
   }
 
Example 11
Source File: ObjectId.java    From DaggerMock with Apache License 2.0 5 votes vote down vote up
private String extractName(Named annotation) {
    if (annotation != null) {
        return annotation.value();
    } else {
        return null;
    }
}
 
Example 12
Source File: HelpPage.java    From actframework with Apache License 2.0 5 votes vote down vote up
private Map<String, String> getCmdParamInfo(CliHandlerProxy proxy, Set<String> multiLinesParams) {
    CommandExecutor executor = proxy.executor();
    Class<?> host = $.getProperty(executor, "commanderClass");
    Method method = $.getProperty(executor, "method");
    Map<String, String> map = new TreeMap<>();
    Class<?>[] paramTypes = method.getParameterTypes();
    Annotation[][] paramAnns = method.getParameterAnnotations();
    for (int i = 0; i < paramTypes.length; ++i) {
        Class<?> pt = paramTypes[i];
        Named named = _getAnno(paramAnns, i, Named.class);
        E.unexpectedIf(null == named, "Cannot find name of the param: %s.%s(%s|%s)", host.getSimpleName(), method.getName(), i, pt.getSimpleName());
        String pn = named.value();
        if (null != _getAnno(paramAnns, i, MultiLines.class)) {
            multiLinesParams.add(pn);
        }
        Required required = _getAnno(paramAnns, i, Required.class);
        if (null != required) {
            map.put(pn, helpOf(required));
        } else {
            act.cli.Optional optional = _getAnno(paramAnns, i, act.cli.Optional.class);
            if (null != optional) {
                String help = helpOf(optional);
                map.put(pn, help);
            } else {
                map.put(pn, "no help");
            }
        }
    }
    return map;
}
 
Example 13
Source File: CDIAccessor.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
public static String getName(Object o) {
    if (o == null) {
        return "<null>";
    }
    Named named = o.getClass().getAnnotation(Named.class);
    if (named != null) {
        return named.value();
    }
    return o.getClass().getSimpleName();
}
 
Example 14
Source File: SEContainer.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
public static String getName(Object o) {
    if (o == null) {
        return "<null>";
    }
    Named named = o.getClass().getAnnotation(Named.class);
    if (named != null) {
        return named.value();
    }
    return o.getClass().getSimpleName();
}
 
Example 15
Source File: CdiTestSuiteRunner.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
private static String checkForLabeledAlternativeConfig(TestControl testControl)
{
    String activeAlternativeLabel = "";

    if (testControl != null)
    {
        Class<? extends TestControl.Label> activeTypedAlternativeLabel =
                testControl.activeAlternativeLabel();

        if (!TestControl.Label.class.equals(activeTypedAlternativeLabel))
        {
            Named labelName = activeTypedAlternativeLabel.getAnnotation(Named.class);

            if (labelName != null)
            {
                activeAlternativeLabel = labelName.value();
            }
            else
            {
                String labelClassName = activeTypedAlternativeLabel.getSimpleName();
                activeAlternativeLabel = labelClassName.substring(0, 1).toLowerCase();

                if (labelClassName.length() > 1)
                {
                    activeAlternativeLabel += labelClassName.substring(1);
                }
            }
        }
    }
    return activeAlternativeLabel;
}
 
Example 16
Source File: AnnotationBeanDefinitionReader.java    From festival with Apache License 2.0 5 votes vote down vote up
private InjectedPoint getSetterInjectedPoint(Method method, Type type) {
    if (JSR250 && method.isAnnotationPresent(Resource.class)) {
        Resource resource = method.getAnnotation(Resource.class);

        String name;
        if (StringUtils.isEmpty(resource.name())) {
            name = StringUtils.makeInitialLowercase(method.getName().substring(3));
        } else {
            name = resource.name();
        }

        if (resource.type() == Object.class) {
            return new InjectedPoint(name, type, true);
        }

        return new InjectedPoint(name, resource.type(), true);
    }

    Named named = AnnotationUtils.getMergedAnnotation(method, Named.class);

    if (named != null) {

        return new InjectedPoint(named.value(), true);
    } else {
        return new InjectedPoint(type);
    }
}
 
Example 17
Source File: AbstractBeanDefinitionReader.java    From festival with Apache License 2.0 4 votes vote down vote up
private void resolveFactoryBean(Map<String, BeanDefinition> beanDefinitions, Class<?> configBeanClass) throws ResolvedException, ConflictedBeanException {
    for (Method method : configBeanClass.getDeclaredMethods()) {
        if (AnnotationUtils.isAnnotationPresent(method, Named.class)) {

            String methodName = method.getName();
            if (Modifier.isAbstract(method.getModifiers())) {
                throw new ResolvedException(String.format("method %s.%s is abstract !",
                        method.getDeclaringClass().getCanonicalName(), methodName));
            }

            if (method.getGenericReturnType().getTypeName().equals("void")) {
                throw new ResolvedException(String.format("factory method %s.%s should have a return value !",
                        method.getDeclaringClass().getCanonicalName(), methodName));
            }

            String scope = BeanDefinition.PROTOTYPE;

            if (AnnotationUtils.isAnnotationPresent(method, Singleton.class)) {
                scope = BeanDefinition.SINGLETON;
            }

            String beanName;
            Named named = AnnotationUtils.getMergedAnnotation(method, Named.class);
            if (named == null || StringUtils.isEmpty(named.value())) {
                beanName = methodName;
            } else {
                beanName = named.value();
            }

            BeanDefinition beanDefinition = BeanDefinition.builder()
                    .beanName(beanName)
                    .beanClass(method.getReturnType())
                    .scope(scope)
                    .constructor(method)
                    .build();

            if (beanDefinitions.containsKey(beanDefinition.getBeanName())) {
                throw new ConflictedBeanException(String.format("the entity named %s has conflicted ! ", beanName));
            }

            beanDefinitions.put(beanDefinition.getBeanName(), beanDefinition);
        }
    }
}
 
Example 18
Source File: AnnotationBeanDefinitionReader.java    From festival with Apache License 2.0 4 votes vote down vote up
@Override
protected String resolveBeanName(Class<?> clazz) {
    String name = "";

    Named named = AnnotationUtils.getMergedAnnotation(clazz, Named.class);

    if (named != null) {
        name = named.value();

    }

    if (StringUtils.isEmpty(name)) {

        name = getBeanNameGenerator().generateBeanName(clazz);

    }
    return name;
}