Archive for March, 2010

NoSuchMethodError of BeanUtils.copyProperty(due to wrong access level)

为了节省开发时间,今天打算做一个数据集合类,可以直接将java中的ResultSet,或者其他Collection派生类的内容copy到该集合中,然后加入自定义的一些方法,比如支持直接导出Excel、CSV、KDF、Image、HTML等。这里借助Apache Commons BeanUtils的和反射(Reflection),将数据库中的一行记录保存为一个对象,然后插入数据集中,结果老是报错如下:

USING CONVERTER org.apache.commons.beanutils.converters.IntegerConverter@1befab0
java.lang.reflect.InvocationTargetException: Cannot set id
at org.apache.commons.beanutils.BeanUtilsBean.copyProperty(BeanUtilsBean.java:449)
at org.apache.commons.beanutils.BeanUtils.copyProperty(BeanUtils.java:129)

… …
Caused by: java.lang.NoSuchMethodException: Property ‘id’ has no setter method
at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:1746)
at org.apache.commons.beanutils.BeanUtilsBean.copyProperty(BeanUtilsBean.java:447)

代码段如下:

private void dumpResultSet(ResultSet rs, Class clazz) throws Exception {
	ResultSetMetaData metaData = (ResultSetMetaData) rs.getMetaData();
	int colCnt = metaData.getColumnCount();
	Field[] fields = clazz.getDeclaredFields();

	while (rs.next()) {
		Object newInstance = clazz.newInstance();

		for (int i = 1; i <= colCnt; i++) {
			try {
				Object value = rs.getObject(i);
				for (int j = 0; j < fields.length; j++) {
					Field f = fields[j];
					if (f.getName().equalsIgnoreCase(
							metaData.getColumnName(i).replaceAll("_", ""))) {

						log.info("f.getName:" + f.getName());

						BeanUtils.copyProperty(newInstance, f.getName(),
								value);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		list.add(newInstance);
	}
}

The root cause: public is required for class UserInfo, or else you’ll get the ‘NoSuchMethodError‘ exception, DO NOT ignore the access level

public class UserInfo {
	private int id;
	private String user;
	private String password;

	public UserInfo() {
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
}

Eclipse一直提示:NoSuchMethodError,可是UserInfo里面明明有对应的setter方法,最后,才鬼使神差的发现,只要将UserInfo整个类声明为public就可以了。我一直都把注意力放到是否把setter和getter设置为public,却忽略了Bean的访问级别,希望碰到类似问题的朋友可以注意一下。

[ad]

重置PL/SQL Developer的试用期

如果你安装了PL/SQL Developer的试用版,但在30天内,没有足够的时间来试用它(可能被别的事情耽搁了),可以把下面的Python(V2.6)程序放入你的启动项里,它可以让你无限期的试用PL/SQL Developer (经测试对版本8.0.2.1505有效,未对其他版本测试),它的原理是每次系统启动的时候,自动检测上次”重置(reset)安装信息”的时间,这些信息保存在一个SQLite数据库中(foobar.db),请不要删除它,如果时差超过了25天,再次重置(reset)。

import os
import shutil
import win32api
import win32con
import sqlite3
import time
import _winreg
from time import *
from datetime import *

# Reset the trial period of PL/SQL Developer, USE AT YOUR OWN RISK, buy it if you really need it
# by Charry

list = list();

def traverse(root, key, list):
	hKey = _winreg.OpenKey(root, key);

	try:
		i = 0
	    	while 1:
			strFullSubKey = "";
			try:
				strSubKey = _winreg.EnumKey(hKey, i)
				strFullSubKey = key + "\\" + strSubKey;
			except WindowsError:
				hKey.Close();
				return;

			traverse(root, strFullSubKey, list);
			#print strFullSubKey;
			list.append(strFullSubKey);

	        	i+=1
	except	WindowsError:
		print

	hKey.Close();

def reg_delete_key(root, key):
	global list;
	traverse(root, key, list);
	for item in list:
		_winreg.DeleteKey(root, item);
		#print item;

	_winreg.DeleteKey(root, key);

def crack():
	conn = sqlite3.connect('foobar.db')
	conn.execute("""CREATE TABLE IF NOT EXISTS CONFIG(
		ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
		LAST_MODIFIED DATE DEFAULT CURRENT_DATE NULL)""")
	cursor = conn.cursor()
	today = datetime.now().strftime("%Y-%m-%d")

	# get the last_modified date
	sql = "SELECT * FROM {0} ORDER BY LAST_MODIFIED DESC;".format("CONFIG", today)
	cursor.execute(sql)
	str_last_modified = '1970-01-01'
	for row in cursor:
		str_last_modified = row[1]
		break

	print "Last Cracked Time: " + str_last_modified

	# get date delta
	tm_last_modified = datetime.strptime(str_last_modified, "%Y-%m-%d")
	time_diff = datetime.now() - tm_last_modified

	if time_diff.days > 25:
		print "Trial period was reset " + str(time_diff) + " ago"
		print "Save the world, save the cheerleader, let's teleport again"
		sql = "INSERT INTO {0} (LAST_MODIFIED) VALUES('{1}');".format("CONFIG", today)
		cursor.execute(sql)
		conn.commit()

		reg_delete_key(_winreg.HKEY_CURRENT_USER, r"Software\Microsoft\Security");
		reg_delete_key(_winreg.HKEY_CURRENT_USER, r"Software\Allround Automations");

	cursor.close()
	conn.close()

crack();

Keyword: Python, PL/SQL Developer, Crack

[ad]

A helper function to remove Windows registry key recursively

The default register function of Python(2.6.x) can’t delete a Windows registry key recursively, an exception(Access is denied) will be thrown if you try to delete a key with sub-keys , below is a wrapper which is easy-to-use, you could use it to delete a key and its sub-keys:

import _winreg 

def traverse(root, key, list):
	hKey = _winreg.OpenKey(root, key);

	try:
		i = 0
	    	while 1:
			strFullSubKey = "";
			try:
				strSubKey = _winreg.EnumKey(hKey, i)
				strFullSubKey = key + "\\" + strSubKey;
			except WindowsError:
				hKey.Close();
				return;

			traverse(root, strFullSubKey, list);
			#print strFullSubKey;
			list.append(strFullSubKey);

	        	i += 1
	except	WindowsError:
		print

	hKey.Close();

def reg_delete_key(root, key):
	global list;
	list = list();
	traverse(root, key, list);
	for item in list:
		_winreg.DeleteKey(root, item);
		#print item;

	_winreg.DeleteKey(root, key);

# Example:
reg_delete_key(_winreg.HKEY_CURRENT_USER, r"Software\charry.org");

Keyword: Python, RegDeleteKey, 注册表, 删除

[ad]

Switch to our mobile site