Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;

import java.lang.reflect.Member;

import java.lang.reflect.Modifier;

public class Main {
    public static <T> Constructor<T> findConstructor(Class<T> paramClass, boolean paramBoolean)
            throws IllegalArgumentException {
        Constructor localConstructor;
        try {
            localConstructor = paramClass.getDeclaredConstructor(new Class[0]);
            if (paramBoolean) {
                checkAndFixAccess(localConstructor);
                return localConstructor;
            }
            if (!Modifier.isPublic(localConstructor.getModifiers()))
                throw new IllegalArgumentException("Default constructor for " + paramClass.getName()
                        + " is not accessible (non-public?): not allowed to try modify access via Reflection: can not instantiate type");
        } catch (NoSuchMethodException localNoSuchMethodException) {
            return null;
        } catch (Exception localException) {
            while (true)
                unwrapAndThrowAsIAE(localException, "Failed to find default constructor of class "
                        + paramClass.getName() + ", problem: " + localException.getMessage());
        }
        return localConstructor;
    }

    public static void checkAndFixAccess(Member paramMember) {
        AccessibleObject localAccessibleObject = (AccessibleObject) paramMember;
        try {
            localAccessibleObject.setAccessible(true);
            return;
        } catch (SecurityException localSecurityException) {
            while (localAccessibleObject.isAccessible())
                ;
            Class localClass = paramMember.getDeclaringClass();
            throw new IllegalArgumentException("Can not access " + paramMember + " (from class "
                    + localClass.getName() + "; failed to set access: " + localSecurityException.getMessage());
        }
    }

    public static void unwrapAndThrowAsIAE(Throwable paramThrowable) {
        throwAsIAE(getRootCause(paramThrowable));
    }

    public static void unwrapAndThrowAsIAE(Throwable paramThrowable, String paramString) {
        throwAsIAE(getRootCause(paramThrowable), paramString);
    }

    public static void throwAsIAE(Throwable paramThrowable) {
        throwAsIAE(paramThrowable, paramThrowable.getMessage());
    }

    public static void throwAsIAE(Throwable paramThrowable, String paramString) {
        if ((paramThrowable instanceof RuntimeException))
            throw ((RuntimeException) paramThrowable);
        if ((paramThrowable instanceof Error))
            throw ((Error) paramThrowable);
        throw new IllegalArgumentException(paramString, paramThrowable);
    }

    public static Throwable getRootCause(Throwable paramThrowable) {
        while (paramThrowable.getCause() != null)
            paramThrowable = paramThrowable.getCause();
        return paramThrowable;
    }
}