FileDocCategorySizeDatePackage
ObjectAnalyzer.javaAPI DocGlassfish v2 API13540Fri May 04 22:32:14 BST 2007com.sun.enterprise.util.diagnostics

ObjectAnalyzer

public class ObjectAnalyzer extends Object
WBN -- generic mechanism for simulating toString() and equals() for any class

Fields Summary
private static final String
fatal
private static boolean
useShortClassNames_
private static Method
setAccessibleMethod
Constructors Summary
Methods Summary
public static booleanequals(java.lang.Object a, java.lang.Object b)

param
a
param
b
return

  
		Class cl = a.getClass();

		if (!cl.equals(b.getClass())) 
			return false;
		
		Field[] fields = cl.getDeclaredFields();
		setAccessible(fields);
		
		for (int i = 0; i < fields.length; i++)
		{  
			Field f = fields[i];
			try
			{  
				if (!f.get(a).equals(f.get(b)))
					return false;
			}
			catch(IllegalAccessException e)
			{  
				return false;
			}
		}
		
		return true;
	
private static com.sun.enterprise.util.diagnostics.FieldInfoVectorgetFieldInfo(java.lang.Object obj, boolean doSuperClasses)

		FieldInfoVector fiv = new FieldInfoVector();
		Class			cl	= safeGetClass(obj);

		if(doSuperClasses == false)
		{
			getFieldInfo(cl, obj, fiv);
			return fiv;
		}

		for(Class theClass = cl; theClass != null && !theClass.equals(Object.class); theClass = safeGetSuperclass(theClass))
			getFieldInfo(theClass, obj, fiv);

		return fiv;
	
private static voidgetFieldInfo(java.lang.Class cl, java.lang.Object obj, com.sun.enterprise.util.diagnostics.FieldInfoVector fiv)

		Field[] fields = null;

		try
		{
			fields = cl.getDeclaredFields();
		}
		catch(SecurityException e) 
		{
			throw new ObjectAnalyzerException("got a SecurityException when calling getDeclaredFields() on " + cl.getName());//NOI18N
		}

		if(fields == null)
			throw new ObjectAnalyzerException("calling getDeclaredFields() on " + cl.getName() + " returned null");//NOI18N
		
		setAccessible(fields);

		for(int i = 0; i < fields.length; i++)
		{
			Field f = fields[i];
			String sval;
			String modifiers = Modifier.toString(f.getModifiers());
			String className = cl.getName();

			if(useShortClassNames_)
				className = StringUtils.toShortClassName(className);

			if(modifiers.length() <= 0)
				modifiers = "(package)";//NOI18N

			try
			{  
				Object val = f.get(obj);
				
				if(val == null)
					sval = "<null>";//NOI18N
				else
					sval = val.toString();
			} 
			catch (IllegalAccessException e)
			{  
				sval = "<IllegalAccessException>";//NOI18N
			}
			
			fiv.addElement(new FieldInfo(className, f, sval, modifiers));
		}
	
public static java.lang.StringgetMethods(java.lang.String className)

param
className
return

		try
		{
			Class clazz = Class.forName(className);
			return getMethods(clazz, false);
		}
		catch(Exception e)
		{
			return "Error loading class: " + e.getMessage();//NOI18N
		}
	
public static java.lang.StringgetMethods(java.lang.Object obj)

param
obj
return

		return getMethods(obj, false);
	
public static java.lang.StringgetMethods(java.lang.Object obj, boolean settersOnly)

param
obj
param
settersOnly
return

		try
		{
			Class clazz = safeGetClass(obj);
			return getMethods(clazz, settersOnly);
		}
		catch(Exception e)
		{
			return e.getMessage();
		}
	
public static java.lang.StringgetMethods(java.lang.Class clazz, boolean settersOnly)

param
clazz
param
settersOnly
return

		String s = new String();

		Method[] methods = clazz.getMethods();

		for(int i = 0; i < methods.length; i++)
		{
			Method m = methods[i];
			boolean isSetter = m.getName().startsWith("set");//NOI18N

			if(settersOnly && isSetter == false)
				continue;
			
			s = s + m.toString() + '\n";
		}
		return s;
	
public static java.lang.StringgetSetters(java.lang.Object obj)

param
obj
return

		return getMethods(obj, true);
	
public static java.lang.StringgetSetters(java.lang.Class clazz)

param
clazz
return

		return getMethods(clazz, true);
	
public static voidmain(java.lang.String[] args)

param
args


	
	
		setupSetAccessibleMethod();
	
		String s = new String("Hello!");//NOI18N

		System.out.println("Regular: \n" + toString(s) + "\n\n");//NOI18N
		System.out.println("Super: \n" + toStringWithSuper(s) + "\n\n");//NOI18N
	
private static java.lang.ClasssafeGetClass(java.lang.Object obj)

		if(obj == null)
			throw new ObjectAnalyzerException(fatal + "null Object parameter");//NOI18N

		if(! (obj instanceof Object))
			throw new ObjectAnalyzerException(fatal + "Object parameter is not really a java.lang.Object");//NOI18N

		Class cl = obj.getClass();

		if(cl == null)
			throw new ObjectAnalyzerException(fatal + "getClass() on parameter Object returned null");//NOI18N
		
		return cl;
	
private static java.lang.ClasssafeGetSuperclass(java.lang.Class cl)

		Class sc = cl.getSuperclass();

		if(sc == null)
			throw new ObjectAnalyzerException("getSuperclass() on parameter Object returned null");//NOI18N
		
		return sc;
	
private static voidsetAccessible(java.lang.reflect.Field[] fields)

		if(setAccessibleMethod == null)
			return;	// Must be pre JDK 1.2.x

		try
		{  
			Boolean b = Boolean.valueOf(true);
			setAccessibleMethod.invoke(null, new Object[] { fields, b } );
		}
		catch(Exception e) 
		{
			Reporter.warn("Got an exception invoking setAccessible: " + e);//NOI18N
		}
	
private static voidsetupSetAccessibleMethod()

		// whoa!  what's this reflection crap doing here?
		// AccessibleObject is a JDK 1.2 class that lets you peek at
		// private variable values.  Since we need to support JDK 1.1 
		// for the VCafe plug-in -- it is now called via 100% reflection
		// techniques...

		setAccessibleMethod = null;

		Class AO;

		try
		{
			AO = Class.forName("java.lang.reflect.AccessibleObject");//NOI18N
		}
		catch(ClassNotFoundException e)
		{
			Reporter.info("Can't find java.lang.reflect.AccessibleObject -- thus I can't show any private or protected variable values.  This must be pre JDK 1.2.x");//NOI18N
			return;
		}

		Method[] methods = AO.getDeclaredMethods();

		for(int i = 0; i < methods.length; i++)
		{
			Method m = methods[i];

			if(m.getName().equals("setAccessible") && m.getParameterTypes().length == 2)//NOI18N
			{
				Reporter.verbose("Found setAccessible: " + m);//NOI18N
				setAccessibleMethod = m;
				break;
			}
		}
	
private static java.lang.StringtoString(java.lang.Object obj, boolean doSuperClasses)

		try
		{
			return getFieldInfo(obj, doSuperClasses).toString();
		}
		catch(ObjectAnalyzerException e)
		{
			return e.getMessage();
		}
	
public static java.lang.StringtoString(java.lang.Object obj)

param
obj
return

		return toString(obj, false);
	
public static java.lang.StringtoStringWithSuper(java.lang.Object obj)

param
obj
return

		return toString(obj, true);
	
public static voiduseLongClassNames()

		useShortClassNames_ = false;
	
public static voiduseShortClassNames()

		useShortClassNames_ = true;