提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
检查类i的构造函数是否私有并增加行覆盖率
//package com.nowjava; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; /* from 时 代 J a v a 公 众 号 - nowjava.com */ 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)); } /** * 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/** 时 代 J a v a 公 众 号 - N o w J a v a . c o m 提供 **/ int constructorModifiers = constructor.getModifiers();