中国山东省济南市建设路
发布网友
发布时间:2022-05-16 18:03
我来回答
共3个回答
热心网友
时间:2023-11-13 09:18
package t3;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public class ObjectSize {
final private static int LENGTH_BOOLEAN=1;
final private static int LENGTH_BYTE=1;
final private static int LENGTH_SHORT=2;
final private static int LENGTH_CHAR=2;
final private static int LENGTH_INT=4;
final private static int LENGTH_FLOAT=4;
final private static int LENGTH_LONG=8;
final private static int LENGTH_DOUBLE=8;
final private static int LENGTH_REFERENCE=4;
final private static int LENGTH_OBJECT=8;
public static int sizeofObject(Object obj)
{ int size=0;
//如果指向为空,则返回引用
if(obj==null) return REFERENCE;
//取得对象的类
Class c=obj.getClass();
//判断参数对象是不是数组类实例化的
if(c.isArray())
{
//引用数组和基本类数组
}
//如果不是静态字段
Field[] fields=obj.getClass().getDeclaredFields();
Field[] fieldsExtends=obj.getClass().getSuperclass().getDeclaredFields();
//合并父子
Field[] newFields=new Field[fields.length+fieldsExtends.length];
System.arraycopy(fields, 0, newFields, 0, fields.length);
System.arraycopy(fieldsExtends, 0, newFields, fields.length, fieldsExtends.length);
//打印测试
for(Field i:fields)
{
i.setAccessible(true);
System.out.println(i);
}
for(Field i:fields)
{ //判断是否为静态,如果不是
if(!Modifier.isStatic(i.getModifiers()))
{
if(i.getType().isPrimitive())
{
size+=sizeofPrimitive(i.getType());
}
else
{
Object objBack=null;
try{
objBack=i.get(obj);
}
catch(IllegalAccessException e){
}
size+=sizeofObject(objBack);
}
}
}
}
/**
* 活得数组的维度
* @param obj
* @return
*/
private static int[] getDimension(Object obj)
{
int dimension=0;
Class c=obj.getClass();
while(c.isArray())
{
dimension++;
c=c.getComponentType();
}
}
public static int sizeofPrimitive(Class c)
{
return;
}
}
-----------------------------------------------------------------------------------------
package treeroot.util;
import Java.lang.reflect.*;
import java.util.*;
/**
* Compute the size of a Runtime object or Primitive type.
* Because the memory allocation is unknown,we just add
* all the fields size of a object.
* the primitive size is thounght as
* boolean:1
* byte:1
* short:2
* char:2
* int:4
* float:4
* long:8
* double:8
* the array size are the sum of all the elements
* the null reference is thought to 4 byte.
* the Object instance has the size 8.
*
*/
public class Size
{
final private static int BOOLEAN=1;
final private static int BYTE=1;
final private static int SHORT=2;
final private static int CHAR=2;
final private static int INT=4;
final private static int FLOAT=4;
final private static int LONG=8;
final private static int DOUBLE=8;
final private static int REFERENCE=4;
final private static int OBJECT=8;
private static ThreadLocal objs=new ThreadLocal();
private static void init(Object o){
Map map=new IdentityHashMap();
map.put(o,null);
objs.set(map);
}
public static int sizeof(boolean i){
return BOOLEAN;
}
public static int sizeof(byte b){
return BYTE;
}
public static int sizeof(short s){
return SHORT;
}
public static int sizeof(char c){
return CHAR;
}
public static int sizeof(int i){
return INT;
}
public static int sizeof(float f){
return FLOAT;
}
public static int sizeof(long l){
return LONG;
}
public static int sizeof(double d){
return DOUBLE;
}
public static int sizeof(Object o){
init(o);
return sizeof0(o);
}
private static int sizeof0(Object o) {
int size=OBJECT;
//if the object is null
if(o==null) return REFERENCE;
Map map=(Map)objs.get();
Class c=o.getClass();
//if it is array
if(c.isArray()){
int[] dimension=getDimension(o);
int len=dimension.length;
Object obj=o;
int num=1;
for(int j=0;j<len-1;j++) num*=dimension[j];
if(dimension[len-1]==0){
size+=num*REFERENCE;
}
else{
num*=dimension[len-1];
//处理递归
int[] index;
Class type=c;
while(type.isArray()) type=type.getComponentType();
//基本类型的数组
if(type.isPrimitive()){
size+=num*sizeofPrimitive(type);
}
//引用类型数组
else{
for(int k=0;k<num;k++){
index=countToIndex(k,dimension);
Object temp=obj;
for(int m=0;m<len;m++){
temp=Array.get(temp,index[m]);
}
//加入数组中的所有对象
if(!map.containsKey(temp)){
size+=sizeof0(temp);
map.put(temp,null);
}
}
}
}
}
// all not-static fields
Field[] fs=getFields(o.getClass());
for(int i=0;i<fs.length;i++){
Field f=fs[i];
if(!Modifier.isStatic(f.getModifiers())){
Class type=f.getType();
//if it is primitive
if(type.isPrimitive()){
size+=sizeofPrimitive(type);
}
//recurtive
else{
Object obj=null;
try{
obj=f.get(o);
}
catch(IllegalAccessException e){
//won't be happen
throw new RuntimeException("impossible");
}
if(!map.containsKey(obj)){
size+=sizeof0(obj);
map.put(obj,null);
}
}
}
}
return size;
}
private static int[] countToIndex(int count,int[] d){
int[] res=new int[d.length];
int c=count;
int i=1;
while(c>0){
int t=1;
for(int j=i;j<d.length;j++) t*=d[j];
if(t>c) i++;
else{
res[i-1]=c/t;
c=c%t;
}
}
return res;
}
private static int sizeofPrimitive(Class c){
if (c==boolean.class){
return BOOLEAN;
}
else if(c==byte.class){
return BYTE;
}
else if(c==char.class){
return CHAR;
}
else if(c==short.class){
return SHORT;
}
else if(c==int.class){
return INT;
}
else if(c==float.class){
return FLOAT;
}
else if(c==long.class){
return LONG;
}
else if(c==double.class){
return DOUBLE;
}
else{
throw new IllegalArgumentException("Thrown by sizeOfPrimitive()");
}
}
private static int[] getDimension(Object obj){
int dimension=0;
Class c=obj.getClass();
while(c.isArray()){
dimension++;
c=c.getComponentType();
}
int[] res=new int[dimension];
Object o=obj;
for(int i=0;i<dimension-1;i++){
res[i]=Array.getLength(o);
o=Array.get(o,0);
}
res[dimension-1]=Array.getLength(o);
return res;
}
private static Field[] getFields(Class c){
Class superClass=c.getSuperclass();
Field[] s=null;
if(superClass!=null){
getFields(superClass);
}
Field[] fs=c.getDeclaredFields();
//设置为可访问的
Field.setAccessible(fs,true);
//合并
int size=0;
if(s!=null) size+=s.length;
if(fs!=null) size+=fs.length;
Field[] result=new Field[size];
int index=0;
if((s!=null)&&(s.length>0)){
System.arraycopy(s,0,result,0,s.length);
index+=s.length;
}
if((fs!=null)&&(fs.length>0)){
System.arraycopy(fs,0,result,index,fs.length);
}
return result;
}
}
热心网友
时间:2023-11-13 09:18
这......什么情况?
热心网友
时间:2023-11-13 09:19
建设路99--1号属于哪个街道