有没有一种方式可以创建某种对象分解路由,并搜索每个部分以查找我期望的值,因为它被隐藏在对象的某个地方,我无法在Eclipse中找到它;它太嵌套了。
我考虑使用反射来遍历对象类的所有字段,并在每个字段内部搜索该值(列表内的字段(列表的列表等))。还有其他想法吗?
我猜您只想找到特定的值并跟踪其来源。而且,您希望在调试时完成这一切。我建议两个选项。
选项1 使用JSON - 将对象序列化为json字符串并在结果上进行手动文本搜索。您需要json.jar(或任何其他解析器)。
try {
System.out.println(new JSONObject(new YourHugeObject()).toString(5));
} catch (JSONException e) {
log(e);
}
这将会产生类似于这样的结果。 (我通过创建一个带有一些嵌套字段、列表和映射的对象来模拟这个过程)
{
"ct": {
"a": 1,
"b": "sdf",
"f": 12,
"nested": {
"key1": {
"kk": "kk",
"ssdf": 123
},
"onemorekey": {
"kk": "kk",
"ssdf": 123
}
}
},
"doubleProp": 12.2,
"lngprop": 1232323,
"strProp": "123",
"stringlist": [
"String1",
"String2",
"String3"
]
}
选项2 将对象转换/序列化为XML。使用XStream,这将是所有可用解析器中最简单的。只需两行代码即可实现。
XStream stream = new XStream();
System.out.println(stream.toXML(new YourHugeObject()));
这将会产生以下结果:
<com.kmg.jsontools.test.JSTest>
<stringlist>
<string>String1</string>
<string>String2</string>
<string>String3</string>
</stringlist>
<strProp>123</strProp>
<doubleProp>12.2</doubleProp>
<lngprop>1232323</lngprop>
<ct>
<a>1</a>
<b>sdf</b>
<f>12.0</f>
<nested>
<entry>
<string>key1</string>
<com.kmg.jsontools.test.Type1>
<kk>kk</kk>
<ssdf>123</ssdf>
</com.kmg.jsontools.test.Type1>
</entry>
<entry>
<string>onemorekey</string>
<com.kmg.jsontools.test.Type1>
<kk>kk</kk>
<ssdf>123</ssdf>
</com.kmg.jsontools.test.Type1>
</entry>
</nested>
</ct>
</com.kmg.jsontools.test.JSTest>
以上两种方法中,你可以将结果输出到控制台或文件中,然后手动检查。另外,你也可以使用反射(reflection)的方式,不过这种方法需要编写大量代码,并且在测试时也需要投入相当多的时间。
org.apache.commons.lang.builder
包,如何?System.out.println ( ToStringBuilder.reflectionToString( YOUR_OBJECT ) );
Map<String, Object> DebugHelper.breakDown(Object bean)
将其实现为数据挖掘,例如使用org.apache.pivot.beans.BeanAdapter可以帮助您将bean视为普通映射,或者(如您所写-您需要深入挖掘)使用递归将所有属性分组为单个大映射。
另一个有用的签名:
Map<String, Object> DebugHelper.breakDown(Object bean, String lookFor)
第二个参数用于在第一个方法的Map中查找值的子字符串。
为什么这很有用?因为您可以使用Eclipse的检查器在调试的任何时候计算此方法的结果。
我认为你需要使用反射来获取数据。这里是一些代码,应该可以帮助你入门:
static String search(Object o, final String search) {
try {
inspect(new HashSet<Object>(), o, new StringWriter() {
public void write(String s) {
System.out.println(s);
if (s.indexOf(search) >= 0) {
throw new RuntimeException(s) {
public Throwable fillInStackTrace() { return null; } };
}
}
});
return "not found";
} catch (Exception e) {
return e.getMessage();
}
}
private static void inspect(HashSet<Object> ignore, Object o, Writer w)
throws Exception {
if (o == null) {
return;
}
for (Class<?> c = o.getClass(); c != null ; c = c.getSuperclass()) {
if (c.isArray()) {
int len = Array.getLength(o);
for (int i=0; i<len; i++) {
inspect(ignore, Array.get(o, i), w);
}
} else if (c.isPrimitive()) {
w.write(o.toString());
} else {
for (Field f : c.getDeclaredFields()) {
if (!Modifier.isStatic(f.getModifiers())) {
f.setAccessible(true);
if (f.getType().isPrimitive()) {
w.write(f.getName() + ": " + f.get(o));
} else {
if (!ignore.contains(o)) {
ignore.add(o);
w.write(f.getName());
inspect(ignore, f.get(o), w);
}
}
}
}
}
}
}
public static void debugPrint(Object o1)
{
if(o1 == null)
{
System.out.println(o1);
return;
}
Class c = o1.getClass();
do
{
printFields(c, o1);
c = c.getSuperclass();
}
while(c != null);
}
private static void printFields(Class c, Object o1)
{
Field[] fields = c.getDeclaredFields();
for(Field field : fields)
{
printField(field, o1);
}
}
private static void printField(Field field, Object o1)
{
try
{
if(Modifier.isFinal(field.getModifiers())) return; //Skip this guy, he's trouble!
field.setAccessible(true);
Object val = field.get(o1);
System.out.println(field.getName() + ":" + toString(val));
}
catch(IllegalAccessException ex)
{
System.out.println("Could not access field" + field.getName());
ex.printStackTrace();
}
}
private static String toString(Object o)
{
if(o instanceof Object[])
return Arrays.toString((Object[])o);
else
return String.valueOf(o);
}
免责声明:这段代码是暴力且非常丑陋的。我只想用这些基本思路演示如何实现 OP 想要的功能:
Field#setAccessible
Iterable
、Enumeration
、原始包装类和 String
的特殊情况@Thomas Mueller's answer 包含了其中一些元素,但我的 #3 和 #4 更加健壮。而他的代码似乎无法处理我扭曲的测试类。然而我可能仍然错过了一些边角案例。请不要把这个发给我的 Java 导师。
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.*;
public class BreakDown
{
public static void main(String[] args)
{
printFields(new TestClass(), "");
}
private static void printFields(final Object obj, final String prefix)
{
if( basics.contains(obj.getClass()) )
{
System.out.println(prefix + obj);
return;
}
for( final Field f : obj.getClass().getDeclaredFields() )
{
try
{
try
{
printOneField(f, obj, prefix);
continue;
}
catch( SecurityException e ) {}
catch( IllegalAccessException e ) {}
AccessController.doPrivileged(new PrivilegedAction<Void>()
{
public Void run()
{
try
{
printOneField(f, obj, prefix);
}
catch( Exception e )
{
e.printStackTrace();
}
return null;
}
});
}
catch( Exception e )
{
e.printStackTrace();
}
}
}
private static void printOneField(Field f, Object obj, String prefix) throws Exception
{
f.setAccessible(true);
System.out.println(prefix + " |" + f.getName() + ":" + (obj = f.get(obj)));
if( obj == null )
return;
if( obj.getClass().isArray() )
{
for( int i = 0; i < Array.getLength(obj); ++i )
printObj(Array.get(obj, i), prefix, f.getName());
}
else if( obj instanceof Iterable )
{
Iterator<?> it = ((Iterable<?>)obj).iterator();
for( ; it.hasNext(); )
printObj(it.next(), prefix, f.getName());
}
else if( obj instanceof Enumeration<?> )
{
Enumeration<?> e = (Enumeration<?>)obj;
for( ; e.hasMoreElements(); )
printObj(e.nextElement(), prefix, f.getName());
}
else if( !basics.contains(obj.getClass()) )
printFields(obj, prefix + " |" + f.getName() + ":" + obj.getClass().getCanonicalName());
}
private static void printObj(Object o, String prefix, String name)
{
printFields(o, " " + prefix + " |" + name + ":[(" + o.getClass().getSimpleName() + ")");
}
private static final Set<Class<?>> basics = new HashSet<Class<?>>();
static
{
basics.add(Integer.class);
basics.add(Long.class);
basics.add(Double.class);
basics.add(Float.class);
basics.add(Boolean.class);
basics.add(Character.class);
basics.add(Byte.class);
basics.add(Void.class);
basics.add(Short.class);
basics.add(String.class);
}
}
class TestClass
{
public TestClass()
{
if( ++count_s < 3 )
list.add(new TestClass());
}
private static int count_s = 0;
public final String a = "a";
final TestClass2 obj = new TestClass2();
final TestClass2[] objArray = new TestClass2[]{new TestClass2(), new TestClass2()};
private final int b = count_s;
private final boolean[] c = new boolean[]{true, false};
private final List<TestClass> list = new ArrayList<TestClass>();
}
class TestClass2
{
private static int count_s = 0;
private final float d = 1.5f * (++count_s);
}
输出结果为:
|count_s:3
|a:a
|obj:TestClass2@6e1408
|obj:TestClass2 |count_s:9
|obj:TestClass2 |d:1.5
|objArray:[LTestClass2;@5483cd
|objArray:[(TestClass2) |count_s:9
|objArray:[(TestClass2) |d:3.0
|objArray:[(TestClass2) |count_s:9
|objArray:[(TestClass2) |d:4.5
|b:0
|c:[Z@19ee1ac
|c:[(Boolean)true
|c:[(Boolean)false
|list:[TestClass@1befab0]
|list:[(TestClass) |count_s:3
|list:[(TestClass) |a:a
|list:[(TestClass) |obj:TestClass2@13c5982
|list:[(TestClass) |obj:TestClass2 |count_s:9
|list:[(TestClass) |obj:TestClass2 |d:6.0
|list:[(TestClass) |objArray:[LTestClass2;@1186fab
|list:[(TestClass) |objArray:[(TestClass2) |count_s:9
|list:[(TestClass) |objArray:[(TestClass2) |d:7.5
|list:[(TestClass) |objArray:[(TestClass2) |count_s:9
|list:[(TestClass) |objArray:[(TestClass2) |d:9.0
|list:[(TestClass) |b:1
|list:[(TestClass) |c:[Z@14b7453
|list:[(TestClass) |c:[(Boolean)true
|list:[(TestClass) |c:[(Boolean)false
|list:[(TestClass) |list:[TestClass@c21495]
|list:[(TestClass) |list:[(TestClass) |count_s:3
|list:[(TestClass) |list:[(TestClass) |a:a
|list:[(TestClass) |list:[(TestClass) |obj:TestClass2@1d5550d
|list:[(TestClass) |list:[(TestClass) |obj:TestClass2 |count_s:9
|list:[(TestClass) |list:[(TestClass) |obj:TestClass2 |d:10.5
|list:[(TestClass) |list:[(TestClass) |objArray:[LTestClass2;@c2ea3f
|list:[(TestClass) |list:[(TestClass) |objArray:[(TestClass2) |count_s:9
|list:[(TestClass) |list:[(TestClass) |objArray:[(TestClass2) |d:12.0
|list:[(TestClass) |list:[(TestClass) |objArray:[(TestClass2) |count_s:9
|list:[(TestClass) |list:[(TestClass) |objArray:[(TestClass2) |d:13.5
|list:[(TestClass) |list:[(TestClass) |b:2
|list:[(TestClass) |list:[(TestClass) |c:[Z@15b7986
|list:[(TestClass) |list:[(TestClass) |c:[(Boolean)true
|list:[(TestClass) |list:[(TestClass) |c:[(Boolean)false
|list:[(TestClass) |list:[(TestClass) |list:[]
我认为你需要使用Java反射API。在这里看一下: http://java.sun.com/developer/technicalArticles/ALT/Reflection/
一些提示 -
1 ) 您可以使用 shift 和单击变量 / 方法 / 类,这将正确地将您带到它。
2 ) 右键单击 - 打开调用层次结构将向您显示变量被调用的确切位置。
3 ) Command 和 O 将向您显示类的大纲,以便您可以快速导航
4 ) 快速类型层次结构将向您显示变量在类的总体方案中的位置和方式。
希望这有所帮助