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

The following examples show how to use javax.annotation.Resource#type() . 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: 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 2
Source File: Reference.java    From development with 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 3
Source File: WebAnnotationSet.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private static String getType(Resource annotation, Class<?> defaultType) {
    Class<?> type = annotation.type();
    if (type == null || type.equals(Object.class)) {
        if (defaultType != null) {
            type = defaultType;
        }
    }
    return Introspection.convertPrimitiveType(type).getCanonicalName();
}
 
Example 4
Source File: WebAnnotationSet.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private static String getType(Resource annotation, Class<?> defaultType) {
    Class<?> type = annotation.type();
    if (type == null || type.equals(Object.class)) {
        if (defaultType != null) {
            type = defaultType;
        }
    }
    return Introspection.convertPrimitiveType(type).getCanonicalName();
}
 
Example 5
Source File: InjectionPlan.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isInjectionPoint(Resource resource, Class fieldType, String errorMessage, Class resourceType ) {
    Class t = resource.type();
    if (t.equals(Object.class)) {
        return fieldType.equals(resourceType);
    } else if (t.equals(resourceType)) {
        if (fieldType.isAssignableFrom(resourceType)) {
            return true;
        } else {
            // type compatibility error
            throw new WebServiceException(errorMessage);
        }
    }
    return false;
}
 
Example 6
Source File: InjectionPlan.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isInjectionPoint(Resource resource, Class fieldType, String errorMessage, Class resourceType ) {
    Class t = resource.type();
    if (t.equals(Object.class)) {
        return fieldType.equals(resourceType);
    } else if (t.equals(resourceType)) {
        if (fieldType.isAssignableFrom(resourceType)) {
            return true;
        } else {
            // type compatibility error
            throw new WebServiceException(errorMessage);
        }
    }
    return false;
}
 
Example 7
Source File: InjectionPlan.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isInjectionPoint(Resource resource, Class fieldType, String errorMessage, Class resourceType ) {
    Class t = resource.type();
    if (t.equals(Object.class)) {
        return fieldType.equals(resourceType);
    } else if (t.equals(resourceType)) {
        if (fieldType.isAssignableFrom(resourceType)) {
            return true;
        } else {
            // type compatibility error
            throw new WebServiceException(errorMessage);
        }
    }
    return false;
}
 
Example 8
Source File: InjectionPlan.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isInjectionPoint(Resource resource, Class fieldType, String errorMessage, Class resourceType ) {
    Class t = resource.type();
    if (t.equals(Object.class)) {
        return fieldType.equals(resourceType);
    } else if (t.equals(resourceType)) {
        if (fieldType.isAssignableFrom(resourceType)) {
            return true;
        } else {
            // type compatibility error
            throw new WebServiceException(errorMessage);
        }
    }
    return false;
}
 
Example 9
Source File: InjectionPlan.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isInjectionPoint(Resource resource, Class fieldType, String errorMessage, Class resourceType ) {
    Class t = resource.type();
    if (t.equals(Object.class)) {
        return fieldType.equals(resourceType);
    } else if (t.equals(resourceType)) {
        if (fieldType.isAssignableFrom(resourceType)) {
            return true;
        } else {
            // type compatibility error
            throw new WebServiceException(errorMessage);
        }
    }
    return false;
}
 
Example 10
Source File: CommonAnnotationBeanPostProcessor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
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 11
Source File: WebAnnotationSet.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private static String getType(Resource annotation, Class<?> defaultType) {
    Class<?> type = annotation.type();
    if (type == null || type.equals(Object.class)) {
        if (defaultType != null) {
            type = defaultType;
        }
    }
    return Introspection.convertPrimitiveType(type).getCanonicalName();
}
 
Example 12
Source File: InjectionPlan.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isInjectionPoint(Resource resource, Class fieldType, String errorMessage, Class resourceType ) {
    Class t = resource.type();
    if (t.equals(Object.class)) {
        return fieldType.equals(resourceType);
    } else if (t.equals(resourceType)) {
        if (fieldType.isAssignableFrom(resourceType)) {
            return true;
        } else {
            // type compatibility error
            throw new WebServiceException(errorMessage);
        }
    }
    return false;
}
 
Example 13
Source File: InjectionPlan.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isInjectionPoint(Resource resource, Class fieldType, String errorMessage, Class resourceType ) {
    Class t = resource.type();
    if (t.equals(Object.class)) {
        return fieldType.equals(resourceType);
    } else if (t.equals(resourceType)) {
        if (fieldType.isAssignableFrom(resourceType)) {
            return true;
        } else {
            // type compatibility error
            throw new WebServiceException(errorMessage);
        }
    }
    return false;
}
 
Example 14
Source File: CommonAnnotationBeanPostProcessor.java    From java-technology-stack with MIT License 5 votes vote down vote up
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 15
Source File: InjectionPlan.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isInjectionPoint(Resource resource, Class fieldType, String errorMessage, Class resourceType ) {
    Class t = resource.type();
    if (t.equals(Object.class)) {
        return fieldType.equals(resourceType);
    } else if (t.equals(resourceType)) {
        if (fieldType.isAssignableFrom(resourceType)) {
            return true;
        } else {
            // type compatibility error
            throw new WebServiceException(errorMessage);
        }
    }
    return false;
}
 
Example 16
Source File: CommonAnnotationBeanPostProcessor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
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 17
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 18
Source File: UnitTestDependencyInjectionTestExecutionListener.java    From wisp with Apache License 2.0 4 votes vote down vote up
@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 19
Source File: AnnotationUtils.java    From simple-robot-core with Apache License 2.0 4 votes vote down vote up
/**
     * 获取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 20
Source File: ResourceInjector.java    From cxf with Apache License 2.0 4 votes vote down vote up
private Class<?> getResourceType(Resource res, Method method) {
    return res.type() != null && !Object.class.equals(res.type())
        ? res.type()
        : method.getParameterTypes()[0];
}