-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a613c6
commit fa809d0
Showing
7 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package main; | ||
|
||
public abstract class AbstractPerson { | ||
protected String name; | ||
protected String phoneNumber; | ||
|
||
protected AbstractPerson() {}; | ||
|
||
protected AbstractPerson(String name, String phoneNumber) { | ||
this.name = name; | ||
this.phoneNumber = phoneNumber; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getPhoneNumber() { | ||
return phoneNumber; | ||
} | ||
|
||
public void setPhoneNumber(String phoneNumber) { | ||
this.phoneNumber = phoneNumber; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package main; | ||
|
||
import java.util.*; | ||
import java.lang.reflect.*; | ||
|
||
public class ArrayFlattening { | ||
|
||
public <T> T concat(Class<?> type, Object... arguments) { | ||
|
||
if (arguments.length == 0) { | ||
return null; | ||
} | ||
|
||
List<Object> list = new ArrayList<>(); | ||
for (int i = 0; i < arguments.length; i++) { | ||
if (arguments[i].getClass().isArray()) { | ||
int len = Array.getLength(arguments[i]); | ||
for (int j = 0; j < len; j++) { | ||
list.add(Array.get(arguments[i], j)); | ||
} | ||
} else { | ||
list.add(arguments[i]); | ||
} | ||
} | ||
|
||
Object flattenedArray = Array.newInstance(type, list.size()); | ||
for (int i = 0; i < list.size(); i++) { | ||
Array.set(flattenedArray, i, list.get(i)); | ||
} | ||
return (T)flattenedArray; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main; | ||
|
||
import java.lang.reflect.Array; | ||
|
||
public class ArrayReflection { | ||
public static void main(String[] args) { | ||
int[] arr = {1, 2, 3, 4, 5}; | ||
if (arr.getClass().isArray()) { | ||
System.out.println("===== is Array ====="); | ||
Class<?> clazz = arr.getClass().getComponentType(); | ||
int len = Array.getLength(arr); | ||
|
||
for (int i = 0; i < len; i++) { | ||
System.out.println(Array.get(arr, i)); | ||
} | ||
} | ||
} | ||
|
||
public Object getArrayEliment(Object array, int index) { | ||
int len = Array.getLength(array); | ||
index = (index < 0) ? len + index : index; | ||
return Array.get(array, index); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package main; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
public class FieldPrint { | ||
public static void main(String[] args) { | ||
Student std = new Student("John", "010-6483-7446", "A"); | ||
|
||
Class<?> clazz = std.getClass(); | ||
Field[] fields = clazz.getDeclaredFields(); | ||
for (var field : fields) { | ||
System.out.println("Field Name : " + field.getName() + ", Field Type : " + field.getType().getName()); | ||
} | ||
|
||
System.out.println("=============="); | ||
|
||
fields = clazz.getFields(); | ||
for (var field : fields) { | ||
System.out.println("Field Name : " + field.getName() + ", Field Type : " + field.getType().getName()); | ||
} | ||
|
||
fields = clazz.getSuperclass().getFields(); | ||
System.out.println("=============="); | ||
|
||
for (var field: fields) { | ||
System.out.println("Field Name : " + field.getName() + ", Field Type : " + field.getType().getName()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package main; | ||
|
||
public class IllegalAccessExceptione { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package main; | ||
|
||
import java.lang.reflect.*; | ||
|
||
public class ObjectSizeCalculator { | ||
private static final long HEADER_SIZE = 12; | ||
private static final long REFERENCE_SIZE = 4; | ||
|
||
public static void main(String[] args) { | ||
sizeOfObject(new Student("John", "010-6483-7446", "A")); | ||
} | ||
|
||
public static long sizeOfObject(Object input) { | ||
long size = 0; | ||
Field[] fields = input.getClass().getDeclaredFields(); | ||
for (var field : fields) { | ||
field.setAccessible(true); | ||
if (!field.isSynthetic()) { | ||
if (field.getType().isPrimitive()) { | ||
size += sizeOfPrimitiveType(field.getType()); | ||
} else { | ||
try { | ||
String value = (String)field.get(input); | ||
size += sizeOfString(value); | ||
} catch (IllegalArgumentException | IllegalAccessException e) { | ||
size += 0; | ||
} | ||
} | ||
} | ||
} | ||
return size + HEADER_SIZE + REFERENCE_SIZE; | ||
} | ||
|
||
|
||
/*************** Helper methods ********************************/ | ||
private static long sizeOfPrimitiveType(Class<?> primitiveType) { | ||
if (primitiveType.equals(int.class)) { | ||
return 4; | ||
} else if (primitiveType.equals(long.class)) { | ||
return 8; | ||
} else if (primitiveType.equals(float.class)) { | ||
return 4; | ||
} else if (primitiveType.equals(double.class)) { | ||
return 8; | ||
} else if (primitiveType.equals(byte.class)) { | ||
return 1; | ||
} else if (primitiveType.equals(short.class)) { | ||
return 2; | ||
} | ||
throw new IllegalArgumentException(String.format("Type: %s is not supported", primitiveType)); | ||
} | ||
|
||
private static long sizeOfString(String inputString) { | ||
int stringBytesSize = inputString.getBytes().length; | ||
return HEADER_SIZE + REFERENCE_SIZE + stringBytesSize; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main; | ||
|
||
public class Student extends AbstractPerson { | ||
private String classCode; | ||
|
||
public Student(String name, String phoneNumber, String classCode) { | ||
super(name, phoneNumber); | ||
this.classCode = classCode; | ||
} | ||
|
||
public String getClassCode() { | ||
return classCode; | ||
} | ||
|
||
public void setClassCode(String classCode) { | ||
this.classCode = classCode; | ||
} | ||
|
||
} |