<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FReflection%2FConstructor</id>
		<title>Java Tutorial/Reflection/Constructor - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FReflection%2FConstructor"/>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/Reflection/Constructor&amp;action=history"/>
		<updated>2026-04-21T20:07:37Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java_Tutorial/Reflection/Constructor&amp;diff=2969&amp;oldid=prev</id>
		<title> в 17:44, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/Reflection/Constructor&amp;diff=2969&amp;oldid=prev"/>
				<updated>2010-05-31T17:44:26Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 17:44, 31 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java_Tutorial/Reflection/Constructor&amp;diff=2970&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/Reflection/Constructor&amp;diff=2970&amp;oldid=prev"/>
				<updated>2010-05-31T15:19:35Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==  A program that displays a class synopsis for the named class ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 2004 David Flanagan.  All rights reserved.&lt;br /&gt;
 * This code is from the book Java Examples in a Nutshell, 3nd Edition.&lt;br /&gt;
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.&lt;br /&gt;
 * You may study, use, and modify it for any non-commercial purpose,&lt;br /&gt;
 * including teaching and use in open-source projects.&lt;br /&gt;
 * You may distribute it non-commercially as long as you retain this notice.&lt;br /&gt;
 * For a commercial use license, or to purchase the book, &lt;br /&gt;
 * please visit http://www.davidflanagan.ru/javaexamples3.&lt;br /&gt;
 */&lt;br /&gt;
//package je3.reflect;&lt;br /&gt;
import java.lang.reflect.Constructor;&lt;br /&gt;
import java.lang.reflect.Field;&lt;br /&gt;
import java.lang.reflect.Member;&lt;br /&gt;
import java.lang.reflect.Method;&lt;br /&gt;
import java.lang.reflect.Modifier;&lt;br /&gt;
/** A program that displays a class synopsis for the named class */&lt;br /&gt;
public class ShowClass {&lt;br /&gt;
  /** The main method. Print info about the named class */&lt;br /&gt;
  public static void main(String[] args) throws ClassNotFoundException {&lt;br /&gt;
    Class c = Class.forName(args[0]);&lt;br /&gt;
    print_class(c);&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Display the modifiers, name, superclass and interfaces of a class or&lt;br /&gt;
   * interface. Then go and list all constructors, fields, and methods.&lt;br /&gt;
   */&lt;br /&gt;
  public static void print_class(Class c) {&lt;br /&gt;
    // Print modifiers, type (class or interface), name and superclass.&lt;br /&gt;
    if (c.isInterface()) {&lt;br /&gt;
      // The modifiers will include the &amp;quot;interface&amp;quot; keyword here...&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; &amp;quot; + typename(c));&lt;br /&gt;
    } else if (c.getSuperclass() != null) {&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; + typename(c) + &amp;quot; extends &amp;quot;&lt;br /&gt;
          + typename(c.getSuperclass()));&lt;br /&gt;
    } else {&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; + typename(c));&lt;br /&gt;
    }&lt;br /&gt;
    // Print interfaces or super-interfaces of the class or interface.&lt;br /&gt;
    Class[] interfaces = c.getInterfaces();&lt;br /&gt;
    if ((interfaces != null) &amp;amp;&amp;amp; (interfaces.length &amp;gt; 0)) {&lt;br /&gt;
      if (c.isInterface())&lt;br /&gt;
        System.out.print(&amp;quot; extends &amp;quot;);&lt;br /&gt;
      else&lt;br /&gt;
        System.out.print(&amp;quot; implements &amp;quot;);&lt;br /&gt;
      for (int i = 0; i &amp;lt; interfaces.length; i++) {&lt;br /&gt;
        if (i &amp;gt; 0)&lt;br /&gt;
          System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
        System.out.print(typename(interfaces[i]));&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot; {&amp;quot;); // Begin class member listing.&lt;br /&gt;
    // Now look up and display the members of the class.&lt;br /&gt;
    System.out.println(&amp;quot;  // Constructors&amp;quot;);&lt;br /&gt;
    Constructor[] constructors = c.getDeclaredConstructors();&lt;br /&gt;
    for (int i = 0; i &amp;lt; constructors.length; i++)&lt;br /&gt;
      // Display constructors.&lt;br /&gt;
      print_method_or_constructor(constructors[i]);&lt;br /&gt;
    System.out.println(&amp;quot;  // Fields&amp;quot;);&lt;br /&gt;
    Field[] fields = c.getDeclaredFields(); // Look up fields.&lt;br /&gt;
    for (int i = 0; i &amp;lt; fields.length; i++)&lt;br /&gt;
      // Display them.&lt;br /&gt;
      print_field(fields[i]);&lt;br /&gt;
    System.out.println(&amp;quot;  // Methods&amp;quot;);&lt;br /&gt;
    Method[] methods = c.getDeclaredMethods(); // Look up methods.&lt;br /&gt;
    for (int i = 0; i &amp;lt; methods.length; i++)&lt;br /&gt;
      // Display them.&lt;br /&gt;
      print_method_or_constructor(methods[i]);&lt;br /&gt;
    System.out.println(&amp;quot;}&amp;quot;); // End class member listing.&lt;br /&gt;
  }&lt;br /&gt;
  /** Return the name of an interface or primitive type, handling arrays. */&lt;br /&gt;
  public static String typename(Class t) {&lt;br /&gt;
    String brackets = &amp;quot;&amp;quot;;&lt;br /&gt;
    while (t.isArray()) {&lt;br /&gt;
      brackets += &amp;quot;[]&amp;quot;;&lt;br /&gt;
      t = t.getComponentType();&lt;br /&gt;
    }&lt;br /&gt;
    String name = t.getName();&lt;br /&gt;
    int pos = name.lastIndexOf(&amp;quot;.&amp;quot;);&lt;br /&gt;
    if (pos != -1)&lt;br /&gt;
      name = name.substring(pos + 1);&lt;br /&gt;
    return name + brackets;&lt;br /&gt;
  }&lt;br /&gt;
  /** Return a string version of modifiers, handling spaces nicely. */&lt;br /&gt;
  public static String modifiers(int m) {&lt;br /&gt;
    if (m == 0)&lt;br /&gt;
      return &amp;quot;&amp;quot;;&lt;br /&gt;
    else&lt;br /&gt;
      return Modifier.toString(m) + &amp;quot; &amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
  /** Print the modifiers, type, and name of a field */&lt;br /&gt;
  public static void print_field(Field f) {&lt;br /&gt;
    System.out.println(&amp;quot;  &amp;quot; + modifiers(f.getModifiers()) + typename(f.getType()) + &amp;quot; &amp;quot;&lt;br /&gt;
        + f.getName() + &amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Print the modifiers, return type, name, parameter types and exception type&lt;br /&gt;
   * of a method or constructor. Note the use of the Member interface to allow&lt;br /&gt;
   * this method to work with both Method and Constructor objects&lt;br /&gt;
   */&lt;br /&gt;
  public static void print_method_or_constructor(Member member) {&lt;br /&gt;
    Class returntype = null, parameters[], exceptions[];&lt;br /&gt;
    if (member instanceof Method) {&lt;br /&gt;
      Method m = (Method) member;&lt;br /&gt;
      returntype = m.getReturnType();&lt;br /&gt;
      parameters = m.getParameterTypes();&lt;br /&gt;
      exceptions = m.getExceptionTypes();&lt;br /&gt;
      System.out.print(&amp;quot;  &amp;quot; + modifiers(member.getModifiers()) + typename(returntype) + &amp;quot; &amp;quot;&lt;br /&gt;
          + member.getName() + &amp;quot;(&amp;quot;);&lt;br /&gt;
    } else {&lt;br /&gt;
      Constructor c = (Constructor) member;&lt;br /&gt;
      parameters = c.getParameterTypes();&lt;br /&gt;
      exceptions = c.getExceptionTypes();&lt;br /&gt;
      System.out.print(&amp;quot;  &amp;quot; + modifiers(member.getModifiers()) + typename(c.getDeclaringClass())&lt;br /&gt;
          + &amp;quot;(&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    for (int i = 0; i &amp;lt; parameters.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0)&lt;br /&gt;
        System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(parameters[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.print(&amp;quot;)&amp;quot;);&lt;br /&gt;
    if (exceptions.length &amp;gt; 0)&lt;br /&gt;
      System.out.print(&amp;quot; throws &amp;quot;);&lt;br /&gt;
    for (int i = 0; i &amp;lt; exceptions.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0)&lt;br /&gt;
        System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(exceptions[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Call Private constructor ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved.&lt;br /&gt;
 *&lt;br /&gt;
 * Redistribution and use in source and binary forms, with or without&lt;br /&gt;
 * modification, are permitted provided that the following conditions&lt;br /&gt;
 * are met:&lt;br /&gt;
 *&lt;br /&gt;
 *   - Redistributions of source code must retain the above copyright&lt;br /&gt;
 *     notice, this list of conditions and the following disclaimer.&lt;br /&gt;
 *&lt;br /&gt;
 *   - Redistributions in binary form must reproduce the above copyright&lt;br /&gt;
 *     notice, this list of conditions and the following disclaimer in the&lt;br /&gt;
 *     documentation and/or other materials provided with the distribution.&lt;br /&gt;
 *&lt;br /&gt;
 *   - Neither the name of Sun Microsystems nor the names of its&lt;br /&gt;
 *     contributors may be used to endorse or promote products derived&lt;br /&gt;
 *     from this software without specific prior written permission.&lt;br /&gt;
 *&lt;br /&gt;
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &amp;quot;AS&lt;br /&gt;
 * IS&amp;quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,&lt;br /&gt;
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR&lt;br /&gt;
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR&lt;br /&gt;
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,&lt;br /&gt;
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,&lt;br /&gt;
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR&lt;br /&gt;
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF&lt;br /&gt;
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING&lt;br /&gt;
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS&lt;br /&gt;
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&lt;br /&gt;
 */&lt;br /&gt;
import java.lang.reflect.Constructor;&lt;br /&gt;
import java.lang.reflect.InvocationTargetException;&lt;br /&gt;
class Deny {&lt;br /&gt;
  private Deny() {&lt;br /&gt;
    System.out.format(&amp;quot;Deny constructor%n&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
public class ConstructorTroubleAccess {&lt;br /&gt;
  public static void main(String... args) {&lt;br /&gt;
    try {&lt;br /&gt;
      Constructor c = Deny.class.getDeclaredConstructor();&lt;br /&gt;
      // c.setAccessible(true); // solution&lt;br /&gt;
      c.newInstance();&lt;br /&gt;
      // production code should handle these exceptions more gracefully&lt;br /&gt;
    } catch (InvocationTargetException x) {&lt;br /&gt;
      x.printStackTrace();&lt;br /&gt;
    } catch (NoSuchMethodException x) {&lt;br /&gt;
      x.printStackTrace();&lt;br /&gt;
    } catch (InstantiationException x) {&lt;br /&gt;
      x.printStackTrace();&lt;br /&gt;
    } catch (IllegalAccessException x) {&lt;br /&gt;
      x.printStackTrace();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Create new instance from Constructor ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved.&lt;br /&gt;
 *&lt;br /&gt;
 * Redistribution and use in source and binary forms, with or without&lt;br /&gt;
 * modification, are permitted provided that the following conditions&lt;br /&gt;
 * are met:&lt;br /&gt;
 *&lt;br /&gt;
 *   - Redistributions of source code must retain the above copyright&lt;br /&gt;
 *     notice, this list of conditions and the following disclaimer.&lt;br /&gt;
 *&lt;br /&gt;
 *   - Redistributions in binary form must reproduce the above copyright&lt;br /&gt;
 *     notice, this list of conditions and the following disclaimer in the&lt;br /&gt;
 *     documentation and/or other materials provided with the distribution.&lt;br /&gt;
 *&lt;br /&gt;
 *   - Neither the name of Sun Microsystems nor the names of its&lt;br /&gt;
 *     contributors may be used to endorse or promote products derived&lt;br /&gt;
 *     from this software without specific prior written permission.&lt;br /&gt;
 *&lt;br /&gt;
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &amp;quot;AS&lt;br /&gt;
 * IS&amp;quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,&lt;br /&gt;
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR&lt;br /&gt;
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR&lt;br /&gt;
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,&lt;br /&gt;
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,&lt;br /&gt;
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR&lt;br /&gt;
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF&lt;br /&gt;
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING&lt;br /&gt;
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS&lt;br /&gt;
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&lt;br /&gt;
 */&lt;br /&gt;
import static java.lang.System.out;&lt;br /&gt;
import java.lang.reflect.Constructor;&lt;br /&gt;
import java.lang.reflect.InvocationTargetException;&lt;br /&gt;
import java.util.HashMap;&lt;br /&gt;
import java.util.Map;&lt;br /&gt;
import java.util.Set;&lt;br /&gt;
class EmailAliases {&lt;br /&gt;
  private Set&amp;lt;String&amp;gt; aliases;&lt;br /&gt;
  private EmailAliases(HashMap&amp;lt;String, String&amp;gt; h) {&lt;br /&gt;
    aliases = h.keySet();&lt;br /&gt;
  }&lt;br /&gt;
  public void printKeys() {&lt;br /&gt;
    out.format(&amp;quot;Mail keys:%n&amp;quot;);&lt;br /&gt;
    for (String k : aliases)&lt;br /&gt;
      out.format(&amp;quot;  %s%n&amp;quot;, k);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
public class RestoreAliases {&lt;br /&gt;
  private static Map&amp;lt;String, String&amp;gt; defaultAliases = new HashMap&amp;lt;String, String&amp;gt;();&lt;br /&gt;
  static {&lt;br /&gt;
    defaultAliases.put(&amp;quot;Duke&amp;quot;, &amp;quot;duke@i-love-java&amp;quot;);&lt;br /&gt;
    defaultAliases.put(&amp;quot;Fang&amp;quot;, &amp;quot;fang@evil-jealous-twin&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
  public static void main(String... args) {&lt;br /&gt;
    try {&lt;br /&gt;
      Constructor ctor = EmailAliases.class.getDeclaredConstructor(HashMap.class);&lt;br /&gt;
      ctor.setAccessible(true);&lt;br /&gt;
      EmailAliases email = (EmailAliases) ctor.newInstance(defaultAliases);&lt;br /&gt;
      email.printKeys();&lt;br /&gt;
      // production code should handle these exceptions more gracefully&lt;br /&gt;
    } catch (InstantiationException x) {&lt;br /&gt;
      x.printStackTrace();&lt;br /&gt;
    } catch (IllegalAccessException x) {&lt;br /&gt;
      x.printStackTrace();&lt;br /&gt;
    } catch (InvocationTargetException x) {&lt;br /&gt;
      x.printStackTrace();&lt;br /&gt;
    } catch (NoSuchMethodException x) {&lt;br /&gt;
      x.printStackTrace();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Demonstrates use of Constructor objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 *     file: ConstructorDemo.java&lt;br /&gt;
 *  package: oreilly.hcj.reflection&lt;br /&gt;
 *&lt;br /&gt;
 * This software is granted under the terms of the Common Public License,&lt;br /&gt;
 * CPL, which may be found at the following URL:&lt;br /&gt;
 * http://www-124.ibm.ru/developerworks/oss/CPLv1.0.htm&lt;br /&gt;
 *&lt;br /&gt;
 * Copyright(c) 2003-2005 by the authors indicated in the @author tags.&lt;br /&gt;
 * All Rights are Reserved by the various authors.&lt;br /&gt;
 *&lt;br /&gt;
########## DO NOT EDIT ABOVE THIS LINE ########## */&lt;br /&gt;
&lt;br /&gt;
import java.lang.reflect.Constructor;&lt;br /&gt;
/**  &lt;br /&gt;
 * Demonstrates use of Constructor objects.&lt;br /&gt;
 *&lt;br /&gt;
 * @author &lt;br /&gt;
 * @version $Revision$&lt;br /&gt;
 */&lt;br /&gt;
public class ConstructorDemo {&lt;br /&gt;
  /** &lt;br /&gt;
   * Run the demo.&lt;br /&gt;
   *&lt;br /&gt;
   * @param args Command line arguments.&lt;br /&gt;
   */&lt;br /&gt;
  public static void main(final String[] args) {&lt;br /&gt;
    try {&lt;br /&gt;
      final Class[] ARG_TYPES = new Class[] { String.class };&lt;br /&gt;
      Constructor cst = Integer.class.getConstructor(ARG_TYPES);&lt;br /&gt;
      System.out.println(cst.newInstance(new Object[] { &amp;quot;45&amp;quot; }));&lt;br /&gt;
    } catch (final Exception ex) {&lt;br /&gt;
      ex.printStackTrace();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
/* ########## End of File ########## */&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Get a compatible constructor for the given value type ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.lang.reflect.Array;&lt;br /&gt;
import java.lang.reflect.Constructor;&lt;br /&gt;
import java.lang.reflect.Method;&lt;br /&gt;
import java.lang.reflect.Modifier;&lt;br /&gt;
import java.net.URL;&lt;br /&gt;
import java.security.AccessController;&lt;br /&gt;
import java.security.CodeSource;&lt;br /&gt;
import java.security.PrivilegedAction;&lt;br /&gt;
import java.security.ProtectionDomain;&lt;br /&gt;
import java.util.HashMap;&lt;br /&gt;
import java.util.HashSet;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
import java.util.Map;&lt;br /&gt;
import java.util.Set;&lt;br /&gt;
/*&lt;br /&gt;
 * JBoss, Home of Professional Open Source&lt;br /&gt;
 * Copyright 2005, JBoss Inc., and individual contributors as indicated&lt;br /&gt;
 * by the @authors tag. See the copyright.txt in the distribution for a&lt;br /&gt;
 * full listing of individual contributors.&lt;br /&gt;
 *&lt;br /&gt;
 * This is free software; you can redistribute it and/or modify it&lt;br /&gt;
 * under the terms of the GNU Lesser General Public License as&lt;br /&gt;
 * published by the Free Software Foundation; either version 2.1 of&lt;br /&gt;
 * the License, or (at your option) any later version.&lt;br /&gt;
 *&lt;br /&gt;
 * This software is distributed in the hope that it will be useful,&lt;br /&gt;
 * but WITHOUT ANY WARRANTY; without even the implied warranty of&lt;br /&gt;
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU&lt;br /&gt;
 * Lesser General Public License for more details.&lt;br /&gt;
 *&lt;br /&gt;
 * You should have received a copy of the GNU Lesser General Public&lt;br /&gt;
 * License along with this software; if not, write to the Free&lt;br /&gt;
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA&lt;br /&gt;
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.&lt;br /&gt;
 */&lt;br /&gt;
public class Main {&lt;br /&gt;
  /////////////////////////////////////////////////////////////////////////&lt;br /&gt;
  //                           Coercion Methods                          //&lt;br /&gt;
  /////////////////////////////////////////////////////////////////////////&lt;br /&gt;
  /**&lt;br /&gt;
   * Get a compatible constructor for the given value type&lt;br /&gt;
   *&lt;br /&gt;
   * @param type       Class to look for constructor in&lt;br /&gt;
   * @param valueType  Argument type for constructor&lt;br /&gt;
   * @return           Constructor or null&lt;br /&gt;
   */&lt;br /&gt;
  public static Constructor getCompatibleConstructor(final Class type,&lt;br /&gt;
                                                     final Class valueType)&lt;br /&gt;
  {&lt;br /&gt;
     // first try and find a constructor with the exact argument type&lt;br /&gt;
     try {&lt;br /&gt;
        return type.getConstructor(new Class[] { valueType });&lt;br /&gt;
     }&lt;br /&gt;
     catch (Exception ignore) {&lt;br /&gt;
        // if the above failed, then try and find a constructor with&lt;br /&gt;
        // an compatible argument type&lt;br /&gt;
        // get an array of compatible types&lt;br /&gt;
        Class[] types = type.getClasses();&lt;br /&gt;
        for (int i=0; i&amp;lt;types.length; i++) {&lt;br /&gt;
           try {&lt;br /&gt;
              return type.getConstructor(new Class[] { types[i] });&lt;br /&gt;
           }&lt;br /&gt;
           catch (Exception ignore2) {}&lt;br /&gt;
        }&lt;br /&gt;
     }&lt;br /&gt;
     // if we get this far, then we can&amp;quot;t find a compatible constructor&lt;br /&gt;
     return null;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Get all constructors or by parameters ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.lang.reflect.Constructor;&lt;br /&gt;
public class GetConstructor {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        Constructor[] cs = String.class.getConstructors();&lt;br /&gt;
        for(int i=0;i&amp;lt;cs.length;i++){&lt;br /&gt;
            System.out.println(cs[i]);        &lt;br /&gt;
        }&lt;br /&gt;
        try {&lt;br /&gt;
            Constructor c = String.class.getConstructor(new Class[]{String.class});&lt;br /&gt;
            System.out.println(c); &lt;br /&gt;
        } catch (SecurityException e) {&lt;br /&gt;
            e.printStackTrace();&lt;br /&gt;
        } catch (NoSuchMethodException e) {&lt;br /&gt;
            e.printStackTrace();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Get constructor by parameter type ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved.&lt;br /&gt;
 *&lt;br /&gt;
 * Redistribution and use in source and binary forms, with or without&lt;br /&gt;
 * modification, are permitted provided that the following conditions&lt;br /&gt;
 * are met:&lt;br /&gt;
 *&lt;br /&gt;
 *   - Redistributions of source code must retain the above copyright&lt;br /&gt;
 *     notice, this list of conditions and the following disclaimer.&lt;br /&gt;
 *&lt;br /&gt;
 *   - Redistributions in binary form must reproduce the above copyright&lt;br /&gt;
 *     notice, this list of conditions and the following disclaimer in the&lt;br /&gt;
 *     documentation and/or other materials provided with the distribution.&lt;br /&gt;
 *&lt;br /&gt;
 *   - Neither the name of Sun Microsystems nor the names of its&lt;br /&gt;
 *     contributors may be used to endorse or promote products derived&lt;br /&gt;
 *     from this software without specific prior written permission.&lt;br /&gt;
 *&lt;br /&gt;
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &amp;quot;AS&lt;br /&gt;
 * IS&amp;quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,&lt;br /&gt;
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR&lt;br /&gt;
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR&lt;br /&gt;
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,&lt;br /&gt;
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,&lt;br /&gt;
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR&lt;br /&gt;
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF&lt;br /&gt;
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING&lt;br /&gt;
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS&lt;br /&gt;
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&lt;br /&gt;
 */&lt;br /&gt;
import static java.lang.System.out;&lt;br /&gt;
import java.lang.reflect.InvocationTargetException;&lt;br /&gt;
public class ConstructorTroubleAgain {&lt;br /&gt;
  public ConstructorTroubleAgain() {&lt;br /&gt;
  }&lt;br /&gt;
  public ConstructorTroubleAgain(Integer i) {&lt;br /&gt;
  }&lt;br /&gt;
  public ConstructorTroubleAgain(Object o) {&lt;br /&gt;
    out.format(&amp;quot;Constructor passed Object%n&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
  public ConstructorTroubleAgain(String s) {&lt;br /&gt;
    out.format(&amp;quot;Constructor passed String%n&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
  public static void main(String... args) {&lt;br /&gt;
    String argType = (args.length == 0 ? &amp;quot;&amp;quot; : args[0]);&lt;br /&gt;
    try {&lt;br /&gt;
      Class&amp;lt;?&amp;gt; c = Class.forName(&amp;quot;ConstructorTroubleAgain&amp;quot;);&lt;br /&gt;
      if (&amp;quot;&amp;quot;.equals(argType)) {&lt;br /&gt;
        // IllegalArgumentException: wrong number of arguments&lt;br /&gt;
        Object o = c.getConstructor().newInstance(&amp;quot;foo&amp;quot;);&lt;br /&gt;
      } else if (&amp;quot;int&amp;quot;.equals(argType)) {&lt;br /&gt;
        // NoSuchMethodException - looking for int, have Integer&lt;br /&gt;
        Object o = c.getConstructor(int.class);&lt;br /&gt;
      } else if (&amp;quot;Object&amp;quot;.equals(argType)) {&lt;br /&gt;
        // newInstance() does not perform method resolution&lt;br /&gt;
        Object o = c.getConstructor(Object.class).newInstance(&amp;quot;foo&amp;quot;);&lt;br /&gt;
      } else {&lt;br /&gt;
        assert false;&lt;br /&gt;
      }&lt;br /&gt;
      // production code should handle these exceptions more gracefully&lt;br /&gt;
    } catch (ClassNotFoundException x) {&lt;br /&gt;
      x.printStackTrace();&lt;br /&gt;
    } catch (NoSuchMethodException x) {&lt;br /&gt;
      x.printStackTrace();&lt;br /&gt;
    } catch (InvocationTargetException x) {&lt;br /&gt;
      x.printStackTrace();&lt;br /&gt;
    } catch (InstantiationException x) {&lt;br /&gt;
      x.printStackTrace();&lt;br /&gt;
    } catch (IllegalAccessException x) {&lt;br /&gt;
      x.printStackTrace();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Getting a Constructor of a Class Object: By obtaining a list of all Constructors object ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.lang.reflect.Constructor;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] argv) throws Exception {&lt;br /&gt;
    Constructor[] cons = String.class.getDeclaredConstructors();&lt;br /&gt;
    for (int i = 0; i &amp;lt; cons.length; i++) {&lt;br /&gt;
      Class[] paramTypes = cons[i].getParameterTypes();&lt;br /&gt;
      System.out.println(cons[i]);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Getting a Constructor of a Class Object: By obtaining a particular Constructor object. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.lang.reflect.Constructor;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] argv) throws Exception {&lt;br /&gt;
    Constructor con = java.awt.Point.class.getConstructor(new Class[] { int.class, int.class });&lt;br /&gt;
    System.out.println(con);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Has Declared Constructor ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.lang.reflect.Constructor;&lt;br /&gt;
public class ReflectionUtils {&lt;br /&gt;
  public static boolean hasDeclaredConstructor(Class targetClass, Class[] partypes) {&lt;br /&gt;
    Constructor constructor = null;&lt;br /&gt;
    try {&lt;br /&gt;
      constructor = targetClass.getConstructor(partypes);&lt;br /&gt;
    }catch (Exception e) {&lt;br /&gt;
      e.printStackTrace();&lt;br /&gt;
    }&lt;br /&gt;
    return constructor != null;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Invoke a constructor which throws Exception ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved.&lt;br /&gt;
 *&lt;br /&gt;
 * Redistribution and use in source and binary forms, with or without&lt;br /&gt;
 * modification, are permitted provided that the following conditions&lt;br /&gt;
 * are met:&lt;br /&gt;
 *&lt;br /&gt;
 *   - Redistributions of source code must retain the above copyright&lt;br /&gt;
 *     notice, this list of conditions and the following disclaimer.&lt;br /&gt;
 *&lt;br /&gt;
 *   - Redistributions in binary form must reproduce the above copyright&lt;br /&gt;
 *     notice, this list of conditions and the following disclaimer in the&lt;br /&gt;
 *     documentation and/or other materials provided with the distribution.&lt;br /&gt;
 *&lt;br /&gt;
 *   - Neither the name of Sun Microsystems nor the names of its&lt;br /&gt;
 *     contributors may be used to endorse or promote products derived&lt;br /&gt;
 *     from this software without specific prior written permission.&lt;br /&gt;
 *&lt;br /&gt;
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &amp;quot;AS&lt;br /&gt;
 * IS&amp;quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,&lt;br /&gt;
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR&lt;br /&gt;
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR&lt;br /&gt;
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,&lt;br /&gt;
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,&lt;br /&gt;
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR&lt;br /&gt;
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF&lt;br /&gt;
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING&lt;br /&gt;
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS&lt;br /&gt;
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&lt;br /&gt;
 */&lt;br /&gt;
import static java.lang.System.err;&lt;br /&gt;
import java.lang.reflect.InvocationTargetException;&lt;br /&gt;
public class ConstructorTroubleToo {&lt;br /&gt;
  public ConstructorTroubleToo() {&lt;br /&gt;
    throw new RuntimeException(&amp;quot;exception in constructor&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
  public static void main(String... args) {&lt;br /&gt;
    try {&lt;br /&gt;
      Class&amp;lt;?&amp;gt; c = Class.forName(&amp;quot;ConstructorTroubleToo&amp;quot;);&lt;br /&gt;
      // Method propagetes any exception thrown by the constructor (including checked exceptions).&lt;br /&gt;
      if (args.length &amp;gt; 0 &amp;amp;&amp;amp; args[0].equals(&amp;quot;class&amp;quot;)) {&lt;br /&gt;
        Object o = c.newInstance();&lt;br /&gt;
      } else {&lt;br /&gt;
        Object o = c.getConstructor().newInstance();&lt;br /&gt;
      }&lt;br /&gt;
      // production code should handle these exceptions more gracefully&lt;br /&gt;
    } catch (ClassNotFoundException x) {&lt;br /&gt;
      x.printStackTrace();&lt;br /&gt;
    } catch (InstantiationException x) {&lt;br /&gt;
      x.printStackTrace();&lt;br /&gt;
    } catch (IllegalAccessException x) {&lt;br /&gt;
      x.printStackTrace();&lt;br /&gt;
    } catch (NoSuchMethodException x) {&lt;br /&gt;
      x.printStackTrace();&lt;br /&gt;
    } catch (InvocationTargetException x) {&lt;br /&gt;
      x.printStackTrace();&lt;br /&gt;
      err.format(&amp;quot;%n%nCaught exception: %s%n&amp;quot;, x.getCause());&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Load class with Class.forName ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved.&lt;br /&gt;
 *&lt;br /&gt;
 * Redistribution and use in source and binary forms, with or without&lt;br /&gt;
 * modification, are permitted provided that the following conditions&lt;br /&gt;
 * are met:&lt;br /&gt;
 *&lt;br /&gt;
 *   - Redistributions of source code must retain the above copyright&lt;br /&gt;
 *     notice, this list of conditions and the following disclaimer.&lt;br /&gt;
 *&lt;br /&gt;
 *   - Redistributions in binary form must reproduce the above copyright&lt;br /&gt;
 *     notice, this list of conditions and the following disclaimer in the&lt;br /&gt;
 *     documentation and/or other materials provided with the distribution.&lt;br /&gt;
 *&lt;br /&gt;
 *   - Neither the name of Sun Microsystems nor the names of its&lt;br /&gt;
 *     contributors may be used to endorse or promote products derived&lt;br /&gt;
 *     from this software without specific prior written permission.&lt;br /&gt;
 *&lt;br /&gt;
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &amp;quot;AS&lt;br /&gt;
 * IS&amp;quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,&lt;br /&gt;
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR&lt;br /&gt;
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR&lt;br /&gt;
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,&lt;br /&gt;
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,&lt;br /&gt;
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR&lt;br /&gt;
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF&lt;br /&gt;
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING&lt;br /&gt;
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS&lt;br /&gt;
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&lt;br /&gt;
 */&lt;br /&gt;
public class ConstructorTrouble {&lt;br /&gt;
  private ConstructorTrouble(int i) {&lt;br /&gt;
  }&lt;br /&gt;
  public static void main(String... args) {&lt;br /&gt;
    try {&lt;br /&gt;
      Class&amp;lt;?&amp;gt; c = Class.forName(&amp;quot;ConstructorTrouble&amp;quot;);&lt;br /&gt;
      Object o = c.newInstance(); // InstantiationException&lt;br /&gt;
      // production code should handle these exceptions more gracefully&lt;br /&gt;
    } catch (ClassNotFoundException x) {&lt;br /&gt;
      x.printStackTrace();&lt;br /&gt;
    } catch (InstantiationException x) {&lt;br /&gt;
      x.printStackTrace();&lt;br /&gt;
    } catch (IllegalAccessException x) {&lt;br /&gt;
      x.printStackTrace();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved.&lt;br /&gt;
 *&lt;br /&gt;
 * Redistribution and use in source and binary forms, with or without&lt;br /&gt;
 * modification, are permitted provided that the following conditions&lt;br /&gt;
 * are met:&lt;br /&gt;
 *&lt;br /&gt;
 *   - Redistributions of source code must retain the above copyright&lt;br /&gt;
 *     notice, this list of conditions and the following disclaimer.&lt;br /&gt;
 *&lt;br /&gt;
 *   - Redistributions in binary form must reproduce the above copyright&lt;br /&gt;
 *     notice, this list of conditions and the following disclaimer in the&lt;br /&gt;
 *     documentation and/or other materials provided with the distribution.&lt;br /&gt;
 *&lt;br /&gt;
 *   - Neither the name of Sun Microsystems nor the names of its&lt;br /&gt;
 *     contributors may be used to endorse or promote products derived&lt;br /&gt;
 *     from this software without specific prior written permission.&lt;br /&gt;
 *&lt;br /&gt;
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &amp;quot;AS&lt;br /&gt;
 * IS&amp;quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,&lt;br /&gt;
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR&lt;br /&gt;
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR&lt;br /&gt;
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,&lt;br /&gt;
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,&lt;br /&gt;
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR&lt;br /&gt;
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF&lt;br /&gt;
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING&lt;br /&gt;
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS&lt;br /&gt;
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&lt;br /&gt;
 */&lt;br /&gt;
import static java.lang.System.out;&lt;br /&gt;
import java.lang.reflect.Constructor;&lt;br /&gt;
import java.lang.reflect.Modifier;&lt;br /&gt;
public class ConstructorAccess {&lt;br /&gt;
  public static void main(String... args) {&lt;br /&gt;
    try {&lt;br /&gt;
      Class&amp;lt;?&amp;gt; c = Class.forName(args[0]);&lt;br /&gt;
      Constructor[] allConstructors = c.getDeclaredConstructors();&lt;br /&gt;
      for (Constructor ctor : allConstructors) {&lt;br /&gt;
        int searchMod = modifierFromString(args[1]);&lt;br /&gt;
        int mods = accessModifiers(ctor.getModifiers());&lt;br /&gt;
        if (searchMod == mods) {&lt;br /&gt;
          out.format(&amp;quot;%s%n&amp;quot;, ctor.toGenericString());&lt;br /&gt;
          out.format(&amp;quot;  [ synthetic=%-5b var_args=%-5b ]%n&amp;quot;,&lt;br /&gt;
              ctor.isSynthetic(), ctor.isVarArgs());&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
      // production code should handle this exception more gracefully&lt;br /&gt;
    } catch (ClassNotFoundException x) {&lt;br /&gt;
      x.printStackTrace();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  private static int accessModifiers(int m) {&lt;br /&gt;
    return m &amp;amp; (Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED);&lt;br /&gt;
  }&lt;br /&gt;
  private static int modifierFromString(String s) {&lt;br /&gt;
    if (&amp;quot;public&amp;quot;.equals(s))&lt;br /&gt;
      return Modifier.PUBLIC;&lt;br /&gt;
    else if (&amp;quot;protected&amp;quot;.equals(s))&lt;br /&gt;
      return Modifier.PROTECTED;&lt;br /&gt;
    else if (&amp;quot;private&amp;quot;.equals(s))&lt;br /&gt;
      return Modifier.PRIVATE;&lt;br /&gt;
    else if (&amp;quot;package-private&amp;quot;.equals(s))&lt;br /&gt;
      return 0;&lt;br /&gt;
    else&lt;br /&gt;
      return -1;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Passing a parameter to the constructor and calling a method dynamically ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String args[]) throws Exception{&lt;br /&gt;
      String name = &amp;quot;java.lang.String&amp;quot;;&lt;br /&gt;
      String methodName = &amp;quot;toLowerCase&amp;quot;;&lt;br /&gt;
      Class cl = Class.forName(name);&lt;br /&gt;
      java.lang.reflect.Constructor constructor = cl.getConstructor(new Class[] { String.class });&lt;br /&gt;
      Object invoker = constructor.newInstance(new Object[] { &amp;quot;AAA&amp;quot; });&lt;br /&gt;
      Class arguments[] = new Class[] {};&lt;br /&gt;
      java.lang.reflect.Method objMethod = cl.getMethod(methodName, arguments);&lt;br /&gt;
      Object result = objMethod.invoke(invoker, (Object[]) arguments);&lt;br /&gt;
      System.out.println(result);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>