📄 jmutility.java
字号:
package jm.util;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import jm.framework.gui.JMEntity;
/**
* <p>Title: JM</p>
*
* <p>Copyright: Copyright (c) 2004-2006</p>
*
* <p>Company: 1SHome</p>
*
* <p>@author Spook</p>
*
* @version 1.5.3.1 see JDK 1.5.0.6
*/
public class JMUtility {
public static final String CLASS_NAME = "Utility";
// ////////////////////////////////////////////////////////////////////////
//System.out.print(untitled1.getClass().getDeclaredField("ia").get(untitled1));
/**
* 将inObj内存储器的值保存到outObj中
*
* @param inObj
* From class
* @param outObj
* To class
*/
@SuppressWarnings({ "unchecked", "unchecked" })
public static void reflectClass (Object inObj, Object outObj) {
final String CLASS = "class ";
final String SET = ".set";
final String JAVA = "(java";
final String JAVA1 = "(int";
try {
// /////////////////////////////////
if (inObj == null || outObj == null) {
return;
}
String inClassName = ("" + inObj.getClass()).substring((CLASS)
.length());
// System.out.println("outClassName=="+inClassName);
Class inClass = Class.forName(inClassName);
// Method[] get = inClass.getMethods();
// /////////////////////////////////
String outClassName = ("" + outObj.getClass()).substring((CLASS)
.length());
// System.out.println("outClassName==="+outClassName);
Class outClass = Class.forName(outClassName);
Method[] set = outClass.getMethods();
String name = "";
for (int i = 0; i < set.length; i++) {
name = "" + set[i];
if (name.indexOf(outClassName + ".set") > 0) {
try {
// System.out.println(name);
// type creat
Class[] sArgTypes = {};
Class[] gArgTypes = null;
// //////////////////////////////////////////////////////
Class[] STRING_TYPES = {String.class};
Class[] STRING_GROUP_TYPES = {String[].class};
Class[] LIST_TYPES = {List.class};
Class[] LIST_GROUP_TYPES = {List[].class};
// Class[] OBJECT_TYPES = { Object.class };
// Class[] OBJECT_GROUP_TYPES = { Object[].class };
Class[] INT_TYPES = {Integer.TYPE};
Class[] INT_GROUP_TYPES = {int[].class};
// Class[] LONG_TYPES = { Long.TYPE };
// Class[] LONG_GROUP_TYPES = { long[].class };
Class[] BIGDECIMAL_TYPES = {BigDecimal.class};
// Class[] BIGDECIMAL_GROUP_TYPES = { BigDecimal[].class };
// //////////////////////////////////////////////////////
if (name.indexOf("(java.lang.String)") > 0) {
gArgTypes = STRING_TYPES;
} else if (name.indexOf("(java.lang.String[])") > 0) {
gArgTypes = STRING_GROUP_TYPES;
} else if (name.indexOf("(java.util.List)") > 0) {
gArgTypes = LIST_TYPES;
} else if (name.indexOf("(java.util.List[])") > 0) {
gArgTypes = LIST_GROUP_TYPES;
} else if (name.indexOf("(java.math.BigDecimal)") > 0) {
gArgTypes = BIGDECIMAL_TYPES;
}
// //////////////////////////////////////////////////////
if (name.indexOf("(int)") > 0) {
// get Method Name from outObj
name = name.substring(name.indexOf(SET)
+ SET.length(), name.indexOf(JAVA1));
String getMethodName = "get" + name;
String setMethodName = "set" + name;
// copy value to outObj from inObj
Method g_method = outClass.getMethod(setMethodName,
INT_TYPES);
Method s_method = inClass.getMethod(getMethodName,
sArgTypes);
Object t = s_method.invoke(inObj);
if (t != null) {
Object[] val = {t};
g_method.invoke(outObj, val);
}
} else if (name.indexOf("(int[])") > 0) {
// get Method Name from outObj
name = name.substring(name.indexOf(SET)
+ SET.length(), name.indexOf(JAVA1));
String getMethodName = "get" + name;
String setMethodName = "set" + name;
// copy value to outObj from inObj
Method g_method = outClass.getMethod(setMethodName,
INT_GROUP_TYPES);
Method s_method = inClass.getMethod(getMethodName,
sArgTypes);
Object t = s_method.invoke(inObj);
if (t != null) {
Object[] val = {t};
g_method.invoke(outObj, val);
}
} else {
// get Method Name from outObj
name = name.substring(name.indexOf(SET)
+ SET.length(), name.indexOf(JAVA));
String getMethodName = "get" + name;
String setMethodName = "set" + name;
// copy value to outObj from inObj
Method g_method = outClass.getMethod(setMethodName,
gArgTypes);
Method s_method = inClass.getMethod(getMethodName,
sArgTypes);
Object t = s_method.invoke(inObj);
if (t != null&& !"".equals(t)) {
Object[] val = {t};
g_method.invoke(outObj, val);
}
}
} catch (Exception ex) {
// ex.printStackTrace();
}
}
}
} catch (Exception ex) {
// ex.printStackTrace();
}
}
// public 儊僜僢僪
/**
* 将JM2DArray内指定列的值保存到JMEntity内
* @param inClass JMEntity
* @param datasSource JM2DArray
* @param index int
*/
@SuppressWarnings("unchecked")
public static void reflectIntoEntity (JMEntity inClass, JM2DArray datasSource, int index) {
try {
if (datasSource == null) {
return;
}
Class outClass = inClass.getClass();
Class[] STRING_TYPES = {String.class};
JMVector<String> methodNames = datasSource.getColNames();
for (int i = 0; i < datasSource.colCount(); i++) {
try {
String setMethodName = "set" + methodNames.get(i);
// copy value to outObj from inObj
Object t = datasSource.getStringValue(index, i);
Method s_method = outClass.getMethod(setMethodName,
STRING_TYPES);
if (t != null&& !"".equals(t)) {
Object[] val = {t};
s_method.invoke(inClass, val);
}
} catch (Exception ex) {
// ex.printStackTrace();
// return;
}
}
} catch (Exception ex) {
// ex.printStackTrace();
}
}
/**
* 将JMEntity内值保存到JM2DArray内,
* @param inClass JMEntity
* @param datasSource JMVector
*/
@SuppressWarnings("unchecked")
public static void reflectIntoClass (JMEntity inClass, JMVector<String> datasSource) {
try {
if (datasSource == null) {
return;
}
Class outClass = inClass.getClass();
Class[] STRING_TYPES = {String.class};
String[] methodNames = inClass.getItems().split(JMEntity.SPLIT);
for (int i = 0; i < datasSource.size(); i++) {
try {
String setMethodName = "set" + methodNames[i];
// copy value to outObj from inObj
Object t = datasSource.get(i);
Method s_method = outClass.getMethod(setMethodName,
STRING_TYPES);
if (t != null&& !"".equals(t)) {
Object[] val = {t};
s_method.invoke(inClass, val);
}
} catch (Exception ex) {
// ex.printStackTrace();
}
}
} catch (Exception ex) {
// ex.printStackTrace();
}
}
public static void reflectIntoClass (JMEntity inClass, JMMap<String,String> datasSource) {
try {
if (datasSource == null) {
return;
}
Class outClass = inClass.getClass();
Class[] STRING_TYPES = {String.class};
String[] methodNames = inClass.getItems().split(JMEntity.SPLIT);
for (int i = 0; i < datasSource.size(); i++) {
try {
String setMethodName = "set" + methodNames[i];
// copy value to outObj from inObj
Object t = datasSource.get(methodNames[i]);
Method s_method = outClass.getMethod(setMethodName,
STRING_TYPES);
if (t != null&& !"".equals(t)) {
Object[] val = {t};
s_method.invoke(inClass, val);
}
} catch (Exception ex) {
// ex.printStackTrace();
}
}
} catch (Exception ex) {
// ex.printStackTrace();
}
}
// ////////////////////////////////////////////////////////////////////////
// public 儊僜僢僪
/**
* 将JMEntity内值保存到JM2DArray内,index是行号,<0为末尾(追加),name为指定列名
* @param inClass JMEntity
* @param datasSource JM2DArray
* @param name String[]
* @param index int
*/
@SuppressWarnings("unchecked")
public static void reflectIntoJM2DArray (JMEntity inClass, JM2DArray datasSource, String[] name, int index) {
try {
if (datasSource == null) {
return;
}
Class t_inClass = inClass.getClass();
if (datasSource.rowCount() != 0 && index < 0) {
datasSource.nextRow();
}
for (int i = 0; i < name.length; i++) {
try {
String setMethodName = "get" + name[i];
// copy value to outObj from inObj
Method s_method = t_inClass.getMethod(setMethodName);
Object t = changNvl(s_method.invoke(inClass));
if (index < 0) {
datasSource.addItem(name[i], t.toString());
} else {
datasSource.replaceItem(index, name[i], t.toString());
}
} catch (Exception ex) {
}
}
} catch (Exception ex) {
}
}
// public 儊僜僢僪
/**
* 将JMEntity内值保存到JM2DArray内,index是行号,<0为末尾(追加)
* @param inClass JMEntity
* @param datasSource JM2DArray
* @param index int
*/
@SuppressWarnings("unchecked")
public static void reflectIntoJM2DArray (JMEntity inClass, JM2DArray datasSource, int index) {
try {
if (datasSource == null) {
return;
}
Class t_inClass = inClass.getClass();
if (index < 0) {
datasSource.nextRow();
}
JMVector<String> colnames = datasSource.getColNames();
if(colnames.size()==0){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -