Advertisement
rezaridwansyah

Untitled

Apr 10th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. package com.bootcamp;
  2.  
  3. import java.lang.reflect.Constructor;
  4. import java.lang.reflect.Field;
  5. import java.lang.reflect.InvocationTargetException;
  6. import java.lang.reflect.Method;
  7. import java.util.ArrayList;
  8. import java.util.Arrays;
  9. import java.util.List;
  10.  
  11. public class Reflections {
  12.  
  13. public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
  14. Field[] bankEntityField = BankEntity.class.getDeclaredFields();
  15. List<Field> bankEntityFieldList = Arrays.asList(bankEntityField);
  16.  
  17. for(Field field : bankEntityField){
  18. System.out.println(field.getName());
  19. }
  20.  
  21. for(int i=0;i<bankEntityField.length;i++){
  22. System.out.println(bankEntityField[i].getName());
  23. }
  24.  
  25. bankEntityFieldList.forEach(field->{
  26. System.out.println(field.getName());
  27. });
  28.  
  29. Integer ip= new Integer(4);
  30. int ipk=4;
  31.  
  32. System.out.println(ip);
  33.  
  34. //Class accountClass = Account.class;
  35. String accountClassName = "com.bootcamp.Account";
  36. Class accountClass = Class.forName(accountClassName);
  37. /*account class adalah class dengan name com.bootcamp.Account*/
  38.  
  39. for(Field accFiled : accountClass.getDeclaredFields()){
  40. System.out.println(accFiled.getName());
  41. }
  42.  
  43. for(int i=0;i<accountClass.getDeclaredFields().length;i++){
  44. accountClass.getDeclaredFields()[i].getName();
  45. }
  46.  
  47. Constructor accountConstructor = accountClass.getConstructor();
  48. Constructor accountDefConsturctor = accountClass.getConstructor(int.class);
  49. Object accountObj = accountConstructor.newInstance();
  50. Object accountObjDef = accountDefConsturctor.newInstance(95);
  51.  
  52. Account account = ((Account) accountObj);
  53. Account accountDef = ((Account) accountObjDef);
  54.  
  55. System.out.println(account);
  56. System.out.println(accountDef);
  57. account.setUserName("afan");
  58. account.setPassword("1234");
  59.  
  60.  
  61. System.out.println(account);
  62. Field licensed = accountClass.getDeclaredField("licensed");
  63. //
  64. licensed.setAccessible(true);
  65. licensed.set(account,true);
  66. System.out.println(account);
  67. System.out.println(account);
  68.  
  69. Method[] accountMethods = accountClass.getDeclaredMethods();
  70.  
  71. for(Method accMethod : accountMethods){
  72. System.out.println("Method "+accMethod.getName());
  73. }
  74.  
  75. Method tranferMethod = accountClass.getDeclaredMethod("transferMoneyToMyRec");
  76. tranferMethod.invoke(account);
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92. //System.out.println(accountClass.getName());
  93. //Class.forName("");
  94.  
  95.  
  96.  
  97.  
  98.  
  99. }
  100. }
  101.  
  102.  
  103. package com.bootcamp;
  104.  
  105. import java.util.Random;
  106.  
  107. public class Account extends BankEntity {
  108. private Integer id;
  109. private String userName;
  110. private String password;
  111. private boolean licensed;
  112. public String nonPrivateProperty;
  113.  
  114. public Account(int id) {
  115. this.id = id;
  116. }
  117.  
  118. public Account() {
  119. this.id = 24;
  120. }
  121.  
  122. public String getUserName() {
  123. return userName;
  124. }
  125.  
  126. public void setUserName(String userName) {
  127. this.userName = userName;
  128. }
  129.  
  130. public String getPassword() {
  131. return password;
  132. }
  133.  
  134. public void setPassword(String password) {
  135. this.password = password;
  136. }
  137.  
  138. public Integer getId() {
  139. return id;
  140. }
  141.  
  142. public void setId(Integer id) {
  143. this.id = id;
  144. }
  145.  
  146. public void getRandomeNumber(){
  147. System.out.println(new Random().nextInt());
  148. }
  149.  
  150. public void powerNumber(Integer intVal){
  151. System.out.println(intVal*intVal);
  152. }
  153.  
  154. public void transferMoneyToMyRec(){
  155. System.out.println("Uang 1 milyar telah di transfer ke rekening anda");
  156. }
  157.  
  158. public static void sayHello(){
  159. System.out.println("hello");
  160. }
  161.  
  162. @Override
  163. public String toString() {
  164. return "Account{" +
  165. "id=" + id +
  166. ", userName='" + userName + '\'' +
  167. ", password='" + password + '\'' +
  168. ", licensed=" + licensed +
  169. '}';
  170. }
  171. }
  172.  
  173.  
  174. package com.bootcamp;
  175.  
  176. public class BankEntity {
  177. private String uid;
  178. private String cardNumber;
  179.  
  180. public String getUid() {
  181. return uid;
  182. }
  183.  
  184. public void setUid(String uid) {
  185. this.uid = uid;
  186. }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement