Java Class.getClassLoader Examples

Java Class.getClassLoader - 30 examples found. These are the top rated real world Java examples of Class.getClassLoader extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
 private Connection openConnection(Class<? extends Driver> driverClass, Properties properties)
     throws Exception {
   assert properties != null;
   try {
     return DriverManager.getConnection(url, properties);
   } catch (Exception e) {
     // if the current class loader can not access the driver class, create driver class directly
     try {
       Driver driverObject = driverClass.getConstructor().newInstance();
       Connection connection = driverObject.connect(url, properties);
       if (connection == null) {
         throw new IllegalStateException(
             MessageFormat.format(
                 "Driver class {0} may not support {1}", driverClass.getName(), url));
       }
       return connection;
     } catch (RuntimeException inner) {
       LOG.debug(
           MessageFormat.format(
               "Failed to resolve driver class (internal error): {0} (on {1})",
               driverClass.getName(), driverClass.getClassLoader()),
           e);
     } catch (Exception inner) {
       LOG.debug(
           MessageFormat.format(
               "Failed to resolve driver class: {0} (on {1})",
               driverClass.getName(), driverClass.getClassLoader()),
           e);
     }
     throw e;
   }
 }
  @StubMethod
  public static ClassMirror getCallerClassMirror(int depth) {
    // Filter out any non-holographic frames
    int nativeDepth = 1;
    Class<?> klass = sun.reflect.Reflection.getCallerClass(nativeDepth);
    while (klass != null) {
      ClassLoader loader = klass.getClassLoader();
      if (loader instanceof HologramClassLoader) {
        if (depth == 0) {
          HologramClassLoader hologramClassLoader = (HologramClassLoader) klass.getClassLoader();
          String className = HologramClassGenerator.getOriginalBinaryClassName(klass.getName());
          return hologramClassLoader.loadOriginalClassMirror(className);
        }
        depth--;
      }
      nativeDepth++;
      klass = sun.reflect.Reflection.getCallerClass(nativeDepth);
    }

    // Off the top of the holographic stack, so refer to the original stack.
    ThreadMirror currentThread = ThreadHolograph.currentThreadMirror();
    List<FrameMirror> stack = currentThread.getStackTrace();
    int frameIndex = stack.size() - 1 - depth;
    if (frameIndex < 0) {
      return null;
    }
    FrameMirror frame = stack.get(frameIndex);
    return frame.declaringClass();
  }
  protected void purgeEntries(Field properties)
      throws IllegalArgumentException, IllegalAccessException {
    if (properties == null) return;

    if (!properties.isAccessible()) properties.setAccessible(true);

    ConcurrentHashMap map = (ConcurrentHashMap) properties.get(null);
    if (map == null) return;

    Iterator<Class> itor = map.keySet().iterator();
    while (itor.hasNext()) {
      Class clazz = itor.next();
      LOG.info("Clazz: " + clazz + " loaded by " + clazz.getClassLoader());
      if (Thread.currentThread().getContextClassLoader().equals(clazz.getClassLoader())) {
        itor.remove();
        LOG.info("removed");
      } else
        LOG.info(
            "not removed: "
                + "contextclassloader="
                + Thread.currentThread().getContextClassLoader()
                + "clazz's classloader="
                + clazz.getClassLoader());
    }
  }
 /**
  * Instantiate an object given a class name. Check that the <code>className</code> is a subclass
  * of <code>superClass</code>. If that test fails or the object could not be instantiated, then
  * <code>defaultValue</code> is returned.
  *
  * @param className The fully qualified class name of the object to instantiate.
  * @param superClass The class to which the new object should belong.
  * @param defaultValue The object to return in case of non-fulfillment
  */
 public static Object instantiateByClassName(
     String className, Class superClass, Object defaultValue) {
   if (className != null) {
     try {
       Class classObj = Loader.loadClass(className);
       if (!superClass.isAssignableFrom(classObj)) {
         LogLog.error(
             "A \""
                 + className
                 + "\" object is not assignable to a \""
                 + superClass.getName()
                 + "\" variable.");
         LogLog.error("The class \"" + superClass.getName() + "\" was loaded by ");
         LogLog.error("[" + superClass.getClassLoader() + "] whereas object of type ");
         LogLog.error(
             "\"" + classObj.getName() + "\" was loaded by [" + classObj.getClassLoader() + "].");
         return defaultValue;
       }
       return classObj.newInstance();
     } catch (Exception e) {
       LogLog.error("Could not instantiate class [" + className + "].", e);
     }
   }
   return defaultValue;
 }
Example #5
0
  public static void main(String[] args) throws Exception {
    Class clazz = Class.forName("java.lang.String");

    System.out.println(clazz.getClassLoader());

    Class clazz2 = Class.forName("com.shengsiyuan.classloader.C");

    System.out.println(clazz2.getClassLoader());
  }
Example #6
0
 public ModuleManager createModuleManagerProxy(
     PluginVersionReference pluginVersionReference, InvocationHandler invocationHandler)
     throws CantCreateProxyException {
   ModuleManager moduleManager = null;
   if (!openModules.containsKey(pluginVersionReference)) {
     try {
       ModuleManager moduleManagerBase =
           FermatSystem.getInstance().getModuleManager2(pluginVersionReference);
       if (moduleManagerBase == null)
         throw new RuntimeException(
             "Module manager null in platform, please check if your plugin is connected, pluginVersionReference: "
                 + pluginVersionReference.toString3());
       Class clazz = moduleManagerBase.getClass();
       moduleManager =
           (ModuleManager)
               Proxy.newProxyInstance(
                   clazz.getClassLoader(), clazz.getInterfaces(), invocationHandler);
       openModules.put(clazz, moduleManager);
     } catch (CantGetModuleManagerException e) {
       try {
         Class clazz = FermatSystem.getInstance().getModuleManager3(pluginVersionReference);
         if (clazz == null)
           throw new RuntimeException(
               "Module manager null in platform, please check if your plugin is connected, pluginVersionReference: "
                   + pluginVersionReference.toString3());
         moduleManager =
             (ModuleManager)
                 Proxy.newProxyInstance(
                     clazz.getClassLoader(), clazz.getInterfaces(), invocationHandler);
         openModules.put(clazz, moduleManager);
       } catch (Exception e2) {
         Log.e(
             TAG,
             "Cant get module manager in platform, please check if your plugin is connected, pluginVersionReference: "
                 + pluginVersionReference.toString3());
         throw new CantCreateProxyException(
             "Cant get module manager from system", e, "factory", "");
       }
     } catch (ModuleManagerNotFoundException e) {
       Log.e(
           TAG,
           "Cant get module manager in platform, please check if your plugin is connected, pluginVersionReference: "
               + pluginVersionReference.toString3());
       throw new CantCreateProxyException(
           "Cant fount module manager from system", e, "factory", "");
     }
   } else {
     moduleManager = openModules.get(pluginVersionReference);
   }
   //        Log.i(TAG,"interfaces: ");
   //        for (Class<?> aClass : moduleManager.getClass().getInterfaces()) {
   //            Log.i(TAG,aClass.getName());
   //        }
   return moduleManager;
 }