Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.apache.marmotta.platform.core.util;
 
Utilities for working with reflection

Author(s):
Sergio Fernández
 
 public class ReflectionUtils {

    
Get the actual class

Parameters:
obj target object
Returns:
actual class
 
     public static Class<?> getClass(Object obj) {
         Class<?> cls = obj.getClass();
         while (isProxied(cls)) {
             cls = cls.getSuperclass();
         }
         return cls;
     }

    
Check is the class is proxies

Parameters:
cls class
Returns:
proxied
 
     public static boolean isProxied(Class<?> cls) {
         return cls.getName().contains("$$EnhancerByCGLIB$$") ||
                 cls.getName().contains("$$FastClassByCGLIB$$") ||
                 cls.getName().contains("_$$_javassist") ||
                 cls.getName().contains("_$$_WeldSubclass") ||
                 cls.getName().contains("$Proxy$");
     }

    
Retrieve the value of the annotation

Parameters:
obj target object
annotation target annotation
field annotation field
Returns:
value (null if object not annotated)
Throws:
java.lang.NoSuchMethodException
java.lang.reflect.InvocationTargetException
java.lang.IllegalAccessException
java.lang.IllegalArgumentException
 
     public static Object getAnnotationValue(Object objClass<? extends AnnotationannotationString fieldthrows IllegalArgumentException,
         Class<?> cls = getClass(obj);
         if (cls.isAnnotationPresent(annotation))
             return invokeMethod(cls.getAnnotation(annotation), field);
         else
             return null;
     }

    
Get the method based on its name

Parameters:
cls target class
method method name
params parameters
Returns:
method
Throws:
java.lang.NoSuchMethodException
 
     public static Method getMethod(Class<?> clsString methodObject[] paramsthrows NoSuchMethodException {
         Class<?>[] classes = new Class[params.length];
         for (int i = 0; i < params.lengthi++) {
             classes[i] = params[i].getClass();
         }
         return cls.getMethod(methodclasses);
     }

    
Invoke the method without parameters over the target object

Parameters:
obj target object
method method name
Returns:
value returned by the method invocation
Throws:
java.lang.IllegalArgumentException
java.lang.IllegalAccessException
java.lang.reflect.InvocationTargetException
java.lang.NoSuchMethodException
        return invokeMethod(objmethodnew Object[0]);
    }

    
Invoke the method over the target object

Parameters:
obj target object
method method name
params parameters
Returns:
value returned by the method invocation
Throws:
java.lang.IllegalArgumentException
java.lang.IllegalAccessException
java.lang.reflect.InvocationTargetException
java.lang.NoSuchMethodException
    public static Object invokeMethod(Object objString methodObject[] paramsthrows IllegalArgumentExceptionIllegalAccessException,
        return invokeMethod(objgetMethod(getClass(obj), methodparams), params);
    }

    
Invoke the method without parameters over the target object

Parameters:
obj target object
method method
Returns:
value returned by the method invocation
Throws:
java.lang.IllegalArgumentException
java.lang.IllegalAccessException
java.lang.reflect.InvocationTargetException
    public static Object invokeMethod(Object objMethod methodthrows IllegalArgumentExceptionIllegalAccessException,
        return invokeMethod(objmethodnew Object[0]);
    }

    
Invoke the method over the target object

Parameters:
obj target object
method method
params parameters
Returns:
value returned by the method invocation
Throws:
java.lang.IllegalArgumentException
java.lang.IllegalAccessException
java.lang.reflect.InvocationTargetException
    public static Object invokeMethod(Object objMethod methodObject[] paramsthrows IllegalArgumentExceptionIllegalAccessException,
        return method.invoke(objparams);
    }
New to GrepCode? Check out our FAQ X