BAEL-4399: corrected the examples as per review
This commit is contained in:
parent
71daa1a7c1
commit
dcc5d77c1b
|
@ -1,6 +1,6 @@
|
|||
package main.java.oldclass;
|
||||
package com.baeldung.exceptions.nosuchfielderror;
|
||||
|
||||
public class Dependent {
|
||||
//This needed to be commented post compilation of NoSuchFielDError and Compile
|
||||
// This needed to be commented post compilation of NoSuchFielDError and Compile
|
||||
public static String message = "Hello Baeldung!!";
|
||||
}
|
||||
|
|
|
@ -1,7 +1,21 @@
|
|||
package main.java.oldclass;
|
||||
package com.baeldung.exceptions.nosuchfielderror;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
public class NoSuchFieldError {
|
||||
public static void main(String[] args) {
|
||||
System.out.println(Dependent.message);
|
||||
// String message = "Hello Baeldung!!!";
|
||||
|
||||
public static String getMessage() throws ClassNotFoundException, NoSuchFieldException, SecurityException,
|
||||
IllegalArgumentException, IllegalAccessException {
|
||||
NoSuchFieldError clss = new NoSuchFieldError();
|
||||
Class<?> aClass = Class.forName(clss.getClass().getName());
|
||||
Field field = aClass.getDeclaredField("message");
|
||||
field.setAccessible(true);
|
||||
String msgStr = (String) field.get(clss);
|
||||
return msgStr;
|
||||
}
|
||||
|
||||
public static String getDependentMessage() {
|
||||
return Dependent.message;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
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);
|
||||
}
|
||||
}
|
|
@ -1,13 +1,24 @@
|
|||
package com.baeldung.exceptions.nosuchmethoderror;
|
||||
package com.baeldung.exceptions.nosuchfielderror;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.Test;
|
||||
|
||||
public class NoSuchFieldErrorTest {
|
||||
|
||||
@Test(expected = NoSuchFieldException.class)
|
||||
public void whenFieldNotFound_thenThrowNoSuchFieldException() {
|
||||
NoSuchFieldError2.print();
|
||||
}
|
||||
@Test(expected = NoSuchFieldException.class)
|
||||
public void whenFieldNotFound_thenThrowNoSuchFieldException() throws Exception {
|
||||
NoSuchFieldError.getMessage();
|
||||
}
|
||||
|
||||
@Test(expected = NoSuchFieldException.class)
|
||||
public void whenDependentFieldNotFound_thenThrowNoSuchFieldException() {
|
||||
NoSuchFieldError.getDependentMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDependentFieldNotFound_returnMessage() {
|
||||
String dependentMessage = NoSuchFieldError.getDependentMessage();
|
||||
assertTrue("Hello Baeldung".equals(dependentMessage));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,5 +16,17 @@
|
|||
},
|
||||
"devDependencies": {
|
||||
"eslint-plugin-react": "^7.9.1"
|
||||
},
|
||||
"browserslist": {
|
||||
"production": [
|
||||
">0.2%",
|
||||
"not dead",
|
||||
"not op_mini all"
|
||||
],
|
||||
"development": [
|
||||
"last 1 chrome version",
|
||||
"last 1 firefox version",
|
||||
"last 1 safari version"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue