Java Class.newInstance Examples

Java Class.newInstance - 30 examples found. These are the top rated real world Java examples of Class.newInstance extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
  private Module[] getModules(Guice guice, Class<?> testClass) {
    List<Module> result = Lists.newArrayList();
    for (Class<? extends Module> moduleClass : guice.modules()) {
      try {
        result.add(moduleClass.newInstance());
      } catch (InstantiationException e) {
        throw new TestNGException(e);
      } catch (IllegalAccessException e) {
        throw new TestNGException(e);
      }
    }
    Class<? extends IModuleFactory> factory = guice.moduleFactory();
    if (factory != IModuleFactory.class) {
      try {
        IModuleFactory factoryInstance = factory.newInstance();
        Module moduleClass = factoryInstance.createModule(m_testContext, testClass);
        if (moduleClass != null) {
          result.add(moduleClass);
        }
      } catch (InstantiationException e) {
        throw new TestNGException(e);
      } catch (IllegalAccessException e) {
        throw new TestNGException(e);
      }
    }

    return result.toArray(new Module[result.size()]);
  }
Example #2
0
  private void showFragment(Class<?> paramClass, String paramString, boolean addToBackStack) {
    Log.d(TAG, "showFragment for " + paramClass);

    FragmentManager localFragmentManager = getSupportFragmentManager();

    Fragment localFragment = localFragmentManager.findFragmentById(R.id.fragment_container);

    if ((localFragment == null) || (!paramClass.isInstance(localFragment))) {
      try {
        Log.d(TAG, "replacing fragments");

        if (addToBackStack) {

          localFragment = (Fragment) paramClass.newInstance();
          localFragmentManager
              .beginTransaction()
              .add(R.id.fragment_container, localFragment)
              .addToBackStack("growth_stack")
              .commit();

        } else {
          localFragment = (Fragment) paramClass.newInstance();
          localFragmentManager
              .beginTransaction()
              .replace(R.id.fragment_container, localFragment)
              .commitAllowingStateLoss();
        }

      } catch (InstantiationException e) {
        e.printStackTrace();
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      }
    }
  }
Example #3
0
 public void testExtractionAndMetric()
     throws IOException, IllegalAccessException, InstantiationException {
   for (Class c : featureClasses) {
     LireFeature lireFeature = (LireFeature) c.newInstance();
     LireFeature tmpLireFeature = (LireFeature) c.newInstance();
     for (String file : testFiles) {
       System.out.println(c.getName() + ": " + file);
       BufferedImage image = ImageIO.read(new FileInputStream(testFilesPath + file));
       //                image = ImageUtils.trimWhiteSpace(image);
       lireFeature.extract(image);
       float delta = 0.0000f;
       assertEquals(lireFeature.getDistance(lireFeature), 0, delta);
       //
       // tmpLireFeature.setStringRepresentation(lireFeature.getStringRepresentation());
       //                assertEquals(lireFeature.getDistance(tmpLireFeature), 0, delta);
       tmpLireFeature.setByteArrayRepresentation(lireFeature.getByteArrayRepresentation());
       assertEquals(lireFeature.getDistance(tmpLireFeature), 0, delta);
       tmpLireFeature.setByteArrayRepresentation(
           lireFeature.getByteArrayRepresentation(),
           0,
           lireFeature.getByteArrayRepresentation().length);
       assertEquals(lireFeature.getDistance(tmpLireFeature), 0, delta);
     }
   }
 }
 @Override
 protected DATA convert(@NonNull JSONObject jsonObject) throws ConvertException {
   try {
     DATA data = null;
     if (mSingler != null) {
       String key = mSingler.primary();
       Field field = mAnnotationMap.get(key);
       if (field == null) {
         throw new RuntimeException("Primary key must be existed and be annotated by Property.");
       }
       Object index = ReflectUtils.getValueFromField(jsonObject, key, field);
       data = SingleData.obtain(mDataClazz, index);
       if (data == null) {
         data = mDataClazz.newInstance();
         SingleData.updateData(mDataClazz, index, data);
       }
     }
     if (data == null) {
       data = mDataClazz.newInstance();
     }
     for (Map.Entry<String, Field> entry : mAnnotationMap.entrySet()) {
       Field field = entry.getValue();
       String key = entry.getKey();
       Object value = ReflectUtils.getValueFromField(jsonObject, key, field);
       if (value != null) {
         ReflectUtils.setValue(data, field, value);
       }
     }
     return data;
   } catch (InstantiationException e) {
     throw new ConvertException(e);
   } catch (IllegalAccessException e) {
     throw new ConvertException(e);
   }
 }
 private void process(FixtureConfig fixtureConfig) {
   log.debug("Processing " + fixtureConfig);
   String sourceClassStr = fixtureConfig.getSourceClass();
   String sourceProcessorClassStr = fixtureConfig.getSourceProcessorClass();
   try {
     Class<?> sourceClassInst = Class.forName(sourceClassStr);
     IStatsSource source = (IStatsSource) sourceClassInst.newInstance();
     Class<?> sourceProcClassInst = Class.forName(sourceProcessorClassStr);
     IStatsProcessor sourceProcessor = (IStatsProcessor) sourceProcClassInst.newInstance();
     sourceProcessor.process(source);
     List<FixtureDTO> fixtures = sourceProcessor.getFixtures();
     Collections.sort(fixtures, new FootballAPIComparator());
     for (FixtureDTO fixtureDTO : fixtures) {
       Fixture match = fixtureRepo.findByMatchId(fixtureDTO.getMatchId());
       if (match != null) {
         log.debug("Found same match. Updating the match id : [ " + match.getId() + " ]");
         fixtureDTO.setId(match.getId());
       } else {
         log.debug("Found new fixture. Creating a new match [ " + fixtureDTO + " ]");
       }
       fixtureRepo.save(fixtureMapper.fixtureDTOToFixture(fixtureDTO));
     }
   } catch (Exception e) {
     log.error("Error.. ", e);
   }
 }
 public T instance() {
   T singletonInstancePerThread = null;
   // use weak reference to prevent cyclic reference during GC
   WeakReference<T> ref = (WeakReference<T>) perThreadCache.get();
   // singletonInstancePerThread=perThreadCache.get();
   // if (singletonInstancePerThread==null) {
   if (ref == null || ref.get() == null) {
     Class<T> clazz = null;
     try {
       clazz =
           (Class<T>) Thread.currentThread().getContextClassLoader().loadClass(singletonClassName);
       singletonInstancePerThread = clazz.newInstance();
     } catch (Exception ignore) {
       try {
         clazz = (Class<T>) Class.forName(singletonClassName);
         singletonInstancePerThread = clazz.newInstance();
       } catch (Exception ignore2) {
       }
     }
     perThreadCache.set(new WeakReference<T>(singletonInstancePerThread));
   } else {
     singletonInstancePerThread = ref.get();
   }
   return singletonInstancePerThread;
 }