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

The following examples show how to use javax.annotation.Resource#type() . 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
Source Project: Tomcat8-Source-Read   File: WebAnnotationSet.java    License: 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 3
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 4
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 5
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
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
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
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 Project: Tomcat7.0.67   File: WebAnnotationSet.java    License: 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 10
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
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 12
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
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
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 15
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 Project: tomcatsrc   File: WebAnnotationSet.java    License: 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 17
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 18
/**
     * 获取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 19
@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 20
Source Project: cxf   File: ResourceInjector.java    License: 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];
}