Java Class.getConstructors Examples

Java Class.getConstructors - 30 examples found. These are the top rated real world Java examples of Class.getConstructors extracted from open source projects. You can rate examples to help us improve the quality of examples.
  public void initTransactionManager(Environment env) {
    Object tm = env.get(EnvironmentName.TRANSACTION_MANAGER);
    if (env.get(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER) != null
        && env.get(EnvironmentName.TRANSACTION_MANAGER) != null) {
      this.txm = (TransactionManager) tm;
      this.jpm = (PersistenceContextManager) env.get(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER);
    } else {
      if (tm != null && tm.getClass().getName().startsWith("org.springframework")) {
        try {
          Class<?> cls =
              Class.forName(
                  "org.drools.container.spring.beans.persistence.DroolsSpringTransactionManager");
          Constructor<?> con = cls.getConstructors()[0];
          this.txm = (TransactionManager) con.newInstance(tm);
          logger.debug("Instantiating  DroolsSpringTransactionManager");

          //                    if ( tm.getClass().getName().toLowerCase().contains( "jpa" ) ) {
          // configure spring for JPA and local transactions
          cls =
              Class.forName("org.drools.container.spring.beans.persistence.DroolsSpringJpaManager");
          con = cls.getConstructors()[0];
          this.jpm = (PersistenceContextManager) con.newInstance(new Object[] {this.env});
          //                    } else {
          //                        // configure spring for JPA and distributed transactions
          //                    }
        } catch (Exception e) {
          logger.warn("Could not instatiate DroolsSpringTransactionManager");
          throw new RuntimeException(
              "Could not instatiate org.drools.container.spring.beans.persistence.DroolsSpringTransactionManager",
              e);
        }
      } else {
        logger.debug("Instantiating  JtaTransactionManager");
        this.txm =
            new JtaTransactionManager(
                env.get(EnvironmentName.TRANSACTION),
                env.get(EnvironmentName.TRANSACTION_SYNCHRONIZATION_REGISTRY),
                tm);
        try {
          Class<?> jpaPersistenceCtxMngrClass =
              Class.forName("org.jbpm.persistence.JpaProcessPersistenceContextManager");
          Constructor<?> jpaPersistenceCtxMngrCtor =
              jpaPersistenceCtxMngrClass.getConstructors()[0];
          this.jpm =
              (PersistenceContextManager)
                  jpaPersistenceCtxMngrCtor.newInstance(new Object[] {this.env});
        } catch (ClassNotFoundException e) {
          this.jpm = new JpaPersistenceContextManager(this.env);
        } catch (Exception e) {
          throw new RuntimeException("Error creating JpaProcessPersistenceContextManager", e);
        }
      }
      env.set(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER, this.jpm);
      env.set(EnvironmentName.TRANSACTION_MANAGER, this.txm);
    }
  }
Example #2
0
    public void generateObjects(String arrayName, Object... parent) {
      if (mRec == null) return;

      Object rawObj;
      try {
        rawObj = mParser.parse(mJson);
        if (rawObj == null) return;

        JSONObject obj = (JSONObject) rawObj;
        JSONArray arr = (JSONArray) obj.get(arrayName);
        int cnt = 0;
        if (arr != null) {
          for (Object jb : arr.toArray()) // each object
          {
            JSONObject jo = (JSONObject) jb;
            mCls.getConstructors()[0].setAccessible(true);

            Object w;
            if (parent != null && parent.length != 0) // Inner class
            w = mCls.getConstructors()[0].newInstance(parent[0]);
            else {
              w = mCls.getConstructors()[0].newInstance((Object) null);
            }
            for (Field f : mCls.getDeclaredFields()) // each field
            {
              try {
                Object fieldVal = jo.get(f.getName());
                if (fieldVal == null) continue;
                if (f.getType() == Integer.class) f.set(w, Integer.parseInt(fieldVal.toString()));
                else if (f.getType() == Date.class)
                  f.set(w, new Date(Long.parseLong(fieldVal.toString())));
                else if (f.getType() == String.class) f.set(w, fieldVal.toString());
              } catch (IllegalArgumentException e) {
                e.printStackTrace();
              } catch (IllegalAccessException e) {
                e.printStackTrace();
              }
            }
            mRec.receiveObject(w);
          }
        }
      } catch (ParseException e1) {
        e1.printStackTrace();
      } catch (IllegalArgumentException e1) {
        e1.printStackTrace();
      } catch (SecurityException e1) {
        e1.printStackTrace();
      } catch (InstantiationException e1) {
        e1.printStackTrace();
      } catch (IllegalAccessException e1) {
        e1.printStackTrace();
      } catch (InvocationTargetException e1) {
        e1.printStackTrace();
      }
    }
  public CepQuery createCepQuery(final String command) {
    // TODO: Correct it, it should NEVER return null!
    Class<?> c = null;
    CepQuery e = null;

    try {
      c = Class.forName(this.cepQueryName);

      final Constructor<?>[] ctors = c.getConstructors();
      e = (CepQuery) ctors[0].newInstance(command);
      return e;

    } catch (final IllegalAccessException ex) {
      ex.printStackTrace();
      return null;
    } catch (final ClassNotFoundException ex) {
      ex.printStackTrace();
      return null;
    } catch (final InstantiationException ex) {
      ex.printStackTrace();
      return null;
    } catch (final IllegalArgumentException ex) {
      // TODO Auto-generated catch block
      ex.printStackTrace();
    } catch (final InvocationTargetException ex) {
      // TODO Auto-generated catch block
      ex.printStackTrace();
    }

    return null;
  }
Example #4
0
  protected ArrayList createListCompletions(Package setOfCompletions) {
    ArrayList listOfCompletions = new ArrayList();

    for (Item i : setOfCompletions) {
      try {
        try {
          if (i instanceof ClassName) {
            String fullName = getFullName(setOfCompletions.getName(), ((ClassName) i).getName());
            Class clazz = getClass().getClassLoader().loadClass(fullName);
            Constructor[] ctor = clazz.getConstructors();

            for (Constructor c : ctor) {
              String cotrCompletion = createConstructorCompletion(c.toString());
              listOfCompletions.add(new BasicCompletion(defaultProvider, cotrCompletion));
            }
            listOfCompletions.add(new BasicCompletion(defaultProvider, i.getName() + "."));
          }
        } catch (NoClassDefFoundError e) {
          e.printStackTrace();
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
      listOfCompletions.add(new BasicCompletion(defaultProvider, i.getName()));
    }
    return listOfCompletions;
  }
 // ------ Common stuff
 static Model createModel(
     String simClass, String _ownerName, java.awt.Frame _ownerFrame, java.net.URL _codebase) {
   Model aModel = null;
   if (_ownerName != null || _codebase != null) {
     try { // Instantiate a model with the given name and three parameters
       Class c = Class.forName(simClass);
       Constructor[] constructors = c.getConstructors();
       for (int i = 0; i < constructors.length; i++) {
         Class[] parameters = constructors[i].getParameterTypes();
         if (parameters.length == 3
             && parameters[0].isAssignableFrom(_ownerName.getClass())
             && parameters[1].isAssignableFrom(_ownerFrame.getClass())
             && parameters[2].isAssignableFrom(_codebase.getClass())) {
           aModel =
               (Model)
                   constructors[i].newInstance(new Object[] {_ownerName, _ownerFrame, _codebase});
           break;
         }
       }
     } catch (Exception exc) {
       exc.printStackTrace();
       aModel = null;
     }
   }
   if (aModel == null) {
     try { // Now try a simple constructor
       Class aClass = Class.forName(simClass);
       aModel = (Model) aClass.newInstance();
     } catch (Exception exc) {
       exc.printStackTrace();
       return null;
     }
   }
   return aModel;
 }
Example #6
0
 protected <T> Constructor<T> findMatchingCtor(Class<T> clazz, Object... args) {
   List<Constructor> ctors = new ArrayList<Constructor>();
   for (Constructor ct : clazz.getConstructors()) {
     if (isCompatible(ct.getParameterTypes(), args)) {
       ctors.add(ct);
     }
   }
   if (ctors.isEmpty()) {
     return null;
   }
   Collections.sort(
       ctors,
       new Comparator<Constructor>() {
         @Override
         public int compare(Constructor o1, Constructor o2) {
           int sum = 0;
           for (Class c0 : o1.getParameterTypes()) {
             for (Class c1 : o2.getParameterTypes()) {
               sum += classDist(c0, c1);
             }
           }
           return sum;
         }
       });
   //noinspection unchecked
   return ctors.get(0);
 }