448 lines
18 KiB
C#
448 lines
18 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using MesTools.ImsApi;
|
|
|
|
namespace MesTools.ImsApi
|
|
{
|
|
public class ObjectEvaluator
|
|
{
|
|
// should a class be printed in one line or every field of a class in a separate line
|
|
|
|
public static bool CLASSES_IN_ONE_LINE;
|
|
private static TypePrinter typePrinter = new TypePrinter();
|
|
private static ArrayPrinter arrayPrinter = new ArrayPrinter(typePrinter);
|
|
private static FieldPrinter fieldNamePrinter = new FieldPrinter();
|
|
|
|
public void evaluateObject(String extFieldName, Object object_, StringBuilder stringBuffer)
|
|
{
|
|
try
|
|
{
|
|
if (object_ == null)
|
|
{
|
|
stringBuffer.Append("null");
|
|
return;
|
|
}
|
|
if (isPrimitiveClass(object_.GetType())) // auch String und StringBuffer direkt
|
|
// anzeigen
|
|
{
|
|
typePrinter.print(typePrinter.getSimpleTypeName(object_.GetType()), stringBuffer);
|
|
fieldNamePrinter.print(extFieldName, stringBuffer);
|
|
if (extFieldName.Equals("password"))
|
|
{
|
|
stringBuffer.Append("*****");
|
|
}
|
|
else
|
|
{
|
|
stringBuffer.Append(object_.ToString());
|
|
}
|
|
return;
|
|
}
|
|
else if (object_.GetType().IsArray)
|
|
{
|
|
evaluateArrayWithoutfield(object_, stringBuffer);
|
|
return;
|
|
}
|
|
|
|
typePrinter.print(object_.GetType().Name, stringBuffer);
|
|
fieldNamePrinter.print(extFieldName, stringBuffer);
|
|
FieldInfo[] fields = object_.GetType().GetFields();
|
|
if (fields.Length > 1)
|
|
{
|
|
stringBuffer.Append('\n');
|
|
}
|
|
// alle Felder auflisten
|
|
for (int i = 0; i < fields.Length; i++)
|
|
{
|
|
// if ((fields[i].getModifiers() & Modifier.PRIVATE) == Modifier.PRIVATE || (fields[i].getModifiers() & Modifier.PROTECTED) == Modifier.PROTECTED)
|
|
// {
|
|
// continue;
|
|
// }
|
|
Type fieldType = fields[i].GetType();
|
|
fieldNamePrinter.printCounter(i, stringBuffer);
|
|
if (isPrimitiveClass(fieldType))
|
|
{
|
|
evalPrimitiveField(fields[i], object_, stringBuffer, "");
|
|
}
|
|
else if (fieldType.Equals(typeof(String)) || fieldType.Equals(typeof(StringBuilder)))
|
|
{
|
|
printString(object_, fields[i], stringBuffer);
|
|
}
|
|
else if (fieldType.IsArray)
|
|
{
|
|
evalArray(fields[i], object_, stringBuffer);
|
|
}
|
|
else
|
|
{
|
|
// komplexe Typen
|
|
Object subObject = fields[i].GetValue(object_);
|
|
evalStructuredField(fields[i], subObject, stringBuffer);
|
|
}
|
|
|
|
if (i < (fields.Length - 1))
|
|
{
|
|
if (CLASSES_IN_ONE_LINE)
|
|
{
|
|
stringBuffer.Append(", ");
|
|
}
|
|
else
|
|
{
|
|
stringBuffer.Append("\n");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception t)
|
|
{
|
|
throw t;
|
|
}
|
|
}
|
|
|
|
private void printString(Object object_, FieldInfo field, StringBuilder stringBuffer)
|
|
{
|
|
// wert ausgeben
|
|
if (!CLASSES_IN_ONE_LINE)
|
|
{
|
|
stringBuffer.Append(" ");
|
|
}
|
|
typePrinter.print("String", stringBuffer);
|
|
fieldNamePrinter.print(field.Name, stringBuffer);
|
|
if (object_ == null)
|
|
{
|
|
stringBuffer.Append("null");
|
|
return;
|
|
}
|
|
|
|
Object subObject = field.GetValue(object_);
|
|
if (subObject == null)
|
|
{
|
|
stringBuffer.Append("null");
|
|
}
|
|
else
|
|
{
|
|
stringBuffer.Append(subObject.ToString());
|
|
}
|
|
}
|
|
|
|
private void evaluateArrayWithoutfield(Object object_, StringBuilder stringBuffer)
|
|
{
|
|
if (object_ == null)
|
|
{
|
|
stringBuffer.Append("null");
|
|
return;
|
|
}
|
|
|
|
if (object_.GetType().Equals(typeof(long[])))
|
|
{
|
|
arrayPrinter.print(null, (long[])object_, stringBuffer);
|
|
}
|
|
else if (object_.GetType().Equals(typeof(int[])))
|
|
{
|
|
arrayPrinter.print(null, (int[])object_, stringBuffer);
|
|
}
|
|
else if (object_.GetType().Equals(typeof(short[])))
|
|
{
|
|
arrayPrinter.print(null, (short[])object_, stringBuffer);
|
|
}
|
|
else if (object_.GetType().Equals(typeof(byte[])))
|
|
{
|
|
arrayPrinter.print(null, (byte[])object_, stringBuffer);
|
|
}
|
|
else if (object_.GetType().Equals(typeof(bool[])))
|
|
{
|
|
arrayPrinter.print(null, (bool[])object_, stringBuffer);
|
|
}
|
|
else if (object_.GetType().Equals(typeof(double[])))
|
|
{
|
|
arrayPrinter.print(null, (double[])object_, stringBuffer);
|
|
}
|
|
else if (object_.GetType().Equals(typeof(float[])))
|
|
{
|
|
arrayPrinter.print(null, (float[])object_, stringBuffer);
|
|
}
|
|
else if (object_.GetType().Equals(typeof(char[])))
|
|
{
|
|
arrayPrinter.print(null, (char[])object_, stringBuffer);
|
|
}
|
|
else if (object_.GetType().Equals(typeof(String[])))
|
|
{
|
|
arrayPrinter.print(null, (String[])object_, stringBuffer);
|
|
}
|
|
else if (object_.GetType().Equals(typeof(StringBuilder[])))
|
|
{
|
|
arrayPrinter.print(null, (StringBuilder[])object_, stringBuffer);
|
|
}
|
|
else if (object_.GetType().Equals(typeof(Int16[])))
|
|
{
|
|
arrayPrinter.print(null, (Int16[])object_, stringBuffer);
|
|
}
|
|
else if (object_.GetType().Equals(typeof(Int16[])))
|
|
{
|
|
arrayPrinter.print(null, (Byte[])object_, stringBuffer);
|
|
}
|
|
else if (object_.GetType().Equals(typeof(Double[])))
|
|
{
|
|
arrayPrinter.print(null, (Double[])object_, stringBuffer);
|
|
}
|
|
else if (object_.GetType().Equals(typeof(Boolean[])))
|
|
{
|
|
arrayPrinter.print(null, (Boolean[])object_, stringBuffer);
|
|
}
|
|
else if (object_.GetType().Equals(typeof(Char[])))
|
|
{
|
|
arrayPrinter.print(null, (Char[])object_, stringBuffer);
|
|
}
|
|
else
|
|
{
|
|
Object[] unknownArray = (Object[])object_;
|
|
if (unknownArray.Length > 0)
|
|
{
|
|
evalArray(unknownArray, null, stringBuffer);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void evalArray(Object[] unknownArray, String varName, StringBuilder stringBuffer)
|
|
{
|
|
// spezielle Type-information für das Array
|
|
// spezielle Type-information für das Array
|
|
if (varName != null)
|
|
{
|
|
fieldNamePrinter.print(varName, stringBuffer);
|
|
}
|
|
if (unknownArray == null)
|
|
{
|
|
stringBuffer.Append("[null]\n");
|
|
return;
|
|
}
|
|
if (unknownArray == null)
|
|
{
|
|
stringBuffer.Append("null");
|
|
return;
|
|
}
|
|
|
|
if (true)
|
|
{
|
|
typePrinter.print(unknownArray.GetType().Name, stringBuffer);
|
|
stringBuffer.Append('[').Append(unknownArray.Length).Append("]\n");
|
|
}
|
|
|
|
String indentation = " ";
|
|
for (int index = 0; index < unknownArray.Length; index++)
|
|
{
|
|
// typePrinter.print(unknownArray.getClass().getSimpleName(), stringBuffer);
|
|
if (index >= ArrayPrinter.MAX_ARRAY_SIZE)
|
|
{
|
|
stringBuffer.Append("output limited to ").Append(ArrayPrinter.MAX_ARRAY_SIZE).Append(" items\n");
|
|
break;
|
|
}
|
|
stringBuffer.Append(indentation).Append('[').Append(index).Append("]");
|
|
if (unknownArray[index] == null)
|
|
{
|
|
stringBuffer.Append(":null");
|
|
}
|
|
else
|
|
{
|
|
// z.B. ein Array of Long[] wird dargestellt als long[]
|
|
if (isPrimitiveClass(unknownArray[index].GetType()))
|
|
{
|
|
evalPrimitiveField(unknownArray[index], stringBuffer, indentation);
|
|
|
|
continue;
|
|
}
|
|
|
|
FieldInfo[] fields = unknownArray[index].GetType().GetFields();
|
|
// alle Felder auflisten
|
|
for (int i = 0; i < fields.Length; i++)
|
|
{
|
|
if (i == 0 && !CLASSES_IN_ONE_LINE)
|
|
{
|
|
stringBuffer.Append("\n");
|
|
}
|
|
|
|
// if ((fields[i].getModifiers() & Modifier.PRIVATE) == Modifier.PRIVATE || (fields[i].getModifiers() & Modifier.PROTECTED) == Modifier.PROTECTED)
|
|
// {
|
|
// continue;
|
|
// }
|
|
fieldNamePrinter.printCounter(i, stringBuffer);
|
|
if (isPrimitiveClass(fields[i].GetType()))
|
|
{
|
|
evalPrimitiveField(fields[i], unknownArray[index], stringBuffer, indentation);
|
|
}
|
|
else if (fields[i].GetType().IsArray)
|
|
{
|
|
evalArray(fields[i], unknownArray[index], stringBuffer);
|
|
}
|
|
else
|
|
{
|
|
stringBuffer.Append(indentation);
|
|
printString(unknownArray[index], fields[i], stringBuffer);
|
|
}
|
|
if (i < fields.Length - 1)
|
|
{
|
|
if (!CLASSES_IN_ONE_LINE)
|
|
{
|
|
stringBuffer.Append("\n");
|
|
}
|
|
else
|
|
{
|
|
stringBuffer.Append(", ");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (index < unknownArray.Length - 1)
|
|
{
|
|
stringBuffer.Append('\n');
|
|
}
|
|
}
|
|
}
|
|
|
|
private void evalPrimitiveField(Object object_, StringBuilder stringbuffer, String indentation)
|
|
{
|
|
|
|
if (!CLASSES_IN_ONE_LINE)
|
|
{
|
|
stringbuffer.Append(indentation).Append(" ");
|
|
}
|
|
if (object_ == null)
|
|
{
|
|
stringbuffer.Append("null");
|
|
return;
|
|
}
|
|
stringbuffer.Append(object_);
|
|
}
|
|
|
|
/**
|
|
* isPrimitive() liefert true fuer int, long, etc. diese Methode liefert true, wenn die Klasse eine Klasse fuer einen
|
|
* primitiven Type ist
|
|
*
|
|
* @param class1
|
|
* @return
|
|
*/
|
|
public bool isPrimitiveClass(Type class1)
|
|
{
|
|
return class1.Namespace.StartsWith("System");
|
|
// (class1 == Integer.class || class1 == Short.class || class1 == Byte.class || class1 == Long.class
|
|
// || class1 == String.class || class1 == StringBuffer.class || class1 == Boolean.class || class1 == Float.class
|
|
// || class1 == Double.class || class1 == Character.class);
|
|
}
|
|
|
|
private void evalPrimitiveField(FieldInfo field, Object object_, StringBuilder stringbuffer, String indentation)
|
|
{
|
|
if (!CLASSES_IN_ONE_LINE)
|
|
{
|
|
stringbuffer.Append(indentation).Append(" ");
|
|
}
|
|
Type fieldType = field.GetType();
|
|
typePrinter.print(field.GetType().Name, stringbuffer);
|
|
fieldNamePrinter.print(field.Name, stringbuffer);
|
|
|
|
if (field.GetValue(object_) == null)
|
|
{
|
|
stringbuffer.Append("null");
|
|
return;
|
|
}
|
|
|
|
/* if (fieldType == Boolean.class || fieldType == boolean.class) {
|
|
stringbuffer.Append(field.getBoolean(object));
|
|
} else if (fieldType == Integer.class || fieldType == int.class) {
|
|
stringbuffer.Append(field.getInt(object));
|
|
} else if (fieldType == Short.class || fieldType == short.class) {
|
|
stringbuffer.Append(field.getShort(object));
|
|
} else if (fieldType == Byte.class || fieldType == byte.class) {
|
|
stringbuffer.Append(field.getByte(object));
|
|
} else if (fieldType == Long.class || fieldType == long.class) {
|
|
stringbuffer.Append(field.getLong(object));
|
|
} else if (fieldType == Character.class || fieldType == char.class) {
|
|
stringbuffer.Append(field.getChar(object));
|
|
} else if (fieldType == Double.class || fieldType == double.class) {
|
|
stringbuffer.Append(field.getDouble(object));
|
|
} else if (fieldType == Float.class || fieldType == float.class) {
|
|
stringbuffer.Append(field.getFloat(object));
|
|
}*/
|
|
}
|
|
|
|
private void evalStructuredField(FieldInfo field, Object object_, StringBuilder stringBuffer)
|
|
{
|
|
String indentation = " ";
|
|
if (object_ == null)
|
|
{
|
|
stringBuffer.Append("null");
|
|
return;
|
|
}
|
|
FieldInfo[] fields = object_.GetType().GetFields();
|
|
// alle Felder auflisten
|
|
for (int i = 0; i < fields.Length; i++)
|
|
{
|
|
|
|
// if ((fields[i].getModifiers() & Modifier.PRIVATE) == Modifier.PRIVATE || (fields[i].getModifiers() & Modifier.PROTECTED) == Modifier.PROTECTED)
|
|
// {
|
|
// fields[i].setAccessible(true);
|
|
// }
|
|
|
|
if (isPrimitiveClass(fields[i].GetType()))
|
|
{
|
|
evalPrimitiveField(fields[i], object_, stringBuffer, indentation);
|
|
}
|
|
else if (fields[i].GetType().IsArray)
|
|
{
|
|
evalArray(fields[i], object_, stringBuffer);
|
|
}
|
|
else if (fields[i].GetType().Equals(typeof(String)) || fields[i].GetType().Equals(
|
|
typeof(StringBuilder)))
|
|
{
|
|
printString(object_, fields[i], stringBuffer);
|
|
}
|
|
// else
|
|
// {
|
|
// evalStructuredField(fields[i], object, stringBuffer);
|
|
// }
|
|
}
|
|
}
|
|
|
|
private void evalArray(FieldInfo field, Object object_, StringBuilder stringBuffer)
|
|
{
|
|
if (object_ == null)
|
|
{
|
|
stringBuffer.Append("null");
|
|
return;
|
|
}
|
|
|
|
String fieldName = field.Name;
|
|
try
|
|
{
|
|
Object simpleObject = field.GetValue(object_);
|
|
Type classOfObject = simpleObject.GetType();
|
|
/* if (classOfObject == long[].class) {
|
|
arrayPrinter.print(fieldName, (long[]) simpleObject, stringBuffer);
|
|
} else if (classOfObject == int[].class) {
|
|
arrayPrinter.print(fieldName, (int[]) simpleObject, stringBuffer);
|
|
} else if (classOfObject == short[].class) {
|
|
arrayPrinter.print(fieldName, (short[]) simpleObject, stringBuffer);
|
|
} else if (classOfObject == byte[].class) {
|
|
arrayPrinter.print(fieldName, (byte[]) simpleObject, stringBuffer);
|
|
} else if (classOfObject == float[].class) {
|
|
arrayPrinter.print(fieldName, (float[]) simpleObject, stringBuffer);
|
|
} else if (classOfObject == double[].class) {
|
|
arrayPrinter.print(fieldName, (double[]) simpleObject, stringBuffer);
|
|
} else if (classOfObject == boolean[].class) {
|
|
arrayPrinter.print(fieldName, (boolean[]) simpleObject, stringBuffer);
|
|
} else if (classOfObject == char[].class) {
|
|
arrayPrinter.print(fieldName, (char[]) simpleObject, stringBuffer);
|
|
} else {
|
|
*/ // ganz allgemein ein Array einer Klasse die von java.lang.Object erbt
|
|
Object[] data = (Object[])simpleObject;
|
|
evalArray(data, fieldName, stringBuffer);
|
|
//}
|
|
}
|
|
catch (Exception )
|
|
{
|
|
stringBuffer.Append("failed to evaluate data for field ").Append(fieldName);
|
|
}
|
|
}
|
|
}
|
|
}
|