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:kieker.tools.bridge.cli.CLIServerMain.java

/**
 * CLI server main.//  w  w  w.  ja  v a2s.co m
 *
 * @param args
 *            command line arguments
 */
public static void main(final String[] args) {
    Configuration configuration;

    int exitCode = 0;
    CLIServerMain.declareOptions();
    try {
        // parse the command line arguments
        commandLine = new BasicParser().parse(options, args);

        // verbosity setup
        verbose = commandLine.hasOption(CMD_VERBOSE);

        // statistics
        stats = commandLine.hasOption(CMD_STATS);

        // daemon mode
        if (commandLine.hasOption(CMD_DAEMON)) {
            System.out.close();
            System.err.close();
            CLIServerMain.getPidFile().deleteOnExit();
        }

        // Find libraries and setup mapping
        final ConcurrentMap<Integer, LookupEntity> lookupEntityMap = ServiceConnectorFactory
                .createLookupEntityMap(CLIServerMain.createRecordMap());

        // Kieker setup
        if (commandLine.hasOption(CMD_KIEKER_CONFIGURATION)) {
            configuration = ConfigurationFactory.createConfigurationFromFile(commandLine.getOptionValue("c"));
        } else {
            configuration = ConfigurationFactory.createSingletonConfiguration();
        }

        // reconfigure kieker configuration
        if (commandLine.hasOption(CMD_PORT) && commandLine.hasOption(CMD_TYPE)) {
            final String type = commandLine.getOptionValue(CMD_TYPE);
            if ("jms-embedded".equals(type)) {
                configuration.setProperty(JMSEmbeddedConnector.PORT, commandLine.getOptionValue(CMD_PORT));
            } else if ("tcp-single-server".equals(type)) {
                configuration.setProperty(TCPSingleServerConnector.PORT, commandLine.getOptionValue(CMD_PORT));
            } else if ("tcp-server".equals(type)) {
                configuration.setProperty(TCPMultiServerConnector.PORT, commandLine.getOptionValue(CMD_PORT));
            } else if ("tcp-client".equals(type)) {
                configuration.setProperty(TCPClientConnector.PORT, commandLine.getOptionValue(CMD_PORT));
            } else if ("http-rest".equals(type)) {
                configuration.setProperty(HTTPConnector.PORT, commandLine.getOptionValue(CMD_PORT));
            }
        }
        if (commandLine.hasOption(CMD_HOST)) {
            configuration.setProperty(TCPClientConnector.HOSTNAME, commandLine.getOptionValue(CMD_HOST));
        }
        if (commandLine.hasOption(CMD_USER)) {
            configuration.setProperty(JMSClientConnector.USERNAME, commandLine.getOptionValue(CMD_USER));
        }
        if (commandLine.hasOption(CMD_PASSWORD)) {
            configuration.setProperty(JMSClientConnector.PASSWORD, commandLine.getOptionValue(CMD_PASSWORD));
        }
        if (commandLine.hasOption(CMD_URL)) {
            configuration.setProperty(JMSClientConnector.URI, commandLine.getOptionValue(CMD_URL));
            configuration.setProperty(HTTPConnector.REST_URL, commandLine.getOptionValue(CMD_URL));
        }
        if (commandLine.hasOption(CMD_CONTEXT)) {
            configuration.setProperty(HTTPConnector.CONTEXT, commandLine.getOptionValue(CMD_CONTEXT));
        }
        if (commandLine.hasOption(CMD_TYPE)) {
            final Reflections reflections = new Reflections("kieker.tools.bridge.connector");
            final Set<Class<?>> connectors = reflections
                    .getTypesAnnotatedWith(kieker.tools.bridge.connector.ConnectorProperty.class);

            for (final Class<?> connector : connectors) {
                if (connector.getAnnotation(kieker.tools.bridge.connector.ConnectorProperty.class).cmdName()
                        .equals(commandLine.getOptionValue(CMD_TYPE))) {
                    configuration.setProperty(CLI_CONNECTOR, connector.getCanonicalName());
                    break;
                }
            }
        }

        // start service depending on type
        final IServiceConnector connector = CLIServerMain.createService(configuration, lookupEntityMap);
        CLIServerMain.getLog().info("Service " + connector.getClass()
                .getAnnotation(kieker.tools.bridge.connector.ConnectorProperty.class).name());
        CLIServerMain.runService(configuration, connector);

    } catch (final ParseException e) {
        CLIServerMain.usage("Parsing failed.  Reason: " + e.getMessage());
        exitCode = 4;
    } catch (final IOException e) {
        CLIServerMain.usage("Mapping file read error: " + e.getMessage());
        exitCode = 1;
    } catch (final CLIConfigurationErrorException e) {
        CLIServerMain.usage("Configuration error: " + e.getMessage());
        exitCode = 2;
    } catch (final ConnectorDataTransmissionException e) {
        CLIServerMain.usage("Communication error: " + e.getMessage());
        exitCode = 3;
    }
    // finally {
    // // The URLClassLoader does not have a close method in Java 1.5
    // // if (classLoader != null) {
    // // try {
    // // classLoader.close();
    // // } catch (final IOException e) {
    // // LOG.error("Classloader failed on close.");
    // // exitCode = 5;
    // // }
    // // }
    // }
    System.exit(exitCode);
}

From source file:Main.java

public static boolean isImplement(Class<?> clazz, Class<?> interfaceClazz) {
    if (clazz.equals(interfaceClazz))
        return true;

    Class<?>[] ifs = clazz.getInterfaces();
    for (Class<?> i : ifs) {
        if (i.equals(interfaceClazz))
            return true;
    }// w w w. j  a  va  2s  .  com

    Class<?> s = clazz.getSuperclass();
    if (s == null || clazz.equals(s.getClass()))
        return false;

    return isImplement(s, interfaceClazz);
}

From source file:Main.java

public static Type[] getGenericType(Class<?> clazz, Class<?> interfaceClazz) {
    Type st = clazz.getGenericSuperclass();
    Type[] ret = getActualTypeArguments(interfaceClazz, st);

    if (ret != null)
        return ret;

    for (Type t : clazz.getGenericInterfaces()) {
        ret = getActualTypeArguments(interfaceClazz, t);
        if (ret != null)
            return ret;
    }//from  w  w  w  .jav a  2s.c om

    Class<?> s = clazz.getSuperclass();
    if (s == null || clazz.equals(s.getClass()))
        return new Type[0];

    return getGenericType(s, interfaceClazz);
}

From source file:Main.java

public static Object newInstance(Class cls, Class<?>[] types, Object... params) {
    Constructor<?> mConstructor;
    try {//from   www  . j a  v a 2  s. co  m
        return cls.getClass().newInstance();
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;

}

From source file:net.certiv.antlr.project.util.Utils.java

public static String findJarPathname(Class<?> ref) {
    try {//from  w  w  w .j  a v  a 2  s  .  c om
        File path = new File(ref.getClass().getProtectionDomain().getCodeSource().getLocation().toURI());
        String name = path.getCanonicalPath();
        if (name.endsWith("jar")) {
            Log.info(Utils.class, "Found: " + name);
            return name;
        }
    } catch (NullPointerException | URISyntaxException | IOException e) {
        Log.warn(Utils.class, "Unable to access executing jar");
    }
    return null;
}

From source file:org.kaaproject.kaa.server.common.dao.impl.DaoUtil.java

/**
 * This method find file by name in context and convert input of file to string.
 *
 * @param name  of the file//from   w ww .j  a  va 2 s. co  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 exception) {
                LOG.error("Can't read data from file", exception);
            }
        }
    }
    return data;
}

From source file:com.feedzai.fos.common.validation.ValidationUtils.java

/**
 * Gets a <code>Class</code> from the given configuration.
 *
 * @param configuration the configuration where the parameter lies
 * @param parameterName the name of the parameter
 * @param clazz         the class that the parameter represents
 * @return the <code>Class</code>
 * @throws IllegalArgumentException if the class was not found
 *//*from w  ww.  j  a va2  s  .  c o m*/
@NotNull
public static <T> Class<T> getClass(Configuration configuration, @NotBlank String parameterName,
        Class<T> clazz) {
    String classname = getStringNotBlank(configuration, parameterName);

    try {
        return clazz.getClass().cast(Class.forName(classname));
    } catch (ClassNotFoundException e) {
        throw new IllegalArgumentException(String.format(NOT_CLASS, classname, clazz.getName()), e);
    }
}

From source file:ar.com.zauber.commons.repository.BaseEntity.java

/**
 * @param theClass//from  ww w . j  ava  2s. c  o m
 * @return
 */
private static Set<Field> getIdentityFields(final Class<?> theClass) {
    Set<Field> fields = new HashSet<Field>();
    if (theClass.getAnnotation(IdentityProperties.class) != null) {
        String[] fieldNamesArray = theClass.getClass().getAnnotation(IdentityProperties.class).fieldNames();
        for (int i = 0; i < fieldNamesArray.length; i++) {
            try {
                fields.add((theClass.getField(fieldNamesArray[i])));
            } catch (final SecurityException e) {
                throw new IllegalStateException(e);
            } catch (final NoSuchFieldException e) {
                throw new IllegalStateException(e);
            }

        }
    } else {
        Field[] fieldsArray = theClass.getFields();
        for (int i = 0; i < fieldsArray.length; i++) {
            if (fieldsArray[i].getAnnotation(IdentityProperty.class) != null) {
                fields.add(fieldsArray[i]);
            }
        }
        if (!theClass.getSuperclass().equals(Object.class)) {
            fields.addAll(getIdentityFields(theClass.getSuperclass()));
        }
    }
    return fields;
}

From source file:net.sf.texprinter.utils.UIUtils.java

/**
 * Loads the image icon from a package. Another simple method, this one
 * will load an image from inside a package and then add it to an ImageIcon
 * object.//from w ww.j a  v a  2 s  . c o  m
 *
 * @param theClass The class. It's used to get the resource.
 * @param path The path. The path is used to make the search more reliable.
 * @param description The description. Telling what the image actually is
 * might somehow help.
 * @return The image icon in a ImageIcon object.
 */
public static ImageIcon createImageIcon(Class theClass, String path, String description) {

    // get the resource
    URL imgURL = theClass.getClass().getResource(path);

    // check if not null
    if (imgURL != null) {

        // return new icon
        return new ImageIcon(imgURL, description);

    } else {

        // return null
        return null;
    }
}

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

public static InputStream getResourceAsInputStream(Class<?> clazz, String resourcePath) {
    if (clazz == null) {
        throw new NullPointerException("clazz cannot be null");
    }/*ww  w .j  av a  2s. c o  m*/
    InputStream 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;
}