Example usage for java.lang.reflect Constructor toString

List of usage examples for java.lang.reflect Constructor toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns a string describing this Constructor .

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Constructor[] constructors = ArrayList.class.getDeclaredConstructors();

    for (int i = 0; i < constructors.length; i++) {
        Constructor c = constructors[i];
        System.out.println(c.toString());
    }//from  w  ww. j  ava 2 s  . co m
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    MyClass cls = new MyClass();
    Class c = cls.getClass();//from w ww. ja va2s. c  om

    // constructor with arguments as Double and Long
    Class[] cArg = new Class[2];
    cArg[0] = Double.class;
    cArg[1] = Long.class;
    Constructor ct = c.getDeclaredConstructor(cArg);
    System.out.println("Constructor = " + ct.toString());

}

From source file:com.threewks.thundr.injection.InjectionContextImpl.java

private <T> T invokeConstructor(Constructor<T> constructor, Object[] args) {
    try {//  w w  w.j  a  va  2 s.  c  om
        return constructor.newInstance(args);
    } catch (Exception e) {
        throw new InjectionException(e, "Failed to create a new instance using the constructor %s: %s",
                constructor.toString(), getRootMessage(e));
    }
}

From source file:bammerbom.ultimatecore.bukkit.configuration.ConfigurationSerialization.java

protected ConfigurationSerializable deserializeViaCtor(Constructor<?> ctor, Map<String, ?> args) {
    try {//from   w w w .java 2s . c  o m
        return (ConfigurationSerializable) ctor.newInstance(args);
    } catch (Throwable ex) {
        Logger.getLogger(ConfigurationSerialization.class.getName()).log(Level.SEVERE,
                "Could not call constructor '" + ctor.toString() + "' of " + clazz + " for deserialization",
                ex instanceof InvocationTargetException ? ex.getCause() : ex);
    }

    return null;
}

From source file:com.chiorichan.configuration.serialization.ConfigurationSerialization.java

protected ConfigurationSerializable deserializeViaCtor(Constructor<? extends ConfigurationSerializable> ctor,
        Map<String, Object> args) {
    try {// www  .  j  av  a2s  . c o m
        return ctor.newInstance(args);
    } catch (Throwable ex) {
        Logger.getLogger(ConfigurationSerialization.class.getName()).log(Level.SEVERE,
                "Could not call constructor '" + ctor.toString() + "' of " + clazz + " for deserialization",
                ex instanceof InvocationTargetException ? ex.getCause() : ex);
    }

    return null;
}

From source file:net.itransformers.topologyviewer.gui.MyVisualizationViewer.java

private Transformer<String, String> createEdgeToolTipTransformer() {
    return new Transformer<String, String>() {
        private Transformer<String, String> createTransformer() {
            TooltipType tooltipType = getTooltip(currentFilter, ForType.EDGE);
            if (tooltipType == null)
                return null;
            String transformerClassName = tooltipType.getTransformer();
            if (transformerClassName == null) {
                return new DefaultEdgeTooltipTransformer(tooltipType, edgeMetadatas);
            } else {
                try {
                    final Class<?> aClass = Class.forName(transformerClassName, true,
                            this.getClass().getClassLoader());
                    if (EdgeTooltipTransformerBase.class.isAssignableFrom(aClass)) {
                        Class<EdgeTooltipTransformerBase> transformerBaseClass = (Class<EdgeTooltipTransformerBase>) aClass;
                        Constructor<EdgeTooltipTransformerBase> constr = transformerBaseClass
                                .getConstructor(new Class[] { TooltipType.class, Map.class });
                        try {
                            EdgeTooltipTransformerBase inst = constr.newInstance(tooltipType, edgeMetadatas);
                            return inst;
                        } catch (IllegalArgumentException iae) {
                            logger.error(constr.toString());
                            throw iae;
                        }//ww  w.ja v  a  2 s  .c  o  m
                        //                            return inst;
                    } else {
                        throw new RuntimeException("not implemented");
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }

        public String transform(String s) {
            final Transformer<String, String> transformer = createTransformer();
            if (transformer == null)
                return "";
            return transformer.transform(s);
        }
    };
}

From source file:net.itransformers.topologyviewer.gui.MyVisualizationViewer.java

private Transformer<String, String> createNodeTooltipTransformer() {
    return new Transformer<String, String>() {
        private Transformer<String, String> createTransformer() {
            TooltipType tooltipType = getTooltip(currentFilter, ForType.NODE);
            if (tooltipType == null)
                return null;
            String transformerClassName = tooltipType.getTransformer();
            if (transformerClassName == null) {
                return new DefaultNodeTooltipTransformer(tooltipType, vertexMetadatas);
            } else {
                try {
                    final Class<?> aClass = Class.forName(transformerClassName, true,
                            this.getClass().getClassLoader());
                    if (NodeTooltipTransformerBase.class.isAssignableFrom(aClass)) {
                        Class<NodeTooltipTransformerBase> transformerBaseClass = (Class<NodeTooltipTransformerBase>) aClass;
                        Constructor<NodeTooltipTransformerBase> constr = transformerBaseClass
                                .getConstructor(new Class[] { TooltipType.class, Map.class });
                        try {
                            NodeTooltipTransformerBase inst = constr.newInstance(tooltipType, vertexMetadatas);
                            return inst;
                        } catch (IllegalArgumentException iae) {
                            logger.error(constr.toString());
                            throw iae;
                        }/*from   w  w w.  ja va2s  .c o m*/
                    } else {
                        throw new RuntimeException("not implemented");
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }

        public String transform(String s) {
            final Transformer<String, String> transformer = createTransformer();
            if (transformer == null)
                return null;
            return transformer.transform(s);
        }
    };
}

From source file:dk.netarkivet.archive.webinterface.BatchGUI.java

/**
 * Creates the HTML code for the arguments of the constructor. It reads the resources for the batchjob, where the
 * metadata for the constructor is defined in the 'resources' annotation for the class.
 * <p>// w w  w  .  ja  v  a  2 s . c o m
 * <br/>
 * E.g. The UrlSearch batchjob. Which has the following resources:<br/>
 *
 * @param c The class whose constructor should be used.
 * @param locale The language package.
 * @return The HTML code for the arguments for executing the batchjob.
 * @Resource(name="regex", description="The regular expression for the " + "urls.", type=java.lang.String.class)<br/>
 * @Resource(name="mimetype", type=java.lang.String.class)<br/>
 * Though the batchjob takes three arguments (thus one undefined). <br/>
 * <br/>
 * <p>
 * Arguments:&lt;br/&gt;<br/>
 * regex (The regular expression for the urls.)&lt;br/&gt;<br/>
 * &lt;input name="arg1" size="50" value=""&gt;&lt;br/&gt;<br/>
 * mimetype&lt;br/&gt;<br/>
 * &lt;input name="arg2" size="50" value=""&gt;&lt;br/&gt;<br/>
 * Argument 3 (missing argument metadata)&lt;br/&gt;<br/>
 * &lt;input name="arg3" size="50" value=""&gt;&lt;br/&gt;<br/>
 * <p>
 * <br/>
 * Which will look like: <br/>
 * <br/>
 * <p>
 * Arguments:<br/>
 * regex (The regular expression for the urls.)<br/>
 * <input name="arg1" size="50" value="" /><br/>
 * mimetype<br/>
 * <input name="arg2" size="50" value="" /><br/>
 * Argument 3 (missing argument metadata)<br/>
 * <input name="arg3" size="50" value="" /><br/>
 * <p>
 * TODO this does not work until batchjobs can be used with arguments.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private static String getHTMLarguments(Class c, Locale locale) {
    Constructor con = findStringConstructor(c);
    Type[] params = con.getParameterTypes();

    // If no parameters, then return no content (new line).
    if (params.length < 1) {
        return "<br/>\n";
    }

    // Retrieve the resources (metadata for the arguments).
    Resources r = (Resources) c.getAnnotation(Resources.class);
    if (r == null) {
        return "<br/>\n";
    }
    Resource[] resource = r.value();

    StringBuilder res = new StringBuilder();

    res.append(I18N.getString(locale, "batchpage;Arguments", new Object[] {}) + ":<br/>\n");

    if (resource.length < params.length) {
        // warn about no metadata.
        res.append(I18N.getString(locale, "batchpage;Bad.argument.metadata.for.the.constructor", con.toString())
                + ".<br/>\n");
        // make default 'arguments'.
        for (int i = 1; i <= params.length; i++) {
            res.append(I18N.getString(locale, "batchpage;Argument.i", i) + "<br/>\n");
            res.append("<input name=\"arg" + i + "\" size=\"" + Constants.HTML_INPUT_SIZE
                    + "\" value=\"\"><br/>\n");
        }
    } else {
        // handle the case, when there is arguments.
        int parmIndex = 0;
        // retrieve the arguments from the resources.
        for (int i = 0; i < resource.length && parmIndex < params.length; i++) {
            if (resource[i].type() == params[parmIndex]) {
                // Use the resource to describe the argument.
                parmIndex++;
                res.append(resource[i].name());
                if (resource[i].description() != null && !resource[i].description().isEmpty()) {
                    res.append(" (" + resource[i].description() + ")");
                }
                res.append("<br/>\n");
                res.append("<input name=\"arg" + parmIndex + "\" size=\"" + Constants.HTML_INPUT_SIZE
                        + "\" value=\"\"><br/>\n");
            }
        }
        // If some arguments did not have a resource description, then
        // use a default 'unknown argument' input box.
        if (parmIndex < params.length) {
            for (int i = parmIndex + 1; i <= params.length; i++) {
                res.append(I18N.getString(locale, "batchpage;Argument.i.missing.argument.metadata", i)
                        + "<br/>\n");
                res.append("<input name=\"arg" + i + "\" size=\"" + Constants.HTML_INPUT_SIZE
                        + "\" value=\"\"><br/>\n");
            }
        }
    }

    res.append("<br/>\n");

    return res.toString();
}

From source file:org.janusgraph.diskstorage.cassandra.astyanax.AstyanaxStoreManager.java

@SuppressWarnings("unchecked")
private static <V> V instantiate(String policyClassName, Integer[] args, String raw) throws Exception {
    for (Constructor<?> con : Class.forName(policyClassName).getConstructors()) {
        Class<?>[] parameterTypes = con.getParameterTypes();

        // match constructor by number of arguments first
        if (args.length != parameterTypes.length)
            continue;

        // check if the constructor parameter types are compatible with argument types (which are integer)
        // note that we allow long.class arguments too because integer is cast to long by runtime.
        boolean intsOrLongs = true;
        for (Class<?> pc : parameterTypes) {
            if (!pc.equals(int.class) && !pc.equals(long.class)) {
                intsOrLongs = false;//from w w w . j a v  a  2  s  .co  m
                break;
            }
        }

        // we found a constructor with required number of parameters but times didn't match, let's carry on
        if (!intsOrLongs)
            continue;

        if (log.isDebugEnabled())
            log.debug("About to instantiate class {} with {} arguments", con.toString(), args.length);

        return (V) con.newInstance(args);
    }

    throw new Exception(
            "Failed to identify a class matching the Astyanax Retry Policy config string \"" + raw + "\"");
}