📄 methodextagshandler.java
字号:
boolean superclasses = TypeConversionUtil.stringToBoolean(attributes.getProperty("superclasses"), false);
boolean sort = TypeConversionUtil.stringToBoolean(attributes.getProperty("sort"), true);
boolean generateChildren = TypeConversionUtil.stringToBoolean(attributes.getProperty("generateChildren"), true);
Collection members = null;
switch (forType) {
case FOR_METHOD:
members = currentClass.getMethods(superclasses);
break;
default:
throw new XDocletException("Bad type: " + forType);
}
if (sort) {
// sort fields, but we should make a copy first, because members is
// not a new copy, it's shared by all
List sortedMembers = new ArrayList(members);
members = sortedMembers;
}
for (Iterator j = members.iterator(); j.hasNext();) {
XMember member = (XMember) j.next();
switch (forType) {
case FOR_METHOD:
setCurrentMethod((XMethod) member);
break;
default:
throw new XDocletException("Bad type: " + forType);
}
XMethod getter = (XMethod) member;
if (super.isGetterMethod(getter)) {
Properties pro = new Properties();
// iterate through sub-components only
pro.setProperty("tagName", "hibernate.component");
if (super.hasTag(pro, FOR_METHOD) && generateChildren) {
Type type = getter.getReturnType();
String temp = prefix + ("get" + type.getType().getName() + "Form().");
forAllMembersEx(type.getType(), template, attributes, forType, temp);
}
setCurrentClass(member.getContainingClass());
curprefix = prefix;
generate(template);
}
}
setCurrentClass(currentClass);
}
/**
* Method className
*
* @return
*/
public String className() {
return currentClass.getName();
}
/**
* Method classNameLower
* @return
*/
public String classNameLower() {
String name = currentClass.getName();
char c = name.charAt(0);
c = Character.toLowerCase(c);
return c + name.substring(1);
}
/**
* Generates a random value for a field. Return "0" for boolean type. used
* to generate random values for sample-data.xml
*
* @return a random value
* @throws XDocletException
*/
public String randomValueForDbUnit() throws XDocletException {
Properties pros = new Properties();
pros.put("type", "content");
String mtype = super.methodType(pros);
StringBuffer result = new StringBuffer();
if ("java.lang.Integer".equals(mtype) || "int".equals(mtype)) {
result.append((int) ((Math.random() * Integer.MAX_VALUE)) );
} else if ("java.lang.Float".equals(mtype) || "float".equals(mtype)) {
result.append((float) ((Math.random() * Float.MAX_VALUE)));
} else if ("java.lang.Long".equals(mtype) || "long".equals(mtype)) {
result.append((long) ((Math.random() * Long.MAX_VALUE)));
} else if ("java.lang.Double".equals(mtype) || "double".equals(mtype)) {
result.append((double) ((Math.random() * Double.MAX_VALUE)));
} else if ("java.lang.Short".equals(mtype) || "short".equals(mtype)) {
result.append((short) ((Math.random() * Short.MAX_VALUE)));
} else if ("java.lang.Byte".equals(mtype) || "byte".equals(mtype)) {
result.append((byte) ((Math.random() * Byte.MAX_VALUE)));
} else if ("java.lang.Boolean".equals(mtype) || "boolean".equals(mtype)) {
result.append("N");
} else if ("java.util.Date".equals(mtype) || "java.sql.Date".equals(mtype)) {
result.append(getDate(new Date()));
} else if ("java.sql.Timestamp".equals(mtype)) {
result.append(new Timestamp(new Date().getTime()).toString());
} else if ("email".equalsIgnoreCase(super.propertyName())) {
result.append(super.propertyName() + (int) ((Math.random() * Integer.MAX_VALUE)) + "@dev.java.net");
} else { // default to String for everything else
String stringWithQuotes = generateStringValue();
result.append(stringWithQuotes.substring(1, stringWithQuotes.length()-1));
}
//System.out.println("propertyType: " + mtype + " | dbUnit value: " + result.toString());
return result.toString();
}
/**
* Generates a random value for a field. Return "true" for boolean type.
* Returned values are used in junit tests to create new values of the
* appropriate type for testing.
*
* @return a random value
* @throws XDocletException
*/
public String randomValueForSetter() throws XDocletException {
Properties pros = new Properties();
pros.put("type", "content");
String mtype = super.methodType(pros);
StringBuffer result = new StringBuffer();
if ("java.lang.Integer".equals(mtype)) {
result.append("new Integer(" + (int) ((Math.random() * Integer.MAX_VALUE)) + ")");
} else if ("int".equals(mtype)) {
result.append("(int) " + (int) ((Math.random() * Integer.MAX_VALUE)));
} else if ("java.lang.Float".equals(mtype) ) {
result.append("new Float(" + (float) ((Math.random() * Float.MAX_VALUE)) + ")");
} else if ("float".equals(mtype)) {
result.append("(float) " + (float) ((Math.random() * Float.MAX_VALUE)));
} else if ("java.lang.Long".equals(mtype)) {
// not sure why, but Long.MAX_VALUE results in too large a number
result.append("new Long(" + (long) ((Math.random() * Integer.MAX_VALUE)) + ")");
} else if ("long".equals(mtype)) {
// not sure why, but Long.MAX_VALUE results in too large a number
result.append((long) ((Math.random() * Integer.MAX_VALUE)));
} else if ("java.lang.Double".equals(mtype)) {
result.append("new Double(" + (double) ((Math.random() * Double.MAX_VALUE)) + ")");
} else if ("double".equals(mtype)) {
result.append((double) ((Math.random() * Double.MAX_VALUE)));
} else if ("java.lang.Short".equals(mtype)) {
result.append("new Short(\"" + (short) ((Math.random() * Short.MAX_VALUE)) + "\")");
} else if ("short".equals(mtype)) {
result.append("(short)" + (short) ((Math.random() * Short.MAX_VALUE)));
} else if ("java.lang.Byte".equals(mtype)) {
result.append("new Byte(\"" + (byte) ((Math.random() * Byte.MAX_VALUE)) + "\")");
} else if ("byte".equals(mtype)) {
result.append("(byte) " + (byte) ((Math.random() * Byte.MAX_VALUE)));
} else if ("java.lang.Boolean".equals(mtype)) {
result.append("new Boolean(\"false\")");
} else if ("boolean".equals(mtype)) {
result.append("false");
} else if ("java.util.Date".equals(mtype)) {
result.append("new java.util.Date()");
} else if ("java.sql.Date".equals(mtype)) {
result.append("new java.sql.Date()");
} else if ("java.sql.Timestamp".equals(mtype)) {
result.append("java.sql.Timestamp.valueOf(\"" + new Timestamp(new Date().getTime()).toString() + "\")");
} else if ("email".equalsIgnoreCase(super.propertyName())) {
result.append("\"" + super.propertyName() + (int) ((Math.random() * Integer.MAX_VALUE)) +
"@dev.java.net\"");
} else { // default to String for everything else
result.append(generateStringValue());
}
//System.out.println("propertyType: " + mtype + " | setter value: " + result.toString());
return result.toString();
}
private String generateStringValue() throws XDocletException {
int maxLen = getHibernateLength();
if (maxLen == 0) { maxLen = 25; }
StringBuffer result = new StringBuffer("\"");
for (int i = 0; (i < maxLen); i++) {
int j = 0;
if (i % 2 == 0) {
j = (int) ((Math.random() * 26) + 65);
} else {
j = (int) ((Math.random() * 26) + 97);
}
result.append(new Character((char)j).toString());
}
result.append("\"");
return result.toString();
}
private int getHibernateLength() throws XDocletException {
Properties props = new Properties();
props.setProperty("tagName", "hibernate.property");
props.setProperty("paramName", "length");
int length = 0;
if (super.hasTag(props, FOR_METHOD)) {
String tagVal = super.getTagValue(props, FOR_METHOD);
//System.out.println("property has hibernate.property length= " + tagVal);
length = Integer.valueOf(tagVal).intValue();
}
return length;
}
/**
* copy from org.appfuse.util.DateUtil
*
* @param aDate
* @return
*/
private static final String getDate(Date aDate) {
return getDate(aDate, datePattern);
}
private static final String getDate(Date aDate, String pattern) {
SimpleDateFormat df = null;
String returnValue = "";
if (aDate != null) {
df = new SimpleDateFormat(pattern);
returnValue = df.format(aDate);
}
return returnValue;
}
/**
* Return default datePattern (MM/dd/yyyy)
* @return a string representing the date pattern on the UI
*/
public static synchronized String getDatePattern() {
String result;
try {
result = ResourceBundle.getBundle("ApplicationResources", Locale.getDefault())
.getString("date.format");
} catch (MissingResourceException mse) {
result = "MM/dd/yyyy";
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -