Reflection: Constructors with Primitive Data Types

Reflection: Constructors with Primitive Data Types

How can I dynamically load a class and call its constructor which has a primitive data type as one of its parameters?

Using reflection to dynamically instantiate a class whose constructor takes a primitive type argument is done almost the same way as for constructors with Object-derived types. I’ve covered how to invoke constructors using reflection before, so I’ll only cover the details of dealing with primitive types.

One difference is that for the parameter type argument of Class.getConstructor(Class[]), you need to use one of the predefined Class objects that represent primitive types. These are Boolean.TYPE, Character.TYPE, Short.TYPE, Integer.TYPE, Long.TYPE, Double.TYPE, Float.TYPE, and Void.TYPE.

Another difference is that when you invoke the constructor, you must use one of the primitive wrapper types (e.g., Integer) to represent the parameter. The wrapper object will be automatically unwrapped by the newInstance() method if necessary. This allows constructors that use one of the wrapper types as an argument (e.g., ConstructMe(Integer)), to also be dynamically invoked through reflection.

The following demonstrates the steps for invoking a constructor that takes an int argument. If you were dynamically loading the class, you would obtain the class reference using Class.forName() rather than by explicitly referencing it as is done in the example.

import java.lang.reflect.*;public final class PrimitiveReflection {  private int __value;  public PrimitiveReflection(int value) {    __value = value;  }  public void print() {    System.out.println(__value);  }  public static final void main(String[] args) {    Constructor constructor;    Method method;    Class[] parameterTypes = new Class[1];    Object[] parameters;     Class clss;    Object obj;    parameterTypes[0] = Integer.TYPE;    clss = PrimitiveReflection.class;    try {      constructor = clss.getConstructor(parameterTypes);    } catch(NoSuchMethodException e) {      e.printStackTrace();      return;    }    // Set up the constructor parameters before creating a new instance    parameters    = new Object[1];    parameters[0] = new Integer(1776);    try {      obj = constructor.newInstance(parameters);    } catch(InstantiationException e) {      e.printStackTrace();      return;    } catch(IllegalAccessException f) {      f.printStackTrace();      return;    } catch(InvocationTargetException g) {      g.printStackTrace();      return;    }    try {      method = obj.getClass().getMethod("print", null);    } catch(NoSuchMethodException e) {      e.printStackTrace();      return;    }    try {      method.invoke(obj, null);    } catch(IllegalAccessException e) {      e.printStackTrace();      return;    } catch(InvocationTargetException f) {      f.printStackTrace();      return;    }  }}

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing