Example usage for java.lang Class toString

List of usage examples for java.lang Class toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Converts the object to a string.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Object clazz = new TestClass();
    String lookingForValue = "firstValue";

    Field field = clazz.getClass().getField(lookingForValue);
    Class clazzType = field.getType();
    if (clazzType.toString().equals("double"))
        System.out.println(field.getDouble(clazz));
    else if (clazzType.toString().equals("int"))
        System.out.println(field.getInt(clazz));

    //System.out.println(field.get(clazz));
}

From source file:Main.java

public static void main(String[] args) {

    Main c = new Main();
    Class cls = c.getClass();

    // returns the string representation of this class object
    String str = cls.toString();
    System.out.println("Class = " + str);

}

From source file:Main.java

public static void main(String[] args) {

    Class cls = String.class;

    Method[] m = cls.getMethods();
    for (int i = 0; i < m.length; i++) {
        // returns te declaring class
        Class dec = m[i].getDeclaringClass();
        // displays all methods
        System.out.println("Method = " + m[i].toString());
        System.out.println(" Declaring class: " + dec.toString());
    }//from  w w w . j  a v a 2  s .c o m
}

From source file:tests.it.crs4.seal.recab.BenchmarkVariantTable.java

@SuppressWarnings("rawtypes") // generated by the array of of Class below
public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.err.println("Usage: BenchmarkVariantTable <vcf file>");
        System.exit(1);/*w  w  w.  jav  a 2s . c om*/
    }

    File vcf = new File(args[0]);
    VcfVariantReader vcfReader = new VcfVariantReader(new FileReader(vcf));

    Class[] tableClasses = new Class[] { HashSetVariantTable.class, ArrayListVariantTable.class,
            ArrayVariantTable.class };

    for (Class klas : tableClasses) {
        System.out.println("Trying " + klas);
        try {
            vcfReader = new VcfVariantReader(new FileReader(vcf));
            VariantTable table = (VariantTable) klas.newInstance();
            Benchmark bench = new Benchmark(table, vcfReader);
            System.out.println("Running bechmark");
            Run r = bench.run();
            System.out.println("finished\n\n");
            r.report(klas.toString(), System.out);
            Thread.sleep(5000);
        } catch (OutOfMemoryError e) {
            System.err.println("ran out of memory with class " + klas);
        } catch (Exception e) {
            System.err.println("Error with class " + klas);
        }
    }
}

From source file:org.dspace.app.itemupdate.ItemUpdate.java

/**
 * /*from  ww w  .j  a v a  2 s .c  om*/
 * @param argv
 */
public static void main(String[] argv) {
    // create an options object and populate it
    CommandLineParser parser = new PosixParser();

    Options options = new Options();

    //processing basis for determining items    
    //item-specific changes with metadata in source directory with dublin_core.xml files  
    options.addOption("s", "source", true, "root directory of source dspace archive ");

    //actions  on items     
    options.addOption("a", "addmetadata", true,
            "add metadata specified for each item; multiples separated by semicolon ';'");
    options.addOption("d", "deletemetadata", true, "delete metadata specified for each item");

    options.addOption("A", "addbitstreams", false, "add bitstreams as specified for each item");

    // extra work to get optional argument
    Option delBitstreamOption = new Option("D", "deletebitstreams", true,
            "delete bitstreams as specified for each item");
    delBitstreamOption.setOptionalArg(true);
    delBitstreamOption.setArgName("BitstreamFilter");
    options.addOption(delBitstreamOption);

    //other params        
    options.addOption("e", "eperson", true, "email of eperson doing the update");
    options.addOption("i", "itemfield", true,
            "optional metadata field that containing item identifier; default is dc.identifier.uri");
    options.addOption("F", "filter-properties", true, "filter class name; only for deleting bitstream");
    options.addOption("v", "verbose", false, "verbose logging");

    //special run states        
    options.addOption("t", "test", false, "test run - do not actually import items");
    options.addOption("P", "provenance", false, "suppress altering provenance field for bitstream changes");
    options.addOption("h", "help", false, "help");

    int status = 0;
    boolean isTest = false;
    boolean alterProvenance = true;
    String itemField = null;
    String metadataIndexName = null;

    Context context = null;
    ItemUpdate iu = new ItemUpdate();

    try {
        CommandLine line = parser.parse(options, argv);

        if (line.hasOption('h')) {
            HelpFormatter myhelp = new HelpFormatter();
            myhelp.printHelp("ItemUpdate", options);
            pr("");
            pr("Examples:");
            pr("  adding metadata:     ItemUpdate -e jsmith@mit.edu -s sourcedir -a dc.contributor -a dc.subject ");
            pr("  deleting metadata:   ItemUpdate -e jsmith@mit.edu -s sourcedir -d dc.description.other");
            pr("  adding bitstreams:   ItemUpdate -e jsmith@mit.edu -s sourcedir -A -i dc.identifier");
            pr("  deleting bitstreams: ItemUpdate -e jsmith@mit.edu -s sourcedir -D ORIGINAL ");
            pr("");

            System.exit(0);
        }

        if (line.hasOption('v')) {
            verbose = true;
        }

        if (line.hasOption('P')) {
            alterProvenance = false;
            pr("Suppressing changes to Provenance field option");
        }

        iu.eperson = line.getOptionValue('e'); // db ID or email

        if (!line.hasOption('s')) // item specific changes from archive dir
        {
            pr("Missing source archive option");
            System.exit(1);
        }
        String sourcedir = line.getOptionValue('s');

        if (line.hasOption('t')) //test
        {
            isTest = true;
            pr("**Test Run** - not actually updating items.");

        }

        if (line.hasOption('i')) {
            itemField = line.getOptionValue('i');
        }

        if (line.hasOption('d')) {
            String[] targetFields = line.getOptionValues('d');

            DeleteMetadataAction delMetadataAction = (DeleteMetadataAction) iu.actionMgr
                    .getUpdateAction(DeleteMetadataAction.class);
            delMetadataAction.addTargetFields(targetFields);

            //undo is an add 
            for (String field : targetFields) {
                iu.undoActionList.add(" -a " + field + " ");
            }

            pr("Delete metadata for fields: ");
            for (String s : targetFields) {
                pr("    " + s);
            }
        }

        if (line.hasOption('a')) {
            String[] targetFields = line.getOptionValues('a');

            AddMetadataAction addMetadataAction = (AddMetadataAction) iu.actionMgr
                    .getUpdateAction(AddMetadataAction.class);
            addMetadataAction.addTargetFields(targetFields);

            //undo is a delete followed by an add of a replace record for target fields
            for (String field : targetFields) {
                iu.undoActionList.add(" -d " + field + " ");
            }

            for (String field : targetFields) {
                iu.undoActionList.add(" -a " + field + " ");
            }

            pr("Add metadata for fields: ");
            for (String s : targetFields) {
                pr("    " + s);
            }
        }

        if (line.hasOption('D')) // undo not supported 
        {
            pr("Delete bitstreams ");

            String[] filterNames = line.getOptionValues('D');
            if ((filterNames != null) && (filterNames.length > 1)) {
                pr("Error: Only one filter can be a used at a time.");
                System.exit(1);
            }

            String filterName = line.getOptionValue('D');
            pr("Filter argument: " + filterName);

            if (filterName == null) // indicates using delete_contents files
            {
                DeleteBitstreamsAction delAction = (DeleteBitstreamsAction) iu.actionMgr
                        .getUpdateAction(DeleteBitstreamsAction.class);
                delAction.setAlterProvenance(alterProvenance);
            } else {
                // check if param is on ALIAS list
                String filterClassname = filterAliases.get(filterName);

                if (filterClassname == null) {
                    filterClassname = filterName;
                }

                BitstreamFilter filter = null;

                try {
                    Class<?> cfilter = Class.forName(filterClassname);
                    pr("BitstreamFilter class to instantiate: " + cfilter.toString());

                    filter = (BitstreamFilter) cfilter.newInstance(); //unfortunate cast, an erasure consequence
                } catch (Exception e) {
                    pr("Error:  Failure instantiating bitstream filter class: " + filterClassname);
                    System.exit(1);
                }

                String filterPropertiesName = line.getOptionValue('F');
                if (filterPropertiesName != null) //not always required
                {
                    try {
                        // TODO try multiple relative locations, e.g. source dir
                        if (!filterPropertiesName.startsWith("/")) {
                            filterPropertiesName = sourcedir + File.separator + filterPropertiesName;
                        }

                        filter.initProperties(filterPropertiesName);
                    } catch (Exception e) {
                        pr("Error:  Failure finding properties file for bitstream filter class: "
                                + filterPropertiesName);
                        System.exit(1);
                    }
                }

                DeleteBitstreamsByFilterAction delAction = (DeleteBitstreamsByFilterAction) iu.actionMgr
                        .getUpdateAction(DeleteBitstreamsByFilterAction.class);
                delAction.setAlterProvenance(alterProvenance);
                delAction.setBitstreamFilter(filter);
                //undo not supported
            }
        }

        if (line.hasOption('A')) {
            pr("Add bitstreams ");
            AddBitstreamsAction addAction = (AddBitstreamsAction) iu.actionMgr
                    .getUpdateAction(AddBitstreamsAction.class);
            addAction.setAlterProvenance(alterProvenance);

            iu.undoActionList.add(" -D "); // delete_contents file will be written, no arg required              
        }

        if (!iu.actionMgr.hasActions()) {
            pr("Error - an action must be specified");
            System.exit(1);
        } else {
            pr("Actions to be performed: ");

            for (UpdateAction ua : iu.actionMgr) {
                pr("    " + ua.getClass().getName());
            }
        }

        pr("ItemUpdate - initializing run on " + (new Date()).toString());

        context = new Context();
        iu.setEPerson(context, iu.eperson);
        context.setIgnoreAuthorization(true);

        HANDLE_PREFIX = ConfigurationManager.getProperty("handle.canonical.prefix");
        if (HANDLE_PREFIX == null || HANDLE_PREFIX.length() == 0) {
            HANDLE_PREFIX = "http://hdl.handle.net/";
        }

        iu.processArchive(context, sourcedir, itemField, metadataIndexName, alterProvenance, isTest);

        context.complete(); // complete all transactions
        context.setIgnoreAuthorization(false);
    } catch (Exception e) {
        if (context != null && context.isValid()) {
            context.abort();
            context.setIgnoreAuthorization(false);
        }
        e.printStackTrace();
        pr(e.toString());
        status = 1;
    }

    if (isTest) {
        pr("***End of Test Run***");
    } else {
        pr("End.");

    }
    System.exit(status);
}

From source file:Main.java

public static void classInspector(Object object) {
    Class cls = object.getClass();
    System.out.println(cls.toString());
    Field[] fieldlist = cls.getDeclaredFields();
    for (int i = 0; i < fieldlist.length; i++) {
        Field fld = fieldlist[i];

        System.out.println("name = " + fld.getName());
        System.out.println("decl class = " + fld.getDeclaringClass());
        System.out.println("type = " + fld.getType());
        System.out.println("-----");
    }/*from   w w  w.  j  a  v  a 2s.  c  om*/
}

From source file:Main.java

private static boolean checkIfActivityImplementsInterface(Activity theActivity, Class theInterface) {
    for (Class i : theActivity.getClass().getInterfaces())
        if (i.toString().equals(theInterface.toString()))
            return true;
    return false;
}

From source file:org.apache.hadoop.hbase.CompatibilityFactory.java

protected static String createExceptionString(Class klass) {
    return EXCEPTION_START + klass.toString() + EXCEPTION_END;
}

From source file:org.pircbotx.hooks.events.MassEventTest.java

public static String wrapClass(Class aClass, String message) {
    return message + " in " + aClass.toString();
}

From source file:com.sf.ddao.chain.CtxHelper.java

public static <T> T get(Context ctx, Class<T> clazz) {
    //noinspection unchecked
    return (T) ctx.get(clazz.toString());
}