Java源码示例:org.apache.catalina.util.Introspection
示例1
protected static void loadMethodsAnnotation(Context context, Class<?> clazz) {
// Initialize the annotations
Method[] methods = Introspection.getDeclaredMethods(clazz);
if (methods != null && methods.length > 0) {
for (Method method : methods) {
Resource annotation = method.getAnnotation(Resource.class);
if (annotation != null) {
if (!Introspection.isValidSetter(method)) {
throw new IllegalArgumentException(sm.getString(
"webAnnotationSet.invalidInjection"));
}
String defaultName = clazz.getName() + SEPARATOR +
Introspection.getPropertyName(method);
Class<?> defaultType = (method.getParameterTypes()[0]);
addResource(context, annotation, defaultName, defaultType);
}
}
}
}
示例2
private static Method findLifecycleCallback(Method currentMethod,
String methodNameFromXml, Method method,
Class<? extends Annotation> annotation) {
Method result = currentMethod;
if (methodNameFromXml != null) {
if (method.getName().equals(methodNameFromXml)) {
if (!Introspection.isValidLifecycleCallback(method)) {
throw new IllegalArgumentException(
"Invalid " + annotation.getName() + " annotation");
}
result = method;
}
} else {
if (method.isAnnotationPresent(annotation)) {
if (currentMethod != null || !Introspection.isValidLifecycleCallback(method)) {
throw new IllegalArgumentException(
"Invalid " + annotation.getName() + " annotation");
}
result = method;
}
}
return result;
}
示例3
/**
* Process the annotations for the listeners.
*/
protected static void loadApplicationListenerAnnotations(Context context) {
Class<?> classClass = null;
String[] applicationListeners =
context.findApplicationListeners();
for (int i = 0; i < applicationListeners.length; i++) {
classClass = Introspection.loadClass(context,
applicationListeners[i]);
if (classClass == null) {
continue;
}
loadClassAnnotation(context, classClass);
loadFieldsAnnotation(context, classClass);
loadMethodsAnnotation(context, classClass);
}
}
示例4
/**
* Process the annotations for the filters.
*/
protected static void loadApplicationFilterAnnotations(Context context) {
Class<?> classClass = null;
FilterDef[] filterDefs = context.findFilterDefs();
for (int i = 0; i < filterDefs.length; i++) {
classClass = Introspection.loadClass(context,
(filterDefs[i]).getFilterClass());
if (classClass == null) {
continue;
}
loadClassAnnotation(context, classClass);
loadFieldsAnnotation(context, classClass);
loadMethodsAnnotation(context, classClass);
}
}
示例5
protected static void loadMethodsAnnotation(Context context,
Class<?> classClass) {
// Initialize the annotations
Method[] methods = Introspection.getDeclaredMethods(classClass);
if (methods != null && methods.length > 0) {
for (Method method : methods) {
Resource annotation = method.getAnnotation(Resource.class);
if (annotation != null) {
if (!Introspection.isValidSetter(method)) {
throw new IllegalArgumentException(sm.getString(
"webAnnotationSet.invalidInjection"));
}
String defaultName = classClass.getName() + SEPARATOR +
Introspection.getPropertyName(method);
Class<?> defaultType =
(method.getParameterTypes()[0]);
addResource(context, annotation, defaultName, defaultType);
}
}
}
}
示例6
private static Method findLifecycleCallback(Method currentMethod,
String methodNameFromXml, Method method,
Class<? extends Annotation> annotation) {
Method result = currentMethod;
if (methodNameFromXml != null) {
if (method.getName().equals(methodNameFromXml)) {
if (!Introspection.isValidLifecycleCallback(method)) {
throw new IllegalArgumentException(
"Invalid " + annotation.getName() + " annotation");
}
result = method;
}
} else {
if (method.isAnnotationPresent(annotation)) {
if (currentMethod != null ||
!Introspection.isValidLifecycleCallback(method)) {
throw new IllegalArgumentException(
"Invalid " + annotation.getName() + " annotation");
}
result = method;
}
}
return result;
}
示例7
/**
* Process the annotations for the listeners.
*/
protected static void loadApplicationListenerAnnotations(Context context) {
Class<?> classClass = null;
String[] applicationListeners =
context.findApplicationListeners();
for (int i = 0; i < applicationListeners.length; i++) {
classClass = Introspection.loadClass(context,
applicationListeners[i]);
if (classClass == null) {
continue;
}
loadClassAnnotation(context, classClass);
loadFieldsAnnotation(context, classClass);
loadMethodsAnnotation(context, classClass);
}
}
示例8
/**
* Process the annotations for the filters.
*/
protected static void loadApplicationFilterAnnotations(Context context) {
Class<?> classClass = null;
FilterDef[] filterDefs = context.findFilterDefs();
for (int i = 0; i < filterDefs.length; i++) {
classClass = Introspection.loadClass(context,
(filterDefs[i]).getFilterClass());
if (classClass == null) {
continue;
}
loadClassAnnotation(context, classClass);
loadFieldsAnnotation(context, classClass);
loadMethodsAnnotation(context, classClass);
}
}
示例9
protected static void loadMethodsAnnotation(Context context,
Class<?> classClass) {
// Initialize the annotations
Method[] methods = Introspection.getDeclaredMethods(classClass);
if (methods != null && methods.length > 0) {
for (Method method : methods) {
Resource annotation = method.getAnnotation(Resource.class);
if (annotation != null) {
if (!Introspection.isValidSetter(method)) {
throw new IllegalArgumentException(sm.getString(
"webAnnotationSet.invalidInjection"));
}
String defaultName = classClass.getName() + SEPARATOR +
Introspection.getPropertyName(method);
Class<?> defaultType =
(method.getParameterTypes()[0]);
addResource(context, annotation, defaultName, defaultType);
}
}
}
}
示例10
private static Method findLifecycleCallback(Method currentMethod,
String methodNameFromXml, Method method,
Class<? extends Annotation> annotation) {
Method result = currentMethod;
if (methodNameFromXml != null) {
if (method.getName().equals(methodNameFromXml)) {
if (!Introspection.isValidLifecycleCallback(method)) {
throw new IllegalArgumentException(
"Invalid " + annotation.getName() + " annotation");
}
result = method;
}
} else {
if (method.isAnnotationPresent(annotation)) {
if (currentMethod != null ||
!Introspection.isValidLifecycleCallback(method)) {
throw new IllegalArgumentException(
"Invalid " + annotation.getName() + " annotation");
}
result = method;
}
}
return result;
}
示例11
/**
* Process the annotations for the listeners.
*
* @param context The context which will have its annotations processed
*/
protected static void loadApplicationListenerAnnotations(Context context) {
String[] applicationListeners = context.findApplicationListeners();
for (String className : applicationListeners) {
Class<?> clazz = Introspection.loadClass(context, className);
if (clazz == null) {
continue;
}
loadClassAnnotation(context, clazz);
loadFieldsAnnotation(context, clazz);
loadMethodsAnnotation(context, clazz);
}
}
示例12
/**
* Process the annotations for the filters.
*
* @param context The context which will have its annotations processed
*/
protected static void loadApplicationFilterAnnotations(Context context) {
FilterDef[] filterDefs = context.findFilterDefs();
for (FilterDef filterDef : filterDefs) {
Class<?> clazz = Introspection.loadClass(context, filterDef.getFilterClass());
if (clazz == null) {
continue;
}
loadClassAnnotation(context, clazz);
loadFieldsAnnotation(context, clazz);
loadMethodsAnnotation(context, clazz);
}
}
示例13
/**
* Process the annotations for the servlets.
*
* @param context The context which will have its annotations processed
*/
protected static void loadApplicationServletAnnotations(Context context) {
Container[] children = context.findChildren();
for (Container child : children) {
if (child instanceof Wrapper) {
Wrapper wrapper = (Wrapper) child;
if (wrapper.getServletClass() == null) {
continue;
}
Class<?> clazz = Introspection.loadClass(context, wrapper.getServletClass());
if (clazz == null) {
continue;
}
loadClassAnnotation(context, clazz);
loadFieldsAnnotation(context, clazz);
loadMethodsAnnotation(context, clazz);
/* Process RunAs annotation which can be only on servlets.
* Ref JSR 250, equivalent to the run-as element in
* the deployment descriptor
*/
RunAs runAs = clazz.getAnnotation(RunAs.class);
if (runAs != null) {
wrapper.setRunAs(runAs.value());
}
// Process ServletSecurity annotation
ServletSecurity servletSecurity = clazz.getAnnotation(ServletSecurity.class);
if (servletSecurity != null) {
context.addServletSecurity(
new ApplicationServletRegistration(wrapper, context),
new ServletSecurityElement(servletSecurity));
}
}
}
}
示例14
protected static void loadFieldsAnnotation(Context context, Class<?> clazz) {
// Initialize the annotations
Field[] fields = Introspection.getDeclaredFields(clazz);
if (fields != null && fields.length > 0) {
for (Field field : fields) {
Resource annotation = field.getAnnotation(Resource.class);
if (annotation != null) {
String defaultName = clazz.getName() + SEPARATOR + field.getName();
Class<?> defaultType = field.getType();
addResource(context, annotation, defaultName, defaultType);
}
}
}
}
示例15
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();
}
示例16
/**
* Checks that the configuration of the type for the specified resource is
* consistent with any injection targets and if the type is not specified,
* tries to configure the type based on the injection targets
*
* @param resource The resource to check
*
* @return <code>true</code> if the type for the resource is now valid (if
* previously <code>null</code> this means it is now set) or
* <code>false</code> if the current resource type is inconsistent
* with the injection targets and/or cannot be determined
*/
private boolean checkResourceType(ResourceBase resource) {
if (!(container instanceof Context)) {
// Only Context's will have injection targets
return true;
}
if (resource.getInjectionTargets() == null ||
resource.getInjectionTargets().size() == 0) {
// No injection targets so use the defined type for the resource
return true;
}
Context context = (Context) container;
String typeName = resource.getType();
Class<?> typeClass = null;
if (typeName != null) {
typeClass = Introspection.loadClass(context, typeName);
if (typeClass == null) {
// Can't load the type - will trigger a failure later so don't
// fail here
return true;
}
}
Class<?> compatibleClass =
getCompatibleType(context, resource, typeClass);
if (compatibleClass == null) {
// Indicates that a compatible type could not be identified that
// worked for all injection targets
return false;
}
resource.setType(compatibleClass.getCanonicalName());
return true;
}
示例17
private Class<?> getSetterType(Class<?> clazz, String name) {
Method[] methods = Introspection.getDeclaredMethods(clazz);
if (methods != null && methods.length > 0) {
for (Method method : methods) {
if (Introspection.isValidSetter(method) &&
Introspection.getPropertyName(method).equals(name)) {
return method.getParameterTypes()[0];
}
}
}
return null;
}
示例18
private Class<?> getFieldType(Class<?> clazz, String name) {
Field[] fields = Introspection.getDeclaredFields(clazz);
if (fields != null && fields.length > 0) {
for (Field field : fields) {
if (field.getName().equals(name)) {
return field.getType();
}
}
}
return null;
}
示例19
/**
* Inject resources in specified method.
*
* @param context jndi context to extract value from
* @param instance object to inject into
* @param method field target for injection
* @param name jndi name value is bound under
* @param clazz class annotation is defined in
* @throws IllegalAccessException if method is inaccessible
* @throws javax.naming.NamingException if value is not accessible in naming context
* @throws java.lang.reflect.InvocationTargetException
* if setter call fails
*/
protected static void lookupMethodResource(Context context,
Object instance, Method method, String name, Class<?> clazz)
throws NamingException, IllegalAccessException, InvocationTargetException {
if (!Introspection.isValidSetter(method)) {
throw new IllegalArgumentException(
sm.getString("defaultInstanceManager.invalidInjection"));
}
Object lookedupResource;
boolean accessibility;
String normalizedName = normalize(name);
if ((normalizedName != null) && (normalizedName.length() > 0)) {
lookedupResource = context.lookup(normalizedName);
} else {
lookedupResource = context.lookup(
clazz.getName() + "/" + Introspection.getPropertyName(method));
}
synchronized (method) {
accessibility = method.isAccessible();
method.setAccessible(true);
method.invoke(instance, lookedupResource);
method.setAccessible(accessibility);
}
}
示例20
protected static void loadFieldsAnnotation(Context context,
Class<?> classClass) {
// Initialize the annotations
Field[] fields = Introspection.getDeclaredFields(classClass);
if (fields != null && fields.length > 0) {
for (Field field : fields) {
Resource annotation = field.getAnnotation(Resource.class);
if (annotation != null) {
String defaultName = classClass.getName() + SEPARATOR + field.getName();
Class<?> defaultType = field.getType();
addResource(context, annotation, defaultName, defaultType);
}
}
}
}
示例21
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();
}
示例22
/**
* Checks that the configuration of the type for the specified resource is
* consistent with any injection targets and if the type is not specified,
* tries to configure the type based on the injection targets
*
* @param resource The resource to check
*
* @return <code>true</code> if the type for the resource is now valid (if
* previously <code>null</code> this means it is now set) or
* <code>false</code> if the current resource type is inconsistent
* with the injection targets and/or cannot be determined
*/
private boolean checkResourceType(ResourceBase resource) {
if (!(container instanceof Context)) {
// Only Context's will have injection targets
return true;
}
if (resource.getInjectionTargets() == null ||
resource.getInjectionTargets().size() == 0) {
// No injection targets so use the defined type for the resource
return true;
}
Context context = (Context) container;
String typeName = resource.getType();
Class<?> typeClass = null;
if (typeName != null) {
typeClass = Introspection.loadClass(context, typeName);
if (typeClass == null) {
// Can't load the type - will trigger a failure later so don't
// fail here
return true;
}
}
Class<?> compatibleClass =
getCompatibleType(context, resource, typeClass);
if (compatibleClass == null) {
// Indicates that a compatible type could not be identified that
// worked for all injection targets
return false;
}
resource.setType(compatibleClass.getCanonicalName());
return true;
}
示例23
private Class<?> getSetterType(Class<?> clazz, String name) {
Method[] methods = Introspection.getDeclaredMethods(clazz);
if (methods != null && methods.length > 0) {
for (Method method : methods) {
if (Introspection.isValidSetter(method) &&
Introspection.getPropertyName(method).equals(name)) {
return method.getParameterTypes()[0];
}
}
}
return null;
}
示例24
private Class<?> getFieldType(Class<?> clazz, String name) {
Field[] fields = Introspection.getDeclaredFields(clazz);
if (fields != null && fields.length > 0) {
for (Field field : fields) {
if (field.getName().equals(name)) {
return field.getType();
}
}
}
return null;
}
示例25
/**
* Inject resources in specified method.
*
* @param context jndi context to extract value from
* @param instance object to inject into
* @param method field target for injection
* @param name jndi name value is bound under
* @param clazz class annotation is defined in
* @throws IllegalAccessException if method is inaccessible
* @throws javax.naming.NamingException if value is not accessible in naming context
* @throws java.lang.reflect.InvocationTargetException
* if setter call fails
*/
protected static void lookupMethodResource(Context context,
Object instance, Method method, String name, Class<?> clazz)
throws NamingException, IllegalAccessException, InvocationTargetException {
if (!Introspection.isValidSetter(method)) {
throw new IllegalArgumentException(
sm.getString("defaultInstanceManager.invalidInjection"));
}
Object lookedupResource;
boolean accessibility;
String normalizedName = normalize(name);
if ((normalizedName != null) && (normalizedName.length() > 0)) {
lookedupResource = context.lookup(normalizedName);
} else {
lookedupResource = context.lookup(
clazz.getName() + "/" + Introspection.getPropertyName(method));
}
synchronized (method) {
accessibility = method.isAccessible();
method.setAccessible(true);
method.invoke(instance, lookedupResource);
method.setAccessible(accessibility);
}
}
示例26
protected static void loadFieldsAnnotation(Context context,
Class<?> classClass) {
// Initialize the annotations
Field[] fields = Introspection.getDeclaredFields(classClass);
if (fields != null && fields.length > 0) {
for (Field field : fields) {
Resource annotation = field.getAnnotation(Resource.class);
if (annotation != null) {
String defaultName = classClass.getName() + SEPARATOR + field.getName();
Class<?> defaultType = field.getType();
addResource(context, annotation, defaultName, defaultType);
}
}
}
}
示例27
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();
}
示例28
/**
* Checks that the configuration of the type for the specified resource is
* consistent with any injection targets and if the type is not specified,
* tries to configure the type based on the injection targets
*
* @param resource The resource to check
*
* @return <code>true</code> if the type for the resource is now valid (if
* previously <code>null</code> this means it is now set) or
* <code>false</code> if the current resource type is inconsistent
* with the injection targets and/or cannot be determined
*/
private boolean checkResourceType(ResourceBase resource) {
if (!(container instanceof Context)) {
// Only Context's will have injection targets
return true;
}
if (resource.getInjectionTargets() == null ||
resource.getInjectionTargets().size() == 0) {
// No injection targets so use the defined type for the resource
return true;
}
Context context = (Context) container;
String typeName = resource.getType();
Class<?> typeClass = null;
if (typeName != null) {
typeClass = Introspection.loadClass(context, typeName);
if (typeClass == null) {
// Can't load the type - will trigger a failure later so don't
// fail here
return true;
}
}
Class<?> compatibleClass =
getCompatibleType(context, resource, typeClass);
if (compatibleClass == null) {
// Indicates that a compatible type could not be identified that
// worked for all injection targets
return false;
}
resource.setType(compatibleClass.getCanonicalName());
return true;
}
示例29
private Class<?> getSetterType(Class<?> clazz, String name) {
Method[] methods = Introspection.getDeclaredMethods(clazz);
if (methods != null && methods.length > 0) {
for (Method method : methods) {
if (Introspection.isValidSetter(method) &&
Introspection.getPropertyName(method).equals(name)) {
return method.getParameterTypes()[0];
}
}
}
return null;
}
示例30
private Class<?> getFieldType(Class<?> clazz, String name) {
Field[] fields = Introspection.getDeclaredFields(clazz);
if (fields != null && fields.length > 0) {
for (Field field : fields) {
if (field.getName().equals(name)) {
return field.getType();
}
}
}
return null;
}