Debian Codegraph @

src/dr/util/Property.java

raw symbols SCIP
1 /*
2 * Property.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.util;
27
28 import java.lang.reflect.Method;
29
30
31 /**
32 * Gets a property of another object using introspection.
33 *
34 * @author Andrew Rambaut
35 * @author Alexei Drummond
36 * @version $Id: Property.java,v 1.19 2005/05/24 20:26:01 rambaut Exp $
37 */
38 public class Property implements Attribute {
39
40 private Object object = null;
41 private Method getter = null;
42 private Object argument = null;
43 private String name = null;
44
45 public Property(Object object, String name) {
46 this(object, name, null);
47 }
48
49 public Property(Object object, String name, Object argument) {
50
51 this.name = name;
52 this.argument = argument;
53
54 this.object = object;
55
56 StringBuffer getterName = new StringBuffer("get");
57 getterName.append(name.substring(0, 1).toUpperCase());
58 getterName.append(name.substring(1));
59 Class c = object.getClass();
60
61 //System.out.println(getterName + "(" + argument + ")");
62
63 try {
64 if (argument != null)
65 getter = c.getMethod(getterName.toString(), new Class[]{argument.getClass()});
66 else
67 getter = c.getMethod(getterName.toString(), (Class[]) null);
68 } catch (NoSuchMethodException e) {
69 }
70
71 }
72
73 public Method getGetter() {
74 return getter;
75 }
76
77 //public Object getObject() { return object; }
78
79 public String getAttributeName() {
80 if (argument == null) return name;
81 return name + "." + argument;
82 }
83
84 public Object getAttributeValue() {
85 if (object == null || getter == null)
86 return null;
87
88 Object result = null;
89 Object[] args = null;
90 if (argument != null)
91 args = new Object[]{argument};
92
93 try {
94 result = getter.invoke(object, args);
95 } catch (Exception e) {
96
97 e.printStackTrace(System.out);
98
99 throw new RuntimeException(e.getMessage());
100 }
101
102 return result;
103 }
104
105 public String getPropertyName() {
106 return name;
107 }
108
109 public String toString() {
110 return getAttributeValue().toString();
111 }
112 }