16 Temmuz 2020 Perşembe

ClassLoader Sınıfı

Giriş
ClassLoader soyut (abstract) bir sınıf. ClassLoader çalışma esnasında bir sınıf veya arayüz ile karşılaşınca onu yükler. Açıklaması şöyle
ClassLoaders are architected so that at start-up the JVM doesn't need to know anything about the classes that will be loaded at runtime. Initially when a JVM starts up, nothing is loaded into it. The class file of the program being executed is loaded first and then other classes and interfaces are loaded as they get referenced in the bytecode being executed.
- ClassLoader Sınıf Yükleme Sırası yazısına bakabilirsiniz
-  Arayüzü gerçekleştiren sınıflardan birisi URLClassLoader.

ClassLoader 3 tane Önemli İş Yapar
1. Loading 
2. Linking . verification + copy + prepare adımlarından oluşur. 
- verification adımında byte-code doğrulanır. Eğer bir hata varsa java.lang.VerifyError fırlatılır
- copy adımında Class JVM'deki heap alanına kopyalanır
- prepare adımında sınıf içindeki static değişkenlere bellek ayrılır ve varsayılan değerler atanır
3. Initialization 
Bu adımda static değişkenlere koddaki değerler atanır ve varsa static constructor çalıştırılır

Defining Loader Nedir?
Açıklaması şöyle
If L creates C directly, we say that L defines C or, equivalently, that L is the defining loader of C.
Initiating Loader Nedir
Açıklaması şöyle
When one class loader delegates to another class loader, the loader that initiates the loading is not necessarily the same loader that completes the loading and defines the class. If L creates C, either by defining it directly or by delegation, we say that L initiates loading of C or, equivalently, that L is an initiating loader of C.
Classloader'lar şöyle çalışsın.
L->Lp->Lq
L sınıfı yükleme işlemini Lp'ye, Lp ise Lq'ya söylesin. Lq yükleme işlemini bitirince Defining Loader, L ve Lp ise Initiating Loader olurlar.


constructor
Her sınıf kendisini yükleyen Classloader nesnesini bilir. Şöyle yaparız.
ClassLoader classLoader = Test.class.getClassLoader();
Thread ile şöyle yaparız.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
getName metodu
Örnek ver

getResource metodu
ClassLoader İle Resource Yüklemek yazısına taşıdım.

getResources metodu
Tüm jar'ların META-INF dizini dolaşmak için şöyle yaparız.
Enumeration<URL> en=getClass().getClassLoader().getResources("META-INF");
if (en.hasMoreElements()) {
  URL url=en.nextElement();
  ...
}
getResourceAsStream metodu
ClassLoader İle Resource Yüklemek yazısına taşıdım.

getSystemClassLoader metodu
Java 9 ile açıklaması şöyle.
The application class loader is no longer an instance of java.net.URLClassLoader (an implementation detail that was never specified in previous releases). Code that assumes that ClassLoader::getSytemClassLoader returns a URLClassLoader object will need to be updated.
Java 9 ile şu kod çalışmıyor.
URL[] ressources = ((URLClassLoader) classLoader).getURLs();
loadClass metodu
Metodun imzası şöyle
public Class<?> loadClass(String name) throws ClassNotFoundException;
Şöyle yaparız.
package javaReflect.test;

public class PrivateCar {

  private String color;

  protected void drive() {
    System.out.println("this is private car! the color is:" + color);
  }
}

ClassLoader loader = Thread.currentThread().getContextClassLoader();
Class clazz = loader.loadClass("javaReflect.test.PrivateCar");



Hiç yorum yok:

Yorum Gönder