List of usage examples for java.beans Beans instantiate
public static Object instantiate(ClassLoader cls, String beanName) throws IOException, ClassNotFoundException
Instantiate a JavaBean.
From source file:Main.java
public static void main(String[] args) throws Exception { Main bean = (Main) Beans.instantiate(ClassLoader.getSystemClassLoader(), "Main"); System.out.println("The Bean = " + bean); }
From source file:Main.java
public static void main(String[] argv) throws Exception { MyBean bean = (MyBean) Beans.instantiate(ClassLoader.getSystemClassLoader(), "MyBean"); }
From source file:MainClass.java
public MainClass(String name) { super("Revived Beans!"); try {/*www .jav a 2 s. c o m*/ Object bean = Beans.instantiate(getClass().getClassLoader(), name); if (Beans.isInstanceOf(bean, JComponent.class)) { JComponent comp = (JComponent) Beans.getInstanceOf(bean, JComponent.class); getContentPane().add("Center", comp); } else { System.out.println("Bean is not a Component..."); } } catch (java.io.IOException e1) { System.out.println("Error loading the serialized object"); } catch (ClassNotFoundException e2) { System.out.println("Can't find the class that goes with the object"); } }
From source file:org.fudgemsg.mapping.JavaBeanBuilder.java
@SuppressWarnings("unchecked") private T newBeanObject() throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException, IOException, ClassNotFoundException { if (getConstructor() != null) { return getConstructor().newInstance(); } else {/*from w w w. ja v a2 s . com*/ // Warning: the Beans.instantiate method below was about 5 times slower in the perf tests return (T) Beans.instantiate(getClass().getClassLoader(), getBeanName()); } }
From source file:org.toryt.util_I.beanfinder.InstantiatingFqcnBeanFinder.java
public final Object findFor(_Argument_ argument) throws BeanFinderConfigurationException, NoBeanFoundException { assert argument != null; String fqcn = fqcn(argument); // throws NoBeanFoundException _LOG.debug(" trying to instantiate class with FQCN " + fqcn + " with default constructor"); Object bean = null;/*from w w w . ja v a2 s . com*/ try { bean = Beans.instantiate(null, fqcn); assert bean != null; if (!(getBeanType().isInstance(bean))) { ClassCastException ccExc = new ClassCastException( bean + " cannot be cast to type " + getBeanType()); logInstantiationProblem1(argument, fqcn, ccExc); } _LOG.debug("Created bean for argument " + argument + " from class " + fqcn); } catch (IOException ioExc) { logInstantiationProblem1(argument, fqcn, ioExc); // throws exception } catch (NullPointerException npExc) { // undocumented; if fqcn is null logInstantiationProblem2(argument, fqcn, npExc); // throws exception } catch (ClassNotFoundException cnfExc) { logInstantiationProblem2(argument, fqcn, cnfExc); } /* it is weird that we cannot get other exceptions here, according to the Beans.instantiate * Javadoc: how about exceptions in the constructor, having no default constructor, etc. * Solution is found in source code of the instantiate method: such exceptions * are all recast to ClassCastException. */ assert bean != null; return bean; }
From source file:org.toryt_II.testobject.DefaultTofPlFactory.java
/** * @pre forClass != null;/*from w ww.j a va 2s . c o m*/ * @pre getCachedTofPl(forClass) == null; */ private <_ForClass_> TestObjectFactoryPriorityList<_ForClass_> getTofPlFromSystemProperty( Class<_ForClass_> forClass) { assert forClass != null; assert getCachedTofPl(forClass) == null; assert forClass == null; _LOG.debug("trying to instantiate TOF PL for class " + forClass.getName() + " via FQCN in system property"); Properties properties = System.getProperties(); String key = SYSTEM_PROPERTY_KEY_PREFIX + forClass.getName(); _LOG.debug(" system property key: \"" + key + "\""); String tofPlFqcn = properties.getProperty(key); _LOG.debug(" value of system property: \"" + tofPlFqcn + "\""); _LOG.debug(" trying to instantiate class with FQCN " + tofPlFqcn + " with default constructor"); try { @SuppressWarnings("unchecked") TestObjectFactoryPriorityList<_ForClass_> result = (TestObjectFactoryPriorityList<_ForClass_>) Beans .instantiate(null, tofPlFqcn); // runtime cannot check against generic type instantiation _LOG.info("Created TOF PL for class " + forClass + " from class " + tofPlFqcn + " (value of system property " + key + ")"); addCachedTofPl(forClass, result); return result; } catch (AlreadyHasTofPlForClassException e) { assert false : "AlreadyHasTofPlForClassException should not happen: " + e; } catch (IOException ioExc) { logTofPlFromSystemPropertyInstantiationProblem(forClass, tofPlFqcn, key, ioExc); } catch (ClassNotFoundException cnfExc) { logTofPlFromSystemPropertyInstantiationProblem(forClass, tofPlFqcn, key, cnfExc); } catch (ClassCastException ccExc) { logTofPlFromSystemPropertyInstantiationProblem(forClass, tofPlFqcn, key, ccExc); } return null; // keep compiler happy }
From source file:poke.server.managers.ElectionManager.java
private Election createElection() { if (election == null) { synchronized (syncPt) { // new election String clazz = ElectionManager.conf.getElectionImplementation(); // if an election instance already existed, this would // override the current election try { election = (Election) Beans.instantiate(this.getClass().getClassLoader(), clazz); election.setNodeId(conf.getNodeId()); election.setListener(this); // this sucks - bad coding here! should use configuration // properties if (election instanceof FloodMaxElection) { logger.warn("Node " + conf.getNodeId() + " setting max hops to arbitrary value (5)"); ((FloodMaxElection) election).setMaxHops(4); }// w w w.j a v a 2 s. co m } catch (Exception e) { logger.error("Failed to create " + clazz, e); } } } return election; }