Skip to content

Commit b64e407

Browse files
author
hborders
committed
Added package com.sun.max.program.option from Maxine Base
1 parent b2bf117 commit b64e407

8 files changed

Lines changed: 1558 additions & 0 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (c) 2009 Sun Microsystems, Inc. All rights reserved.
3+
*
4+
* Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product
5+
* that is described in this document. In particular, and without limitation, these intellectual property
6+
* rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or
7+
* more additional patents or pending patent applications in the U.S. and in other countries.
8+
*
9+
* U.S. Government Rights - Commercial software. Government users are subject to the Sun
10+
* Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its
11+
* supplements.
12+
*
13+
* Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and Solaris are trademarks or
14+
* registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks
15+
* are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the
16+
* U.S. and other countries.
17+
*
18+
* UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open
19+
* Company, Ltd.
20+
*/
21+
package com.sun.max.program.option;
22+
23+
import com.sun.max.*;
24+
import com.sun.max.program.ProgramError;
25+
26+
import java.lang.reflect.Field;
27+
28+
/**
29+
* This class implements a command line option that stores its value in a field
30+
* via reflection.
31+
*
32+
* @author Ben L. Titzer
33+
*/
34+
public class FieldOption<T> extends Option<T> {
35+
36+
protected final Object object;
37+
protected final Field field;
38+
protected T nullValue;
39+
40+
public FieldOption(String name, Object object, Field field, T defaultValue, Type<T> type, String help) {
41+
super(name, defaultValue, type, help);
42+
this.object = object;
43+
this.field = field;
44+
this.nullValue = defaultValue;
45+
}
46+
47+
/**
48+
* Gets the value of this option. This implementation stores the field's value in a reflected field
49+
* and access requires a reflective access.
50+
* @return the value of this option
51+
*/
52+
@Override
53+
public T getValue() {
54+
try {
55+
return Utils.<T>cast(field.get(object));
56+
} catch (IllegalAccessException e) {
57+
throw ProgramError.unexpected(e);
58+
}
59+
}
60+
61+
/**
62+
* Sets the value of this option. This implementation stores the field's value in a reflected field
63+
* and thus setting the value requires a reflective access.
64+
* @param value the value to set the new value to
65+
*/
66+
@Override
67+
public void setValue(T value) {
68+
try {
69+
if (value == null) {
70+
field.set(object, nullValue);
71+
} else {
72+
field.set(object, value);
73+
}
74+
} catch (Exception e) {
75+
throw ProgramError.unexpected("Error updating the value of " + field, e);
76+
}
77+
}
78+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
3+
*
4+
* Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product
5+
* that is described in this document. In particular, and without limitation, these intellectual property
6+
* rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or
7+
* more additional patents or pending patent applications in the U.S. and in other countries.
8+
*
9+
* U.S. Government Rights - Commercial software. Government users are subject to the Sun
10+
* Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its
11+
* supplements.
12+
*
13+
* Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and Solaris are trademarks or
14+
* registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks
15+
* are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the
16+
* U.S. and other countries.
17+
*
18+
* UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open
19+
* Company, Ltd.
20+
*/
21+
package com.sun.max.program.option;
22+
23+
import com.sun.max.*;
24+
25+
/**
26+
* The {@code MaxPackageOptionType} class.
27+
* Created Nov 20, 2007
28+
*
29+
* @author Ben L. Titzer
30+
*/
31+
public class MaxPackageOptionType extends Option.Type<MaxPackage> {
32+
public final MaxPackage superPackage;
33+
public final Class classType;
34+
35+
public MaxPackageOptionType(MaxPackage superPackage, Class classType) {
36+
super(MaxPackage.class, "vm-package");
37+
this.superPackage = superPackage;
38+
this.classType = classType;
39+
}
40+
@Override
41+
public MaxPackage parseValue(String string) {
42+
final String fullName = superPackage.name() + "." + string;
43+
if (string != null && string.length() > 0) {
44+
MaxPackage result = MaxPackage.fromName(fullName);
45+
if (result == null) {
46+
result = MaxPackage.fromName(string);
47+
}
48+
if (result == null) {
49+
throw new Option.Error("MaxPackage not found: " + string + " (or " + fullName + ")");
50+
}
51+
return result;
52+
}
53+
return null;
54+
}
55+
56+
@Override
57+
public String getValueFormat() {
58+
return "<package-name>";
59+
}
60+
}
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/*
2+
* Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
3+
*
4+
* Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product
5+
* that is described in this document. In particular, and without limitation, these intellectual property
6+
* rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or
7+
* more additional patents or pending patent applications in the U.S. and in other countries.
8+
*
9+
* U.S. Government Rights - Commercial software. Government users are subject to the Sun
10+
* Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its
11+
* supplements.
12+
*
13+
* Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and Solaris are trademarks or
14+
* registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks
15+
* are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the
16+
* U.S. and other countries.
17+
*
18+
* UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open
19+
* Company, Ltd.
20+
*/
21+
package com.sun.max.program.option;
22+
23+
import com.sun.max.*;
24+
25+
/**
26+
* The {@code Option} class represents a command-line or other configuration
27+
* option with a particular name, type, and description.
28+
*
29+
* @author Ben L. Titzer
30+
*/
31+
public class Option<T> implements Cloneable {
32+
33+
/**
34+
* The {@code Option.Type} class represents a type for an option. This class
35+
* implements method for parsing and unparsing values from strings.
36+
*/
37+
public abstract static class Type<T> {
38+
protected final String typeName;
39+
public final Class<T> type;
40+
41+
protected Type(Class<T> type, String typeName) {
42+
this.typeName = typeName;
43+
this.type = type;
44+
}
45+
46+
public String getTypeName() {
47+
return typeName;
48+
}
49+
50+
public String unparseValue(T value) {
51+
return String.valueOf(value);
52+
}
53+
54+
public abstract T parseValue(String string) throws Option.Error;
55+
56+
public abstract String getValueFormat();
57+
58+
public Option<T> cast(Option option) {
59+
return Utils.cast(option);
60+
}
61+
}
62+
63+
public static class Error extends java.lang.Error {
64+
65+
public Error(String message) {
66+
super(message);
67+
}
68+
}
69+
70+
protected final String name;
71+
protected T defaultValue;
72+
protected final Type<T> type;
73+
protected final String help;
74+
protected T value;
75+
76+
/**
77+
* The constructor for the {@code Option} class creates constructs a new
78+
* option with the specified parameters.
79+
*
80+
* @param name the name of the option as a string
81+
* @param defaultValue the default value of the option
82+
* @param type the type of the option, which is used for parsing and unparsing values
83+
* @param help a help description which is usually used to generate a formatted
84+
* help output
85+
*/
86+
public Option(String name, T defaultValue, Type<T> type, String help) {
87+
this.defaultValue = defaultValue;
88+
this.name = name;
89+
this.type = type;
90+
this.help = help;
91+
value = null;
92+
}
93+
94+
/**
95+
* The {@code getName()} method returns the name of this option as a string.
96+
*
97+
* @return the name of this option
98+
*/
99+
public String getName() {
100+
return name;
101+
}
102+
103+
/**
104+
* Sets the default value for this option,
105+
* which is the value that the option retains if no assignment is made.
106+
*
107+
* @param val the default value of the option
108+
*/
109+
public void setDefaultValue(T val) {
110+
defaultValue = val;
111+
}
112+
113+
/**
114+
* The {@code getDefaultValue()} method returns the default value for this option,
115+
* which is the value that the option retains if no assignment is made.
116+
*
117+
* @return the default value of the option
118+
*/
119+
public T getDefaultValue() {
120+
return defaultValue;
121+
}
122+
123+
/**
124+
* The {@code getValue()} method retrieves the current value of this option.
125+
*
126+
* @return the current value of this option
127+
*/
128+
public T getValue() {
129+
return !assigned ? defaultValue : value;
130+
}
131+
132+
private boolean assigned;
133+
134+
/**
135+
* The {@code setValue()) method sets the value of this option.
136+
*
137+
* @param value the new value to this option
138+
*/
139+
public void setValue(T value) {
140+
assigned = true;
141+
this.value = value;
142+
}
143+
144+
/**
145+
* The {@code setValue()} method sets the value of this option, given a string value.
146+
* The type of this option is used to determine how to parse the string into a value
147+
* of the appropriate type. Thus this method may potentially throw runtime exceptions
148+
* if parsing fails.
149+
*
150+
* @param string the new value of this option as a string
151+
*/
152+
public void setString(String string) {
153+
setValue(type.parseValue(string));
154+
}
155+
156+
/**
157+
* The {@code getType()} method returns the type of this option.
158+
* @return the type of this option.
159+
*/
160+
public Type<T> getType() {
161+
return type;
162+
}
163+
164+
/**
165+
* The {@code getString()} method retrieves the value of this option as a string.
166+
* The type of this option is used to determine how to unparse the value into a string.
167+
*
168+
* @return the value of this option as a string
169+
*/
170+
public String getString() {
171+
return type.unparseValue(getValue());
172+
}
173+
174+
public String getHelp() {
175+
return help;
176+
}
177+
178+
@Override
179+
public String toString() {
180+
return getName();
181+
}
182+
}

0 commit comments

Comments
 (0)