Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
solu-jt0907 committed Aug 31, 2022
1 parent 3a613c6 commit fa809d0
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 0 deletions.
29 changes: 29 additions & 0 deletions ch01/src/main/AbstractPerson.java
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;
}
}
32 changes: 32 additions & 0 deletions ch01/src/main/ArrayFlattening.java
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;
}
}
24 changes: 24 additions & 0 deletions ch01/src/main/ArrayReflection.java
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);
}
}
29 changes: 29 additions & 0 deletions ch01/src/main/FieldPrint.java
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());
}
}
}
5 changes: 5 additions & 0 deletions ch01/src/main/IllegalAccessExceptione.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main;

public class IllegalAccessExceptione {

}
57 changes: 57 additions & 0 deletions ch01/src/main/ObjectSizeCalculator.java
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;
}
}
19 changes: 19 additions & 0 deletions ch01/src/main/Student.java
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;
}

}

0 comments on commit fa809d0

Please sign in to comment.