-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathVariable.java
More file actions
72 lines (63 loc) · 1.71 KB
/
Copy pathVariable.java
File metadata and controls
72 lines (63 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package treeModel;
import java.lang.reflect.*;
import java.util.*;
/**
* A variable with a type, name, and value.
*/
public class Variable
{
private Class<?> type;
private String name;
private Object value;
private ArrayList<Field> fields;
/**
* Construct a variable.
* @param aType the type
* @param aName the name
* @param aValue the value
*/
public Variable(Class<?> aType, String aName, Object aValue)
{
type = aType;
name = aName;
value = aValue;
fields = new ArrayList<>();
// find all fields if we have a class type except we don't expand strings and null values
if (!type.isPrimitive() && !type.isArray() && !type.equals(String.class) && value != null)
{
// get fields from the class and all superclasses
for (Class<?> c = value.getClass(); c != null; c = c.getSuperclass())
{
Field[] fs = c.getDeclaredFields();
AccessibleObject.setAccessible(fs, true);
// get all nonstatic fields
for (Field f : fs)
if ((f.getModifiers() & Modifier.STATIC) == 0) fields.add(f);
}
}
}
/**
* Gets the value of this variable.
* @return the value
*/
public Object getValue()
{
return value;
}
/**
* Gets all nonstatic fields of this variable.
* @return an array list of variables describing the fields
*/
public ArrayList<Field> getFields()
{
return fields;
}
public String toString()
{
String r = type + " " + name;
if (type.isPrimitive()) r += "=" + value;
else if (type.equals(String.class)) r += "=" + value;
else if (value == null) r += "=null";
return r;
}
}