BAEL-4399:Changes for showing NoSuchFieldError
This commit is contained in:
parent
7f19907f51
commit
71daa1a7c1
@ -0,0 +1,6 @@
|
||||
package main.java.oldclass;
|
||||
|
||||
public class Dependent {
|
||||
//This needed to be commented post compilation of NoSuchFielDError and Compile
|
||||
public static String message = "Hello Baeldung!!";
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package main.java.oldclass;
|
||||
|
||||
public class NoSuchFieldError {
|
||||
public static void main(String[] args) {
|
||||
System.out.println(Dependent.message);
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package main.java.reflection;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
public class NoSuchFieldError2 {
|
||||
//String message = "Hello Baeldung!!!";
|
||||
|
||||
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
|
||||
print();
|
||||
}
|
||||
|
||||
public static void print(){
|
||||
ValidateNoSuchFieldError clss = new ValidateNoSuchFieldError();
|
||||
Class<?> aClass = Class.forName(clss.getClass().getName());
|
||||
Field field = aClass.getDeclaredField("message");
|
||||
field.setAccessible(true);
|
||||
String msgStr = (String)field.get(clss);
|
||||
System.out.println(msgStr);
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.baeldung.exceptions.nosuchmethoderror;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class NoSuchFieldErrorTest {
|
||||
|
||||
@Test(expected = NoSuchFieldException.class)
|
||||
public void whenFieldNotFound_thenThrowNoSuchFieldException() {
|
||||
NoSuchFieldError2.print();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user