Java Code Examples for javax.annotation.Resource#name()

The following examples show how to use javax.annotation.Resource#name() . These examples are extracted from open source projects. 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
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 2
private String getResourceName(InjectionPoint injectionPoint) {
    Resource resource = getResourceAnnotation(injectionPoint);
    String mappedName = resource.mappedName();
    if (!mappedName.equals("")) {
        return mappedName;
    }
    String name = resource.name();
    if (!name.equals("")) {
        return RESOURCE_LOOKUP_PREFIX + "/" + name;
    }
    String propertyName;
    if (injectionPoint.getMember() instanceof Field) {
        propertyName = injectionPoint.getMember().getName();
    } else if (injectionPoint.getMember() instanceof Method) {
        propertyName = getPropertyName((Method) injectionPoint.getMember());
        if (propertyName == null) {
            throw new IllegalArgumentException("Injection point represents a method which doesn't follow "
                    + "JavaBean conventions (unable to determine property name) " + injectionPoint);
        }
    } else {
        throw new AssertionError("Unable to inject into " + injectionPoint);
    }
    String className = injectionPoint.getMember().getDeclaringClass().getName();
    return RESOURCE_LOOKUP_PREFIX + "/" + className + "/" + propertyName;
}
 
Example 3
Source Project: Tomcat8-Source-Read   File: WebAnnotationSet.java    License: MIT License 5 votes vote down vote up
private static String getName(Resource annotation, String defaultName) {
    String name = annotation.name();
    if (name == null || name.equals("")) {
        if (defaultName != null) {
            name = defaultName;
        }
    }
    return name;
}
 
Example 4
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 5
public ResourceElement(Member member, AnnotatedElement ae, @Nullable PropertyDescriptor pd) {
	super(member, pd);
	Resource resource = ae.getAnnotation(Resource.class);
	String resourceName = resource.name();
	Class<?> resourceType = resource.type();
	this.isDefaultName = !StringUtils.hasLength(resourceName);
	if (this.isDefaultName) {
		resourceName = this.member.getName();
		if (this.member instanceof Method && resourceName.startsWith("set") && resourceName.length() > 3) {
			resourceName = Introspector.decapitalize(resourceName.substring(3));
		}
	}
	else if (embeddedValueResolver != null) {
		resourceName = embeddedValueResolver.resolveStringValue(resourceName);
	}
	if (Object.class != resourceType) {
		checkResourceType(resourceType);
	}
	else {
		// No resource type specified... check field/method.
		resourceType = getResourceType();
	}
	this.name = (resourceName != null ? resourceName : "");
	this.lookupType = resourceType;
	String lookupValue = resource.lookup();
	this.mappedName = (StringUtils.hasLength(lookupValue) ? lookupValue : resource.mappedName());
	Lazy lazy = ae.getAnnotation(Lazy.class);
	this.lazyLookup = (lazy != null && lazy.value());
}
 
Example 6
public ResourceElement(Member member, AnnotatedElement ae, @Nullable PropertyDescriptor pd) {
	super(member, pd);
	Resource resource = ae.getAnnotation(Resource.class);
	String resourceName = resource.name();
	Class<?> resourceType = resource.type();
	this.isDefaultName = !StringUtils.hasLength(resourceName);
	if (this.isDefaultName) {
		resourceName = this.member.getName();
		if (this.member instanceof Method && resourceName.startsWith("set") && resourceName.length() > 3) {
			resourceName = Introspector.decapitalize(resourceName.substring(3));
		}
	}
	else if (embeddedValueResolver != null) {
		resourceName = embeddedValueResolver.resolveStringValue(resourceName);
	}
	if (Object.class != resourceType) {
		checkResourceType(resourceType);
	}
	else {
		// No resource type specified... check field/method.
		resourceType = getResourceType();
	}
	this.name = (resourceName != null ? resourceName : "");
	this.lookupType = resourceType;
	String lookupValue = resource.lookup();
	this.mappedName = (StringUtils.hasLength(lookupValue) ? lookupValue : resource.mappedName());
	Lazy lazy = ae.getAnnotation(Lazy.class);
	this.lazyLookup = (lazy != null && lazy.value());
}
 
Example 7
Source Project: Tomcat7.0.67   File: WebAnnotationSet.java    License: Apache License 2.0 5 votes vote down vote up
private static String getName(Resource annotation, String defaultName) {
    String name = annotation.name();
    if (name == null || name.equals("")) {
        if (defaultName != null) {
            name = defaultName;
        }
    }
    return name;
}
 
Example 8
public ResourceElement(Member member, AnnotatedElement ae, PropertyDescriptor pd) {
	super(member, pd);
	Resource resource = ae.getAnnotation(Resource.class);
	String resourceName = resource.name();
	Class<?> resourceType = resource.type();
	this.isDefaultName = !StringUtils.hasLength(resourceName);
	if (this.isDefaultName) {
		resourceName = this.member.getName();
		if (this.member instanceof Method && resourceName.startsWith("set") && resourceName.length() > 3) {
			resourceName = Introspector.decapitalize(resourceName.substring(3));
		}
	}
	else if (embeddedValueResolver != null) {
		resourceName = embeddedValueResolver.resolveStringValue(resourceName);
	}
	if (resourceType != null && Object.class != resourceType) {
		checkResourceType(resourceType);
	}
	else {
		// No resource type specified... check field/method.
		resourceType = getResourceType();
	}
	this.name = resourceName;
	this.lookupType = resourceType;
	String lookupValue = (lookupAttribute != null ?
			(String) ReflectionUtils.invokeMethod(lookupAttribute, resource) : null);
	this.mappedName = (StringUtils.hasLength(lookupValue) ? lookupValue : resource.mappedName());
	Lazy lazy = ae.getAnnotation(Lazy.class);
	this.lazyLookup = (lazy != null && lazy.value());
}
 
Example 9
public ResourceElement(Member member, AnnotatedElement ae, PropertyDescriptor pd) {
	super(member, pd);
	Resource resource = ae.getAnnotation(Resource.class);
	String resourceName = resource.name();
	Class<?> resourceType = resource.type();
	this.isDefaultName = !StringUtils.hasLength(resourceName);
	if (this.isDefaultName) {
		resourceName = this.member.getName();
		if (this.member instanceof Method && resourceName.startsWith("set") && resourceName.length() > 3) {
			resourceName = Introspector.decapitalize(resourceName.substring(3));
		}
	}
	else if (beanFactory instanceof ConfigurableBeanFactory){
		resourceName = ((ConfigurableBeanFactory) beanFactory).resolveEmbeddedValue(resourceName);
	}
	if (resourceType != null && Object.class != resourceType) {
		checkResourceType(resourceType);
	}
	else {
		// No resource type specified... check field/method.
		resourceType = getResourceType();
	}
	this.name = resourceName;
	this.lookupType = resourceType;
	this.mappedName = resource.mappedName();
	Lazy lazy = ae.getAnnotation(Lazy.class);
	this.lazyLookup = (lazy != null && lazy.value());
}
 
Example 10
Source Project: tomcatsrc   File: WebAnnotationSet.java    License: Apache License 2.0 5 votes vote down vote up
private static String getName(Resource annotation, String defaultName) {
    String name = annotation.name();
    if (name == null || name.equals("")) {
        if (defaultName != null) {
            name = defaultName;
        }
    }
    return name;
}
 
Example 11
Source Project: development   File: Reference.java    License: Apache License 2.0 5 votes vote down vote up
public static Reference createFor(Resource resource, Field field) {
    final Class<?> type;
    if (!Object.class.equals(resource.type())) {
        type = resource.type();
    } else {
        type = field.getType();
    }
    final String name;
    if (resource.name().length() > 0) {
        name = resource.name();
    } else {
        name = field.getDeclaringClass().getName() + "/" + field.getName();
    }
    return new Reference(type, name, field);
}
 
Example 12
@Override
public <T> void hear(TypeLiteral<T> typeLiteral, TypeEncounter<T> typeEncounter) {
    for (Class<?> c = typeLiteral.getRawType(); c != Object.class; c = c.getSuperclass()) {
        for (Field field : typeLiteral.getRawType().getDeclaredFields()) {
            Resource resourceAnnotation = field.getAnnotation(Resource.class);
            if (resourceAnnotation != null) {
                String resourceName = resourceAnnotation.name();
                Context contextToLookup = defaultContext;
                String contextName = "default";

                // Check if this injection is from an additional context
                JndiContext jndiContextAnnotation = field.getAnnotation(JndiContext.class);
                if (jndiContextAnnotation != null) {
                    contextName = jndiContextAnnotation.value();
                    contextToLookup = jndiContexts.get(contextName);
                    if (contextToLookup == null) {
                        throw SeedException.createNew(JndiErrorCode.UNKNOWN_JNDI_CONTEXT)
                                .put("field", field)
                                .put("context", contextName);
                    }
                }

                // Register the members injector
                if (!resourceName.isEmpty()) {
                    typeEncounter.register(new ResourceMembersInjector<>(field,
                            contextToLookup,
                            contextName,
                            resourceName));
                }
            }
        }
    }
}
 
Example 13
/**
     * 获取Depend注解。
     * 如果获取不到Depend, 则尝试获取{@link javax.annotation.Resource}
     * @param from
     * @return
     */
    public static Depend getDepend(AnnotatedElement from){
//        ProxyUtils.annotationProxyByDefault()
        // 先获取depend
        final Depend depend = getAnnotation(from, Depend.class);
        if(depend != null){
            return depend;
        }else{
            try {
                final Resource resource = getAnnotation(from, Resource.class);
                if(resource == null){
                    return null;
                }else{
                    final String name = resource.name();
                    final Class<?> type = resource.type();

                    Map<String, BiFunction<Method, Object[], Object>> proxyMap = new HashMap<>();
                    proxyMap.put("value", (m, o) -> name);
                    proxyMap.put("type", (m, o) -> type);
                    proxyMap.put("equals", (m, o) -> {
                       final Object other = o[0];
                       if(other instanceof Annotation){
                           if(((Annotation) other).annotationType() != Depend.class){
                               return false;
                           }else{
                               return resource.hashCode() == other.hashCode();
                           }
                       }else{
                           return false;
                       }
                    });
                    proxyMap.put("toString", (m, o) -> "@Depend->" + resource.toString() + "("+ resource.hashCode() +")");
                    proxyMap.put("hashCode", (m, o) -> resource.hashCode());
                    proxyMap.put("annotationType", (m, o) -> Depend.class);
                    final Depend proxyDepend = ProxyUtils.annotationProxyByDefault(Depend.class, proxyMap);
                    // 计入缓存
                    boolean b = saveCache(from, proxyDepend);
                    return proxyDepend;
                }
            }catch (Throwable e){
                e.printStackTrace();
                return null;
            }
        }
    }
 
Example 14
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
    super.afterTestMethod(testContext);

    DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) testContext.getApplicationContext()
            .getAutowireCapableBeanFactory();

    /**
     * 方法结束后记录被测试对象的bean名称
     */
    Object bean = testContext.getTestInstance();
    List<Field> fields = getDeclaredFields(bean);
    for (Field field : fields) {
        InjectMocks injectMocks = field.getAnnotation(InjectMocks.class);
        if (injectMocks == null) {
            continue;
        }
        Object testedBean = null;
        String testedBeanName = null;
        /**
         * 被测试的对象如果通过spring自动注入,则记录
         * 两种注入方式 Autowired
         * Resource
         */
        if (field.getAnnotation(Autowired.class) != null) {
            Qualifier qualifier = field.getAnnotation(Qualifier.class);
            testedBean = qualifier == null ? beanFactory.getBean(field.getType())
                    : beanFactory.getBean(qualifier.value());
            testedBeanName = qualifier == null ? beanFactory.getBeanNamesForType(field.getType())[0]
                    : qualifier.value();
        } else if (field.getAnnotation(Resource.class) != null) {
            Resource resource = field.getAnnotation(Resource.class);
            Class<?> type = resource.type();
            String name = resource.name();
            if (StringUtils.isNotEmpty(name)) {
                testedBean = beanFactory.getBean(name);
                testedBeanName = name;
            } else {
                testedBean = (type != Object.class) ? beanFactory.getBean(type)
                        : beanFactory.getBean(field.getType());
                testedBeanName = (type != Object.class) ? beanFactory.getBeanNamesForType(type)[0]
                        : beanFactory.getBeanNamesForType(field.getType())[0];
            }
        }

        if (testedBean != null) {
            testedObjects.put(testedBeanName, testedBean);
        }
    }
}
 
Example 15
Source Project: redkale   File: CacheMemorySource.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public String resourceName() {
    Resource res = this.getClass().getAnnotation(Resource.class);
    return res == null ? "cachememory" : res.name();
}
 
Example 16
Source Project: redkale   File: Sncp.java    License: Apache License 2.0 4 votes vote down vote up
public static String getResourceName(Service service) {
    if (service == null) return null;
    Resource res = service.getClass().getAnnotation(Resource.class);
    return res == null ? null : res.name();
}