Javolution 6.0.0 java
javolution.lang.Initializer Class Reference
Collaboration diagram for javolution.lang.Initializer:
[legend]

Public Member Functions

 Initializer (ClassLoader classLoader)
 
Class<?>[] loadedClasses ()
 
void loadClass (Class<?> cls)
 
boolean initializeLoadedClasses ()
 

Static Public Attributes

static final Configurable< Boolean > SHOW_INITIALIZED
 

Private Attributes

final ClassLoader classLoader
 

Detailed Description

An initializer for all classes loaded by any given class loader.

Initialization of classes at startup (or during bundle activation) ensures a significantly improved worst case execution time especially if the classes has configuration logic to be executed then.

Javolution activator initialize Realtime classes when started. When running outside OSGi the method

can be used to that effect..

Class loading can be performed in a lazy manner and therefore some parts of the class loading process may be done on first use rather than at load time. Javolution bundle activator ensure that all its classes are initialized at start up. The following code illustrates how this can be done for any bundle. [code] public class MyActivator implements BundleActivator { public void start(BundleContext bc) throws Exception { Initializer initializer = new Initializer(MyActivator.class.getClassLoader()); initializer.loadClass(com.foo.internal.UnreferencedClass.class); // Load explicitly classes not directly or indirectly referenced. ... initializer.initializeLoadedClasses(); // Recursive loading/initialization. ... // Continue activation } }[/code]

This utility use reflection to find the classes loaded and may not be supported on all platforms.

Author
Jean-Marie Dautelle
Version
5.1, July 26, 2007

Definition at line 50 of file Initializer.java.

Constructor & Destructor Documentation

◆ Initializer()

javolution.lang.Initializer.Initializer ( ClassLoader  classLoader)

Creates an initializer for the specified class loader.

Definition at line 69 of file Initializer.java.

69  {
70  this.classLoader = classLoader;
71  }

References javolution.lang.Initializer.classLoader.

Member Function Documentation

◆ initializeLoadedClasses()

boolean javolution.lang.Initializer.initializeLoadedClasses ( )

Initializes all the loaded classes. If the initialization leads to more classes being loaded, these classes are initialized as well (recursive process).

Returns
true
if initialization has been performed successfully;
false
otherwise.

Definition at line 119 of file Initializer.java.

119  {
120  boolean isInitializationSuccessful = true;
121  int nbrClassesInitialized = 0;
122  while (true) {
123  Class<?>[] classes = loadedClasses();
124  if (classes == null) {
125  LogContext
126  .debug("Automatic class initialization not supported.");
127  return false;
128  }
129  if (nbrClassesInitialized >= classes.length)
130  break; // Done.
131  for (int i = nbrClassesInitialized; i < classes.length; i++) {
132  Class<?> cls = classes[i];
133  try {
134  if (SHOW_INITIALIZED.get())
135  LogContext.debug("Initialize ", cls.getName());
136  Class.forName(cls.getName(), true, classLoader);
137  } catch (ClassNotFoundException ex) {
138  isInitializationSuccessful = false;
139  LogContext.error(ex); // Should never happen.
140  }
141  }
142  nbrClassesInitialized = classes.length;
143  }
144  LogContext.debug("Initialization of ", nbrClassesInitialized,
145  " classes loaded by ", classLoader);
146  return isInitializationSuccessful;
147  }

References javolution.lang.Initializer.classLoader, javolution.context.LogContext.debug(), javolution.context.LogContext.error(), javolution.lang.Configurable< T >.get(), javolution.lang.Initializer.loadedClasses(), and javolution.lang.Initializer.SHOW_INITIALIZED.

Referenced by javolution.osgi.internal.OSGiServices.initializeRealtimeClasses().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ loadClass()

void javolution.lang.Initializer.loadClass ( Class<?>  cls)

Loads the specified class (does not perform any initialization). This method is typically used to load unreferenced classes.

Definition at line 103 of file Initializer.java.

103  {
104  try {
105  classLoader.loadClass(cls.getName());
106  } catch (ClassNotFoundException e) {
107  LogContext.debug("Class " + cls + " not found.");
108  }
109  }

References javolution.lang.Initializer.classLoader, and javolution.context.LogContext.debug().

Referenced by javolution.osgi.internal.OSGiServices.initializeRealtimeClasses().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ loadedClasses()

Class<?> [] javolution.lang.Initializer.loadedClasses ( )

Returns the classes loaded by the class loader of this initializer or null if not supported by the platform.

Definition at line 78 of file Initializer.java.

78  {
79  Class<?> cls = classLoader.getClass();
80  while (cls != java.lang.ClassLoader.class) {
81  cls = cls.getSuperclass();
82  }
83  try {
84  java.lang.reflect.Field fldClasses = cls
85  .getDeclaredField("classes");
86  fldClasses.setAccessible(true);
87  Vector<Class<?>> list = (Vector<Class<?>>) fldClasses
88  .get(classLoader);
89  Class<?>[] classes = new Class<?>[list.size()];
90  for (int i = 0; i < classes.length; i++) {
91  classes[i] = list.get(i);
92  }
93  return classes;
94  } catch (Throwable e) {
95  return null;
96  }
97  }

References javolution.lang.Initializer.classLoader.

Referenced by javolution.lang.Initializer.initializeLoadedClasses().

Here is the caller graph for this function:

Member Data Documentation

◆ classLoader

final ClassLoader javolution.lang.Initializer.classLoader
private

◆ SHOW_INITIALIZED

final Configurable<Boolean> javolution.lang.Initializer.SHOW_INITIALIZED
static
Initial value:
= new Configurable<Boolean>() {
@Override
protected Boolean getDefault() {
return false;
}
}

Indicates if the class being initialized should be logged (default

false

).

Definition at line 56 of file Initializer.java.

Referenced by javolution.lang.Initializer.initializeLoadedClasses().


The documentation for this class was generated from the following file:
javolution.osgi.internal.OSGiServices.initializeRealtimeClasses
static boolean initializeRealtimeClasses()
Definition: OSGiServices.java:124
javolution
javolution.lang.Initializer.SHOW_INITIALIZED
static final Configurable< Boolean > SHOW_INITIALIZED
Definition: Initializer.java:56
javolution.lang.Initializer.classLoader
final ClassLoader classLoader
Definition: Initializer.java:64
javolution.lang.Initializer.loadedClasses
Class<?>[] loadedClasses()
Definition: Initializer.java:78
javolution.osgi.internal
Definition: ConfigurableListenerImpl.java:9
javolution.osgi.internal.OSGiServices
Definition: OSGiServices.java:50
javolution.osgi
javolution.lang.Configurable.get
T get()
Definition: Configurable.java:170