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:org.kaaproject.kaa.server.common.dao.model.sql.ModelUtils.java

/**
 * This method find file by name in context and convert input of file to string.
 *
 * @param name  of the file//from w ww  .jav a  2  s  . c  o m
 * @param clazz the clazz
 * @return String
 */
public static String getStringFromFile(String name, Class<?> clazz) {
    String data = "";
    if (isNotBlank(name)) {
        InputStream is = clazz.getClass().getResourceAsStream(name);
        if (is != null) {
            LOG.trace("Load input stream of file {}", is);
            try {
                byte[] arrayData = IOUtils.toByteArray(is);
                if (arrayData != null) {
                    data = new String(arrayData, Charset.forName("UTF-8"));
                }
            } catch (IOException ex) {
                LOG.error("Can't read data from file", ex);
            }
        }
    }
    return data;
}

From source file:org.grails.datastore.mapping.mongo.config.MongoMappingContext.java

/**
 * Check whether a type is a native mongo type that can be stored by the mongo driver without conversion.
 * @param clazz The class to check./* w  w w  .j  a v a  2s.  c  o m*/
 * @return true if no conversion is required and the type can be stored natively.
 */
public static boolean isMongoNativeType(Class clazz) {
    return MongoMappingContext.MONGO_NATIVE_TYPES.contains(clazz.getName())
            || Bson.class.isAssignableFrom(clazz.getClass());
}

From source file:com.netxforge.oss2.core.xml.JaxbUtils.java

private static Schema getValidatorFor(final Class<?> origClazz) {
    final Class<?> clazz = (Class<?>) (origClazz instanceof Class<?> ? origClazz : origClazz.getClass());
    LogUtils.tracef(clazz, "finding XSD for class %s", clazz);

    if (m_schemas.containsKey(clazz)) {
        return m_schemas.get(clazz);
    }/*from w  w w .j  a  v  a 2s  .c  o  m*/

    final ValidateUsing schemaFileAnnotation = clazz.getAnnotation(ValidateUsing.class);
    if (schemaFileAnnotation == null || schemaFileAnnotation.value() == null) {
        return null;
    }

    final String schemaFileName = schemaFileAnnotation.value();
    InputStream schemaInputStream = null;
    try {
        final SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
        if (schemaInputStream == null) {
            final File schemaFile = new File(
                    System.getProperty("opennms.home") + "/share/xsds/" + schemaFileName);
            if (schemaFile.exists()) {
                LogUtils.tracef(clazz, "using file %s", schemaFile);
                schemaInputStream = new FileInputStream(schemaFile);
            }
            ;
        }
        if (schemaInputStream == null) {
            final File schemaFile = new File("target/xsds/" + schemaFileName);
            if (schemaFile.exists()) {
                LogUtils.tracef(clazz, "using file %s", schemaFile);
                schemaInputStream = new FileInputStream(schemaFile);
            }
            ;
        }
        if (schemaInputStream == null) {
            final URL schemaResource = Thread.currentThread().getContextClassLoader()
                    .getResource("xsds/" + schemaFileName);
            if (schemaResource == null) {
                LogUtils.debugf(clazz, "Unable to load resource xsds/%s from the classpath.", schemaFileName);
            } else {
                LogUtils.tracef(clazz, "using resource %s from classpath", schemaResource);
                schemaInputStream = schemaResource.openStream();
            }
        }
        if (schemaInputStream == null) {
            LogUtils.tracef(clazz, "Did not find a suitable XSD.  Skipping.");
            return null;
        }
        final Schema schema = factory.newSchema(new StreamSource(schemaInputStream));
        m_schemas.put(clazz, schema);
        return schema;
    } catch (final Throwable t) {
        LogUtils.warnf(clazz, t, "an error occurred while attempting to load %s for validation",
                schemaFileName);
        return null;
    } finally {
        IOUtils.closeQuietly(schemaInputStream);
    }
}

From source file:org.wso2.carbon.utils.i18n.ProjectResourceBundle.java

/**
 * Construct a new ProjectResourceBundle
 *
 * @param projectName   The name of the project to which the class belongs.
 *                      It must be a proper prefix of the caller's package.
 * @param caller        The calling class.
 *                      This is used to get the package name to further construct
 *                      the basename as well as to get the proper ClassLoader.
 * @param resourceName  The name of the resource without the
 *                      ".properties" extension
 * @param locale        The locale//from w ww  .ja  v  a2  s . c o m
 * @param extendsBundle If non-null, then this ExtendMessages will
 *                      default to extendsBundle.
 * @throws MissingResourceException if projectName is not a prefix of
 *                                  the caller's package name, or if the resource could not be
 *                                  found/loaded.
 */
public static ProjectResourceBundle getBundle(String projectName, Class caller, String resourceName,
        Locale locale, ResourceBundle extendsBundle) {
    return getBundle(projectName, getPackage(caller.getClass().getName()), resourceName, locale,
            caller.getClass().getClassLoader(), extendsBundle);
}

From source file:com.ibm.soatf.component.soap.builder.ResourceUtils.java

public static InputStream getResourceWithAbsolutePackagePathAsStream(Class<?> clazz, String absolutePackagePath,
        String resourceName) {/*from w ww  .j  a  v a  2  s  .  c o m*/
    if (clazz == null) {
        throw new NullPointerException("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);
    }
    if (resource == null) {
        throw new IllegalArgumentException(String.format("Resource [%s] loading failed", resourcePath));
    }
    return resource;
}

From source file:org.apache.axis.i18n.ProjectResourceBundle.java

/**
 * Construct a new ProjectResourceBundle
 * /* ww  w.j av a 2s .co  m*/
 * @param projectName The name of the project to which the class belongs.
 *        It must be a proper prefix of the caller's package.
 * 
 * @param caller The calling class.
 *        This is used to get the package name to further construct
 *        the basename as well as to get the proper ClassLoader.
 * 
 * @param resourceName The name of the resource without the
 *        ".properties" extension
 * 
 * @param locale The locale
 * 
 * @param extendsBundle If non-null, then this ExtendMessages will
 *         default to extendsBundle.
 * 
 * @throws MissingResourceException if projectName is not a prefix of
 *         the caller's package name, or if the resource could not be
 *         found/loaded.
 */
public static ProjectResourceBundle getBundle(String projectName, Class caller, String resourceName,
        Locale locale, ResourceBundle extendsBundle) throws MissingResourceException {
    return getBundle(projectName, getPackage(caller.getClass().getName()), resourceName, locale,
            caller.getClass().getClassLoader(), extendsBundle);
}

From source file:net.sf.ufsc.ServiceLoader.java

/**
 * Creates a new service loader for the given service type, using the
 * current thread's {@linkplain java.lang.Thread#getContextClassLoader
 * context class loader}.//w ww. ja v  a2 s .  c  om
 *
 * <p> An invocation of this convenience method of the form
 *
 * <blockquote><pre>
 * ServiceLoader.load(<i>service</i>)</pre></blockquote>
 *
 * is equivalent to
 *
 * <blockquote><pre>
 * ServiceLoader.load(<i>service</i>,
 *                    Thread.currentThread().getContextClassLoader())</pre></blockquote>
 *
 * @param  service
 *         The interface or abstract class representing the service
 *
 * @return A new service loader
 */
public static <S> ServiceLoader<S> load(Class<S> service) {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    Log log = LogFactory.getLog(service.getClass());
    log.info("Loading ufsc service for " + service.getName());
    return ServiceLoader.load(service, cl);
}

From source file:io.github.moosbusch.lumpi.util.LumpiUtil.java

public static void logOnEDT(final Class<?> type, final Level level, final Throwable ex) {
    new EdtRunnable() {
        @Override/*from  w  w  w  .ja  va2s .c o m*/
        public void run() {
            Logger.getLogger(type.getClass().getName()).log(level, null, ex);
        }

    }.invokeLater();

}

From source file:net.sourceforge.jaulp.lang.PropertiesUtils.java

/**
 * Load properties.//  w  ww  .java 2 s. co m
 * 
 * @param clazz
 *            the clazz
 * @param name
 *            the package path with the file name
 * @return the properties
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static Properties loadProperties(Class<?> clazz, final String name) throws IOException {
    Properties properties = loadProperties(name);
    if (properties == null) {
        final InputStream is = ClassUtils.getResourceAsStream(clazz.getClass(), name);
        if (is != null) {
            properties = new Properties();
            properties.load(is);
        }
    }
    return properties;
}

From source file:org.apache.hadoop.mapreduce.v2.hs.HSProxies.java

@SuppressWarnings("unchecked")
public static <T> T createProxy(Configuration conf, InetSocketAddress hsaddr, Class<T> xface,
        UserGroupInformation ugi) throws IOException {

    T proxy;/*  ww  w. j a v a2s. com*/
    if (xface == RefreshUserMappingsProtocol.class) {
        proxy = (T) createHSProxyWithRefreshUserMappingsProtocol(hsaddr, conf, ugi);
    } else if (xface == GetUserMappingsProtocol.class) {
        proxy = (T) createHSProxyWithGetUserMappingsProtocol(hsaddr, conf, ugi);
    } else if (xface == HSAdminRefreshProtocol.class) {
        proxy = (T) createHSProxyWithHSAdminRefreshProtocol(hsaddr, conf, ugi);
    } else {
        String message = "Unsupported protocol found when creating the proxy "
                + "connection to History server: " + ((xface != null) ? xface.getClass().getName() : "null");
        LOG.error(message);
        throw new IllegalStateException(message);
    }
    return proxy;
}