Java Class Loader and Reflection: Understanding Class Metadata and Dynamic Object Creation, Slides of Java Programming

An in-depth exploration of java class loader and reflection, two essential concepts in java programming. Learn about the role of java class loader in creating and managing class objects, and discover how reflection api allows for dynamic inspection and manipulation of classes and objects at runtime. Explanations, examples, and practical usage of important reflection classes such as class, field, and method.

Typology: Slides

2011/2012

Uploaded on 07/07/2012

proo
proo 🇮🇳

4.4

(26)

96 documents

1 / 32

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Programming
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

Partial preview of the text

Download Java Class Loader and Reflection: Understanding Class Metadata and Dynamic Object Creation and more Slides Java Programming in PDF only on Docsity!

Programming

Assignment

  • Read about “Java Class Loader “ and submit one trace of class

loading in java, the submission should include the following

  • Good explanation of Java Class Loader
  • One example code used to observe class loading
  • Trace of class loading for your example code
  • Explanation of the trace.
  • Dead Line : Friday March 16 2012
  • Submission
  • Submission should be in the form of a pdf or word file, containing all elements in one file
  • The name of the file should be PJ_YourName_RollNo
  • Marks 10

What is Reflection?

  • Can we answer these questions if we don’t know what is

reflection?

  • How we can print the class name of all the objects in any java program
  • Given an object , how can we print the list of all the methods and

fields?

  • Given an object , how can we find the super class?
  • User provides us name of a class while the program is running and

wants us to create and object of that class?

  • Can we write any code to achieve all this at compile time?

What is Reflection?

  • Access to internal information for classes loaded into the JVM
    • Allows you to write code that works with classes selected during execution, not in the source code.
  • To obtain the properties/instance of any object at runtime,

sometimes without even knowing anything about the class at

compile time

  • Programs looking at itself
  • Consist of metadata and operations to manipulate the metadata
  • Why use Reflection
  • Compilers (Grade student’s program, check code against compilation rule)
  • Write a GUI IDE for some programming language
  • Dynamic inspection of classes and objects
  • Dynamic creation of objects of unknown classes

Important Reflection Classes

class Class { Constructor[] getConstructors(); Field getDeclaredField(String name); Field[] getDeclaredFields(); Method[] getDeclaredMethods(); ... }


class Field { Class getType(); ... }


class Method { Class[] getParameterTypes(); Class getReturnType(); ... }

Class

  • Metadata
    • Data about data : Information about any object created
  • Java stores object metadata

in a separate class

  • Name : java.lang.reflection.Class
  • For multiple objects of the same class only one Class object is created
  • Accessing metada
  • Using Class one can inspect classes at runtime, commonly inspections include
  • Class Name
  • Class Modifies (public, private, synchronized etc.)
  • Package Info
  • Superclass
  • Implemented Interfaces
  • Constructors
  • Methods
  • Fields

Inspecting Class at runtime

  • Modifiers are grouped as integers, can be checked using

Modifer class

  • Modifier.isAbstract(int modifiers) Modifier.isFinal(int modifiers)
  • Modifier.isInterface(int modifiers) Modifier.isNative(int modifiers)
  • Modifier.isPrivate(int modifiers) Modifier.isProtected(int modifiers)
  • Modifier.isPublic(int modifiers) Modifier.isStatic(int modifiers)
  • Modifier.isStrict(int modifiers) Modifier.isSynchronized(int modifiers)
  • Modifier.isTransient(int modifiers) Modifier.isVolatile(int modifiers)

Inspecting Class at runtime

  • Package Info
    • Package package = aClass.getPackage()
  • Superclass
    • Class superclass = aClass.getSuperclass();
  • Constructors
    • Constructor[] constructors = aClass.getConstructors();
  • Methods
    • Method[] method = aClass.getMethods();
  • Fields
    • Field[] method = aClass.getFields();

Fields

  • Obtaining Field Objects
    • Field field = aClass.getField("someField");
    • Field[] fields = aClass.getFields();
    • Both of these will return public fields of a class
  • Field Name
  • Field Type
  • Getting and Setting Field Values

Reflection Example

class DemoClass { public String demoMethod(String demoParam) { System.out.println("Parameter passed: " + demoParam); return DemoClass.class.getName(); } }

public class DynamicClassLoadingExample {

public static void main(String[] args) { ClassLoader myClassLoader = ClassLoader.getSystemClassLoader();

// Step 2: Define a class to be loaded.

String classNameToBeLoaded = "net.viralpatel.itext.pdf.DemoClass";

// Step 3: Load the class

Class myClass = myClassLoader.loadClass(classNameToBeLoaded);

Abstract Classes and Interfaces

The objectives of this chapter are:

To explore the concept of abstract classes To understand interfaces To understand the importance of both.

What is an Abstract class?

Superclasses are created through the process called "generalization"

Common features (methods or variables) are factored out of object classifications (ie. classes). Those features are formalized in a class. This becomes the superclass The classes from which the common features were taken become subclasses to the newly created super class

Often, the superclass does not have a "meaning" or does not directly relate to a "thing" in the real world

It is an artifact of the generalization process

Because of this, abstract classes cannot be instantiated

They act as place holders for abstraction

What Are Abstract Classes Used For?

Abstract classes are used heavily in Design Patterns

Creational Patterns: Abstract class provides interface for creating objects. The subclasses do the actual object creation Structural Patterns: How objects are structured is handled by an abstract class. What the objects do is handled by the subclasses Behavioural Patterns: Behavioural interface is declared in an abstract superclass. Implementation of the interface is provided by subclasses.

Be careful not to over use abstract classes

Every abstract class increases the complexity of your design Every subclass increases the complexity of your design Ensure that you receive acceptable return in terms of functionality given the added complexity.

Defining Abstract Classes

Inheritance is declared using the "extends" keyword

If inheritance is not defined, the class extends a class called Object

public abstract class Vehicle { private String make; private String model; private int tireCount; [...]

public class Car extends Vehicle { private int trunkCapacity; [...]

Vehicle

  • make: String
  • model: String
  • tireCount: int

Car

  • trunkCapacity: int

Truck

  • bedCapacity: int

public class Truck extends Vehicle { private int bedCapacity; [...]

Often referred to as "concrete" classes