├── LICENSE ├── README.md ├── pom.xml └── src └── main └── java └── dev └── westy ├── Entity.java └── Main.java /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Miraç 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JavaReflection 2 | Java Reflection Mini Help. 3 | 4 | ## Todos: 5 | - Change Fields, Change Final Field 6 | - Print Method Modifiers 7 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | dev.westy 8 | Reflection 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 18 13 | 18 14 | UTF-8 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/main/java/dev/westy/Entity.java: -------------------------------------------------------------------------------- 1 | package dev.westy; 2 | 3 | public class Entity { 4 | private String name; 5 | private int old; 6 | public static int HP = 20; 7 | public static final String SEX = "erko"; 8 | 9 | public Entity(String name, int old) { 10 | this.name = name; 11 | this.old = old; 12 | } 13 | 14 | public String setName(String name) { 15 | this.name = name; 16 | return this.name; 17 | } 18 | 19 | public String getName() { 20 | return name; 21 | } 22 | public int getOld() { 23 | return old; 24 | } 25 | 26 | public int setOld(int old) { 27 | this.old = old; 28 | 29 | return this.old; 30 | } 31 | 32 | public String setProtection(String protectionName, int item) { 33 | return "Protection Changed!"; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/dev/westy/Main.java: -------------------------------------------------------------------------------- 1 | package dev.westy; 2 | 3 | 4 | import java.lang.reflect.Field; 5 | import java.lang.reflect.InvocationTargetException; 6 | import java.lang.reflect.Method; 7 | import java.lang.reflect.Parameter; 8 | 9 | public class Main { 10 | public static String getValueByFieldName(Field f, Object obj) { 11 | String result = ""; 12 | 13 | try { 14 | f.setAccessible(true); 15 | Object fieldValue = f.get(obj); 16 | 17 | if (fieldValue instanceof String) { 18 | result = (String) fieldValue; 19 | } else { 20 | result = String.valueOf(fieldValue); 21 | } 22 | 23 | } catch (IllegalAccessException e) { 24 | throw new RuntimeException(e); 25 | } 26 | 27 | return result; 28 | }; 29 | 30 | public static Object invokeMethod(Method method, Object obj, Object[] parameters) 31 | throws IllegalAccessException, InvocationTargetException { 32 | try { 33 | method.setAccessible(true); 34 | Object result = method.invoke(obj, parameters); 35 | return result; 36 | 37 | } catch (IllegalAccessException | InvocationTargetException e) { 38 | throw e; 39 | } 40 | } 41 | 42 | public static void main(String[] args) { 43 | Entity Mirac = new Entity("Mirac", 16); 44 | 45 | // Get Visible Methods 46 | 47 | try { 48 | System.out.println("Visible Methods: "); 49 | 50 | Class entityClass = (Class) Class.forName("dev.westy.Entity"); 51 | 52 | for(Method m : entityClass.getMethods()) { 53 | String paramsString = "("; 54 | int index = 0; 55 | 56 | for(Parameter param : m.getParameters()) { 57 | index += 1; 58 | if(m.getParameters().length == index) { 59 | paramsString += param.getType().getName() + " " + param.getName(); 60 | } else { 61 | paramsString += param.getType().getName() + " " + param.getName() + ", "; 62 | } 63 | 64 | } 65 | 66 | paramsString += ");"; 67 | 68 | System.out.println(m.getReturnType().getName() + " " + m.getName() + paramsString); 69 | } 70 | 71 | } catch (ClassNotFoundException e) { 72 | throw new RuntimeException(e); 73 | } 74 | 75 | // All Methods 76 | 77 | try { 78 | System.out.println("\nAll Methods: "); 79 | 80 | Class entityClass = (Class) Class.forName("dev.westy.Entity"); 81 | 82 | for(Method m : entityClass.getDeclaredMethods()) { 83 | String paramsString = "("; 84 | int index = 0; 85 | 86 | for(Parameter param : m.getParameters()) { 87 | index += 1; 88 | if(m.getParameters().length == index) { 89 | paramsString += param.getType().getName() + " " + param.getName(); 90 | } else { 91 | paramsString += param.getType().getName() + " " + param.getName() + ", "; 92 | } 93 | 94 | } 95 | 96 | paramsString += ");"; 97 | 98 | System.out.println(m.getReturnType().getName() + " " + m.getName() + paramsString); 99 | } 100 | 101 | } catch (ClassNotFoundException e) { 102 | throw new RuntimeException(e); 103 | } 104 | 105 | 106 | //Visible Fields 107 | 108 | try { 109 | System.out.println("\nVisible Fields: "); 110 | 111 | Class entityClass = (Class) Class.forName("dev.westy.Entity"); 112 | 113 | for(Field f : entityClass.getFields()) { 114 | String result = ""; 115 | 116 | result += f.getType().getName() + " " + f.getName() + ";"; 117 | 118 | System.out.println(result); 119 | } 120 | 121 | } catch (ClassNotFoundException e) { 122 | throw new RuntimeException(e); 123 | } 124 | 125 | //All Fields 126 | 127 | try { 128 | System.out.println("\nAll Fields: "); 129 | 130 | Class entityClass = (Class) Class.forName("dev.westy.Entity"); 131 | 132 | for(Field f : entityClass.getDeclaredFields()) { 133 | String result = ""; 134 | 135 | result += f.getType().getName() + " " + f.getName() + ";"; 136 | 137 | System.out.println(result); 138 | } 139 | 140 | } catch (ClassNotFoundException e) { 141 | throw new RuntimeException(e); 142 | } 143 | 144 | 145 | //Visible Fields Values 146 | 147 | try { 148 | System.out.println("\nVisible Fields Values: "); 149 | 150 | Class entityClass = (Class) Class.forName("dev.westy.Entity"); 151 | 152 | for(Field f : entityClass.getFields()) { 153 | String result = ""; 154 | 155 | result += f.getType().getName() + " " + f.getName() + ";"; 156 | 157 | System.out.println(result + " Value: " + getValueByFieldName(f, Mirac)); 158 | } 159 | 160 | } catch (ClassNotFoundException e) { 161 | throw new RuntimeException(e); 162 | } 163 | 164 | //All Fields Values 165 | 166 | try { 167 | System.out.println("\nAll Fields Values: "); 168 | 169 | Class entityClass = (Class) Class.forName("dev.westy.Entity"); 170 | 171 | for(Field f : entityClass.getDeclaredFields()) { 172 | String result = ""; 173 | 174 | result += f.getType().getName() + " " + f.getName() + ";"; 175 | 176 | System.out.println(result + " Value: " + getValueByFieldName(f, Mirac)); 177 | } 178 | 179 | } catch (ClassNotFoundException e) { 180 | throw new RuntimeException(e); 181 | } 182 | 183 | try { 184 | System.out.println("\nInvoke Methods: "); 185 | 186 | Class entityClass = (Class) Class.forName("dev.westy.Entity"); 187 | 188 | for(Method m : entityClass.getDeclaredMethods()) { 189 | String paramsString = "("; 190 | int index = 0; 191 | 192 | for(Parameter param : m.getParameters()) { 193 | index += 1; 194 | if(m.getParameters().length == index) { 195 | paramsString += param.getType().getName() + " " + param.getName(); 196 | } else { 197 | paramsString += param.getType().getName() + " " + param.getName() + ", "; 198 | } 199 | 200 | } 201 | 202 | paramsString += ");"; 203 | 204 | try { 205 | String invokedValue = ""; 206 | 207 | if(m.getName().equals("getName")) { 208 | invokedValue += invokeMethod(m, Mirac, new Object[]{}); 209 | } else if(m.getName().equals("setName")) { 210 | invokedValue += invokeMethod(m, Mirac, new Object[]{ "Yigit" }); 211 | } else if(m.getName().equals("setProtection")) { 212 | invokedValue += invokeMethod(m, Mirac, new Object[]{ "Diamond", 30 }).toString(); 213 | } else if(m.getName().equals("getOld")) { 214 | invokedValue += invokeMethod(m, Mirac, new Object[]{ }).toString(); 215 | } else if(m.getName().equals("setOld")) { 216 | invokedValue += invokeMethod(m, Mirac, new Object[]{ 20 }).toString(); 217 | } 218 | 219 | System.out.println(m.getReturnType().getName() + " " + m.getName() + paramsString + " Invoked: " + "'" + invokedValue + "'"); 220 | } catch (IllegalAccessException e) { 221 | throw new RuntimeException(e); 222 | } catch (InvocationTargetException e) { 223 | throw new RuntimeException(e); 224 | } 225 | } 226 | 227 | } catch (ClassNotFoundException e) { 228 | throw new RuntimeException(e); 229 | } 230 | } 231 | } --------------------------------------------------------------------------------