Java examples for Reflection:Constructor
Checks if constructor of class i private and adds line coverage
//package com.java2s; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Modifier; public class Main { public static void main(String[] argv) throws Exception { Class clazz = String.class; System.out.println(isPrivate(clazz)); }/*from w ww .j ava2 s .c o m*/ /** * Checks if constructor of class i private and adds line coverage */ public static boolean isPrivate(Class<?> clazz) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Constructor<?> constructor = clazz .getDeclaredConstructor((Class<?>[]) null); // the modifiers int can tell us metadata about the constructor int constructorModifiers = constructor.getModifiers(); // constructor is private so we first have to make it accessible constructor.setAccessible(true); // now construct an instance constructor.newInstance((Object[]) null); return Modifier.isPrivate(constructorModifiers); } }