Search in sources :

Example 1 with ReflectionException

use of cn.taketoday.core.reflect.ReflectionException in project today-infrastructure by TAKETODAY.

the class DefineClassHelper method defineClass.

/**
 * Loads a class file by {@code java.lang.invoke.MethodHandles.Lookup}.
 * It is obtained by using {@code neighbor}.
 *
 * @param neighbor a class belonging to the same package that the loaded
 * class belogns to.
 * @param bcode the bytecode.
 */
public static Class<?> defineClass(Class<?> neighbor, byte[] bcode) throws ReflectionException {
    try {
        DefineClassHelper.class.getModule().addReads(neighbor.getModule());
        Lookup prvlookup = MethodHandles.privateLookupIn(neighbor, MethodHandles.lookup());
        return prvlookup.defineClass(bcode);
    } catch (IllegalAccessException | IllegalArgumentException e) {
        throw new ReflectionException(e.getMessage() + ": " + neighbor.getName() + " has no permission to define the class", e);
    }
}
Also used : ReflectionException(cn.taketoday.core.reflect.ReflectionException) Lookup(java.lang.invoke.MethodHandles.Lookup)

Example 2 with ReflectionException

use of cn.taketoday.core.reflect.ReflectionException in project today-framework by TAKETODAY.

the class DefineClassHelper method defineClass.

/**
 * Loads a class file by {@code java.lang.invoke.MethodHandles.Lookup}.
 * It is obtained by using {@code neighbor}.
 *
 * @param neighbor a class belonging to the same package that the loaded
 * class belogns to.
 * @param bcode the bytecode.
 */
public static Class<?> defineClass(Class<?> neighbor, byte[] bcode) throws ReflectionException {
    try {
        DefineClassHelper.class.getModule().addReads(neighbor.getModule());
        Lookup prvlookup = MethodHandles.privateLookupIn(neighbor, MethodHandles.lookup());
        return prvlookup.defineClass(bcode);
    } catch (IllegalAccessException | IllegalArgumentException e) {
        throw new ReflectionException(e.getMessage() + ": " + neighbor.getName() + " has no permission to define the class", e);
    }
}
Also used : ReflectionException(cn.taketoday.core.reflect.ReflectionException) Lookup(java.lang.invoke.MethodHandles.Lookup)

Example 3 with ReflectionException

use of cn.taketoday.core.reflect.ReflectionException in project today-framework by TAKETODAY.

the class ReflectionUtilsTest method testNewPropertyAccessor.

@Test
public void testNewPropertyAccessor() throws NoSuchFieldException {
    final PropertyBean propertyBean = new PropertyBean();
    final Field declaredField = PropertyBean.class.getDeclaredField("static_pro");
    final PropertyAccessor staticProAccessor = PropertyAccessor.fromField(declaredField);
    assertEquals(staticProAccessor.get(null), 0);
    staticProAccessor.set(null, 2);
    assertEquals(staticProAccessor.get(null), 2);
    final Field boolField = PropertyBean.class.getDeclaredField("bool");
    final PropertyAccessor boolAccessor = PropertyAccessor.fromField(boolField);
    assertEquals(boolAccessor.get(propertyBean), false);
    boolAccessor.set(propertyBean, true);
    assertEquals(boolAccessor.get(propertyBean), true);
    final Field finalProField = PropertyBean.class.getDeclaredField("finalPro");
    final PropertyAccessor finalProAccessor = PropertyAccessor.fromField(finalProField);
    assertEquals(finalProAccessor.get(propertyBean), 10L);
    try {
        finalProAccessor.set(null, 101);
    } catch (ReflectionException e) {
        assertEquals(finalProAccessor.get(propertyBean), 10L);
    }
    final Field staticFinalProField = PropertyBean.class.getDeclaredField("staticFinalPro");
    final PropertyAccessor staticFinalProAccessor = PropertyAccessor.fromField(staticFinalProField);
    assertEquals(staticFinalProAccessor.get(propertyBean), (short) 100);
    try {
        staticFinalProAccessor.set(null, 101);
    } catch (ReflectionException e) {
        assertEquals(staticFinalProAccessor.get(propertyBean), (short) 100);
    }
}
Also used : Field(java.lang.reflect.Field) ReflectionException(cn.taketoday.core.reflect.ReflectionException) PropertyAccessor(cn.taketoday.core.reflect.PropertyAccessor) Test(org.junit.jupiter.api.Test)

Example 4 with ReflectionException

use of cn.taketoday.core.reflect.ReflectionException in project today-infrastructure by TAKETODAY.

the class ReflectionUtilsTest method testNewPropertyAccessor.

@Test
public void testNewPropertyAccessor() throws NoSuchFieldException {
    final PropertyBean propertyBean = new PropertyBean();
    final Field declaredField = PropertyBean.class.getDeclaredField("static_pro");
    final PropertyAccessor staticProAccessor = PropertyAccessor.fromField(declaredField);
    assertEquals(staticProAccessor.get(null), 0);
    staticProAccessor.set(null, 2);
    assertEquals(staticProAccessor.get(null), 2);
    final Field boolField = PropertyBean.class.getDeclaredField("bool");
    final PropertyAccessor boolAccessor = PropertyAccessor.fromField(boolField);
    assertEquals(boolAccessor.get(propertyBean), false);
    boolAccessor.set(propertyBean, true);
    assertEquals(boolAccessor.get(propertyBean), true);
    final Field finalProField = PropertyBean.class.getDeclaredField("finalPro");
    final PropertyAccessor finalProAccessor = PropertyAccessor.fromField(finalProField);
    assertEquals(finalProAccessor.get(propertyBean), 10L);
    try {
        finalProAccessor.set(null, 101);
    } catch (ReflectionException e) {
        assertEquals(finalProAccessor.get(propertyBean), 10L);
    }
    final Field staticFinalProField = PropertyBean.class.getDeclaredField("staticFinalPro");
    final PropertyAccessor staticFinalProAccessor = PropertyAccessor.fromField(staticFinalProField);
    assertEquals(staticFinalProAccessor.get(propertyBean), (short) 100);
    try {
        staticFinalProAccessor.set(null, 101);
    } catch (ReflectionException e) {
        assertEquals(staticFinalProAccessor.get(propertyBean), (short) 100);
    }
}
Also used : Field(java.lang.reflect.Field) ReflectionException(cn.taketoday.core.reflect.ReflectionException) PropertyAccessor(cn.taketoday.core.reflect.PropertyAccessor) Test(org.junit.jupiter.api.Test)

Aggregations

ReflectionException (cn.taketoday.core.reflect.ReflectionException)4 PropertyAccessor (cn.taketoday.core.reflect.PropertyAccessor)2 Lookup (java.lang.invoke.MethodHandles.Lookup)2 Field (java.lang.reflect.Field)2 Test (org.junit.jupiter.api.Test)2