mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-11-17 05:52:26 +00:00
added utility class for reflection
git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@28535 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
1 parent
76560f9d5d
commit
089f66504e
1 file changed
+156
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
* The MIT License
|
||||
*
|
||||
* Copyright (c) 2004-2010, Oracle Corporation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package hudson.util;
|
||||
|
||||
import org.kohsuke.stapler.ClassDescriptor;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.AbstractList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Utility code for reflection.
|
||||
*
|
||||
* @author Kohsuke Kawaguchi
|
||||
* @since 1.351
|
||||
*/
|
||||
public class ReflectionUtils extends org.springframework.util.ReflectionUtils {
|
||||
/**
|
||||
* Finds a public method of the given name, regardless of its parameter definitions,
|
||||
*/
|
||||
public static Method getPublicMethodNamed(Class c, String methodName) {
|
||||
for( Method m : c.getMethods() )
|
||||
if(m.getName().equals(methodName))
|
||||
return m;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an object-oriented view of parameters of each type.
|
||||
*/
|
||||
public static List<Parameter> getParameters(Method m) {
|
||||
return new MethodInfo(m);
|
||||
}
|
||||
|
||||
/**
|
||||
* Most reflection operations give us properties of parameters in a batch,
|
||||
* so we use this object to store them, then {@link Parameter} will created
|
||||
* more object-oriented per-parameter view.
|
||||
*/
|
||||
private static final class MethodInfo extends AbstractList<Parameter> {
|
||||
private final Method method;
|
||||
private final Class<?>[] types;
|
||||
private Type[] genericTypes;
|
||||
private Annotation[][] annotations;
|
||||
private String[] names;
|
||||
|
||||
private MethodInfo(Method method) {
|
||||
this.method = method;
|
||||
types = method.getParameterTypes();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Parameter get(int index) {
|
||||
return new Parameter(this,index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return types.length;
|
||||
}
|
||||
|
||||
public Type[] genericTypes() {
|
||||
if (genericTypes==null)
|
||||
genericTypes = method.getGenericParameterTypes();
|
||||
return genericTypes;
|
||||
}
|
||||
|
||||
public Annotation[][] annotations() {
|
||||
if (annotations==null)
|
||||
annotations = method.getParameterAnnotations();
|
||||
return annotations;
|
||||
}
|
||||
|
||||
public String[] names() {
|
||||
if (names==null)
|
||||
names = ClassDescriptor.loadParameterNames(method);
|
||||
return names;
|
||||
}
|
||||
}
|
||||
|
||||
public static final class Parameter {
|
||||
private final MethodInfo parent;
|
||||
private final int index;
|
||||
|
||||
public Parameter(MethodInfo parent, int index) {
|
||||
this.parent = parent;
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the type of this parameter.
|
||||
*/
|
||||
public Class<?> type() {
|
||||
return parent.types[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the unerased generic type of this parameter.
|
||||
*/
|
||||
public Type genericType() {
|
||||
return parent.genericTypes()[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all the annotations on this parameter.
|
||||
*/
|
||||
public Annotation[] annotations() {
|
||||
return parent.annotations()[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the specified annotation on this parameter or null.
|
||||
*/
|
||||
public <A extends Annotation> A annotation(Class<A> type) {
|
||||
for (Annotation a : annotations())
|
||||
if (a.annotationType()==type)
|
||||
return type.cast(a);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Name of this parameter.
|
||||
*
|
||||
* If unknown, this method returns null.
|
||||
*/
|
||||
public String name() {
|
||||
String[] names = parent.names();
|
||||
if (index<names.length)
|
||||
return names[index];
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user