Example usage for java.lang Class getClass

List of usage examples for java.lang Class getClass

Introduction

In this page you can find the example usage for java.lang Class getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:com.hurence.logisland.classloading.PluginProxy.java

/**
 * Creates a proxy. Useful tu kind of 'tunnel' object from a classloader to another one.
 * Please beware linkage errors./* www  .  j  ava  2 s. c o  m*/
 *
 * @param object the object to proxy.
 * @return the proxied object.
 */
public static <T> T create(T object) {

    Class<?> superClass = null;

    // Check class
    Class<?> currentCls = object.getClass();
    while (superClass == null) {
        try {
            Class.forName(currentCls.getSuperclass().getName());
            superClass = currentCls.getSuperclass();
        } catch (ClassNotFoundException e) {
            currentCls = currentCls.getSuperclass();
        }
    }

    Class[] interfaces = getAllInterfaces(object);

    List<Class<?>> il = new ArrayList<>();

    // Check available interfaces
    for (Class<?> i : interfaces) {
        try {
            Class.forName(i.getClass().getName());
            il.add(i);
        } catch (ClassNotFoundException e) {
        }
    }

    if (superClass == null && il.size() == 0) {
        return object;
    }

    return (T) createProxy(object, superClass, il.toArray(new Class[il.size()]), null);
}

From source file:Main.java

public static void dumpMethod(final Method method) {
    final StringBuilder builder = new StringBuilder();
    builder.append("------------------------------\n");
    builder.append("MethodName: ").append(method.getName()).append("\n");
    builder.append("ParameterTypes:{");
    for (Class<?> cls : method.getParameterTypes()) {
        builder.append(cls.getName()).append(", ");
    }//from  w w  w  .ja  v a  2  s . co  m
    builder.append("}\n");
    builder.append("GenericParameterTypes:{");
    for (Type cls : method.getGenericParameterTypes()) {
        builder.append(cls.getClass()).append(", ");
    }
    builder.append("}\n");
    builder.append("TypeParameters:{");
    for (TypeVariable<Method> cls : method.getTypeParameters()) {
        builder.append(cls.getName()).append(", ");
    }
    builder.append("}\n");
    builder.append("DeclaredAnnotations:{");
    for (Annotation cls : method.getDeclaredAnnotations()) {
        builder.append(cls).append(", ");
    }
    builder.append("}\n");
    builder.append("Annotations:{");
    for (Annotation cls : method.getAnnotations()) {
        builder.append(cls).append(", ");
    }
    builder.append("}\n");
    builder.append("ExceptionTypes:{");
    for (Class<?> cls : method.getExceptionTypes()) {
        builder.append(cls.getName()).append(", ");
        ;
    }
    builder.append("}\n");
    builder.append("ReturnType: ").append(method.getReturnType());
    builder.append("\nGenericReturnType: ").append(method.getGenericReturnType());
    builder.append("\nDeclaringClass: ").append(method.getDeclaringClass());
    builder.append("\n");

    System.out.println(builder.toString());
}

From source file:org.trend.hgraph.mapreduce.pagerank.Utils.java

static HTable initTable(Configuration conf, String tableKey, @SuppressWarnings("rawtypes") Class clazz)
        throws IOException {
    HTable table = null;/*from w w w .  j  a  va2s.c  om*/
    String tableName = conf.get(tableKey);
    Validate.notEmpty(tableName,
            tableKey + " shall be set before running this task:" + clazz.getClass().getName());
    try {
        table = new HTable(conf, tableName);
    } catch (IOException e) {
        System.err.println("initial table:" + tableName + " failed");
        e.printStackTrace(System.err);
        throw e;
    }
    return table;
}

From source file:org.seasar.dbflute.cbean.ConditionBeanContext.java

public static void loadCoolClasses() {
    boolean debugEnabled = false; // If you watch the log, set this true.
    // Against the ClassLoader Headache for S2Container's HotDeploy!
    // However, These classes are in Library since 0.9.0
    // so this process may not be needed...
    final StringBuilder sb = new StringBuilder();
    {/*from w w  w  .  j a  v  a2s  .  c  om*/
        final Class<?> clazz = org.seasar.dbflute.cbean.SimplePagingBean.class;
        if (debugEnabled) {
            sb.append("  ...Loading class of " + clazz.getName() + " by " + clazz.getClassLoader().getClass())
                    .append(ln());
        }
    }
    {
        loadClass(org.seasar.dbflute.AccessContext.class);
        loadClass(org.seasar.dbflute.CallbackContext.class);
        loadClass(org.seasar.dbflute.cbean.EntityRowHandler.class);
        loadClass(org.seasar.dbflute.cbean.coption.FromToOption.class);
        loadClass(org.seasar.dbflute.cbean.coption.LikeSearchOption.class);
        loadClass(org.seasar.dbflute.cbean.grouping.GroupingOption.class);
        loadClass(org.seasar.dbflute.cbean.grouping.GroupingRowEndDeterminer.class);
        loadClass(org.seasar.dbflute.cbean.grouping.GroupingRowResource.class);
        loadClass(org.seasar.dbflute.cbean.grouping.GroupingRowSetupper.class);
        loadClass(org.seasar.dbflute.cbean.pagenavi.PageNumberLink.class);
        loadClass(org.seasar.dbflute.cbean.pagenavi.PageNumberLinkSetupper.class);
        loadClass(org.seasar.dbflute.jdbc.CursorHandler.class);
        if (debugEnabled) {
            sb.append("  ...Loading class of ...and so on");
        }
    }
    if (debugEnabled) {
        _log.debug("{Initialize against the ClassLoader Headache}" + ln() + sb);
    }
}

From source file:com.centeractive.ws.builder.utils.ResourceUtils.java

public static URL getResourceWithAbsolutePackagePath(Class<?> clazz, String absolutePackagePath,
        String resourceName) {// ww  w . j av  a 2s  . c o  m
    checkNotNull(clazz, "clazz cannot be null");
    String resourcePath = getResourcePath(absolutePackagePath, resourceName);
    URL resource = null;
    // first attempt - outside/inside jar file
    resource = clazz.getClass().getResource(resourcePath);
    // second attempt - servlet container - inside application lib folder
    if (resource == null) {
        if (resourcePath.charAt(0) == '/') {
            String resourcePathWithoutLeadingSlash = resourcePath.substring(1);
            resource = Thread.currentThread().getContextClassLoader()
                    .getResource(resourcePathWithoutLeadingSlash);
        }
    }
    return checkNotNull(resource, String.format("Resource [%s] loading failed", resourcePath));
}

From source file:com.centeractive.ws.builder.utils.ResourceUtils.java

public static InputStream getResourceWithAbsolutePackagePathAsStream(Class<?> clazz, String absolutePackagePath,
        String resourceName) {//  www  .  jav a 2  s  .co  m
    checkNotNull(clazz, "clazz cannot be null");
    String resourcePath = getResourcePath(absolutePackagePath, resourceName);
    InputStream resource = null;
    // first attempt - outside/inside jar file
    resource = clazz.getClass().getResourceAsStream(resourcePath);
    // second attempt - servlet container - inside application lib folder
    if (resource == null) {
        ClassLoader classLoader = clazz.getClass().getClassLoader();
        if (classLoader != null)
            resource = classLoader.getResourceAsStream(resourcePath);
    }
    return checkNotNull(resource, String.format("Resource [%s] loading failed", resourcePath));
}

From source file:org.red5.server.service.ConversionUtils.java

/**
 * Convert map to bean//from   w  ww .j  a  v a  2s .  c  o m
 * @param source                Source map
 * @param target                Target class
 * @return                      Bean of that class
 * @throws ConversionException
 */
public static Object convertMapToBean(Map<?, ?> source, Class<?> target) throws ConversionException {
    Object bean = newInstance(target.getClass().getName());
    if (bean == null) {
        throw new ConversionException("Unable to create bean using empty constructor");
    }
    try {
        BeanUtils.populate(bean, source);
    } catch (Exception e) {
        throw new ConversionException("Error populating bean", e);
    }
    return bean;
}

From source file:com.example.soapcommon.core.ResourceUtils.java

public static URL getResourceWithAbsolutePackagePath(Class<?> clazz, String absolutePackagePath,
        String resourceName) {/*w w w  .  j  a va2 s.  c  o m*/
    checkNotNull(clazz, "clazz cannot be null");
    String resourcePath = getResourcePath(absolutePackagePath, resourceName);
    URL resource = null;
    // first attempt - outside/inside jar file
    resource = clazz.getClass().getResource(resourcePath);
    // second attempt - servlet container - inside application lib folder
    if (resource == null) {
        if (resourcePath.charAt(0) == '/') {
            String resourcePathWithoutLeadingSlash = resourcePath.substring(1);
            resource = Thread.currentThread().getContextClassLoader()
                    .getResource(resourcePathWithoutLeadingSlash);
        }
    }
    checkArgument(resource != null, String.format("Resource [%s] loading failed", resourcePath));
    return resource;
}

From source file:com.centeractive.ws.common.ResourceUtils.java

public static URL getResourceWithAbsolutePackagePath(Class<?> clazz, String absolutePackagePath,
        String resourceName) {/*from w w w  .  j  av a2s .c  om*/
    checkNotNull(clazz, "clazz cannot be null");
    String resourcePath = getResourcePath(absolutePackagePath, resourceName);
    URL resource = null;
    // first attempt - outside/inside jar file

    resource = clazz.getClass().getResource(resourcePath);

    // second attempt - servlet container - inside application lib folder

    if (resource == null) {
        if (resourcePath.charAt(0) == '/') {
            String resourcePathWithoutLeadingSlash = resourcePath.substring(1);
            resource = Thread.currentThread().getContextClassLoader()
                    .getResource(resourcePathWithoutLeadingSlash);
        }
    }
    checkArgument(resource != null, String.format("Resource [%s] loading failed", resourcePath));
    return resource;
}

From source file:com.centeractive.ws.common.ResourceUtils.java

public static InputStream getResourceWithAbsolutePackagePathAsStream(Class<?> clazz, String absolutePackagePath,
        String resourceName) {/*from   www  . jav  a  2s.c  om*/
    checkNotNull(clazz, "clazz cannot be null");
    String resourcePath = getResourcePath(absolutePackagePath, resourceName);
    InputStream resource = null;
    // first attempt - outside/inside jar file
    resource = clazz.getClass().getResourceAsStream(resourcePath);
    // second attempt - servlet container - inside application lib folder
    if (resource == null) {
        ClassLoader classLoader = clazz.getClass().getClassLoader();
        if (classLoader != null)
            resource = classLoader.getResourceAsStream(resourcePath);
    }
    checkArgument(resource != null, String.format("Resource [%s] loading failed", resourcePath));
    return resource;
}