admin 管理员组文章数量: 887021
2023年12月21日发(作者:电商网站网页设计)
目录
需要包: .......................................................................................................................................... 2
工具类POBuildUtil ......................................................................................................................... 2
工具类BuildUtil .............................................................................................................................. 6
工具类Assistant ............................................................................................................................... 8
RenderClass: ................................................................................................................................ 27
使用例子: .................................................................................................................................... 29
需要包:
1. apache-commons-lang-**.jar
2. asm-**.jar
工具类POBuildUtil
import ;
import tputStream;
import tion;
import or;
import ils;
import riter;
import Visitor;
import s;
public class POBuildUtil extends ClassLoader implements Opcodes {
public Class build(String clsname,String savepath,Collection
properties)
{
Class cls = null;
try
{
String classname = erClassName(clsname);
ClassWriter cw = new ClassWriter(false);
(V1_1, ACC_PUBLIC, classname, null,
"java/lang/Object", null);
MethodVisitor mw = ethod(ACC_PUBLIC, "
"()V", null, null);
arInsn(ALOAD, 0);
ethodInsn(INVOKESPECIAL, "java/lang/Object",
"
nsn(RETURN);
axs(1, 1);
nd();
BuildProperty property = null;
String propertytype = null;
String propertyname = null;;
Iterator iterator = or();
while (t())
{
property = (BuildProperty)();
propertytype =
erClassName(e());
propertyname = lize(e());
//n(propertyname);
ield(ACC_PRIVATE, e(),
"L"+propertytype+";", null, null).visitEnd();
mw = ethod(ACC_PUBLIC,
"get"+propertyname, "()L"+propertytype+";", null,
null);
ode();
arInsn(ALOAD, 0);
mw
.visitFieldInsn(GETFIELD, classname,
e(),
"L"+propertytype+";");
nsn(ARETURN);
axs(1, 1);
nd();
mw = ethod(ACC_PUBLIC,
"set"+propertyname, "(L"+propertytype+";)V",
null, null);
ode();
arInsn(ALOAD, 0);
arInsn(ALOAD, 1);
mw
.visitFieldInsn(PUTFIELD, classname,
e(),
"L"+propertytype+";");
axs(2, 2);
nsn(RETURN);
nd();
}
nd();
byte[] code = Array();
if (savepath!=null)
{
NewFile(savepath);
FileOutputStream fos =
FileOutputStream(savepath);
(code);
();
}
cls = Class(clsname, code, 0, );
new
}
}
}
return cls;
catch (Throwable e)
{
}
return cls;
tackTrace();
工具类BuildUtil
public class BuildUtil {
public static String transferClassName(Class
cls)
{
String clsname = e();
return e('.', '/');
}
public static String transferClassName(String
clsname)
{
return e('.', '/');
}
}
一. 工具类BuildProperty
public class BuildProperty {
private String name;
private String type;
public BuildProperty()
{
}
public BuildProperty(String name,String type)
{
= name;
= type;
}
public String getName() {
return name;
}
public void setName(String name) {
= name;
}
public String getType() {
return type;
}
public void setType(String type) {
= type;
}
}
工具类Assistant
import edReader;
import edWriter;
import ;
import putStream;
import tputStream;
import treamReader;
import StreamWriter;
import DateFormat;
import ar;
import tContext;
import rvletRequest;
import ;
public class Assistant {
private static final Logger log = ger();
public static String convertRequestParameter(String parameter)
throws Exception
{
}
public static String convertRequestParameter(String parameter,String
return convertRequestParameter(parameter,null);
format) throws Exception
{
}
public static boolean notEmpty(HttpServletRequest request,String
if (parameter!=null)
{
}
return parameter;
if (format==null)
format="utf-8";
parameter = (parameter,format);
key)
{
if (ameter(key)!=null &&
ameter(key).length()>0)
{
return true;
}
else
{
return false;
}
}
public static void writeFileContent(String
content ,ServletContext context) throws Exception
{
writeFileContent( getFullPath(url,context),content);
}
public static String HTMLEncode(String txt) {
if (txt!=null && ()>0)
{
txt=eAll("&","&");
txt=eAll("<","<");
txt=eAll(">",">");
url,String
}
}
txt=eAll(""",""");
txt=eAll("'","");
return txt;
public static boolean existFile(String urlpath,ServletContext context)
{
}
public static void writeFileContent(String path,String content) throws
File file = new File(getFullPath(urlpath, context));
if (())
return true;
else
return false;
Exception
{
File file = new File(path);
if (!())
NewFile();
write= new OutputStreamWriter(new OutputStreamWriter
FileOutputStream(file),"GBK");
}
public static String getFileContent(String url,ServletContext context)
BufferedWriter writer=new BufferedWriter(write);
(content);
();
();
throws Exception
StringBuffer buf = new StringBuffer();
File file = new File(path);
if (())
{
InputStreamReader read = new InputStreamReader(new
{
}
public static String getFileContent(String path) throws Exception
{
String filepath = getFullPath(url,context);
return getFileContent(filepath);
FileInputStream(file),"GBK");
}
}
BufferedReader reader=new BufferedReader(read);
try
{
String content = ne();
while (content!=null)
{
}
(content);
content = ne();
}finally
{
}
if (reader!=null)
();
if (read!=null)
();
return ng();
//���ڼ�(����)
public static addTimeByDay( date,int days)
throws Exception
{
}
public static addTimeByMinutes( date,int
Calendar calendar=tance();
e(date);
(,()+days);
return e();
minutes) throws Exception
{
Calendar calendar=tance();
e(date);
(,()+minutes);
}
public static addTimeBySeconds( date,int
return e();
seconds) throws Exception
{
Calendar calendar=tance();
e(date);
(,()+seconds);
}
public static nowTime() throws Exception
{
SimpleDateFormat sdf = new
return e();
SimpleDateFormat("yyyy-MM-dd");
String datestr =
(tance().getTime());
}
public static nowFullTime() throws Exception
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd
return (datestr);
HH:mm:ss");
String datestr =
(tance().getTime());
}
return (datestr);
public static nowFullTime(String format) throws
Exception
{
SimpleDateFormat sdf = new SimpleDateFormat(format);
String datestr =
(tance().getTime());
}
public static String convertDateStrToString(String datestr,String
return (datestr);
format) throws Exception
{
String result = null;
SimpleDateFormat sdf = new SimpleDateFormat(format);
try
{
}
catch (Exception ex)
{
}
sdf = new SimpleDateFormat("yyyy-MM-dd");
result = ((datestr));
result = ((datestr));
}
return result;
public static String convertDateToString( date,String
format) throws Exception
{
}
public static formatDateStr(String datestr) throws
SimpleDateFormat sdf = new SimpleDateFormat(format);
return (date);
Exception
{
SimpleDateFormat sdf = new
SimpleDateFormat("yyyy-MM-dd");
}
public static formatDateStr(String datestr,String format)
return (datestr);
throws Exception
{
result = null;
SimpleDateFormat sdf = new SimpleDateFormat(format);
try
}
{
}
catch (Exception ex)
{
}
return result;
sdf = new SimpleDateFormat("yyyy-MM-dd");
result = (datestr);
result = (datestr);
public static formatFullDateStr(String datestr) throws
Exception
{
result = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd
HH:mm:ss");
try
{
}
catch (Exception ex)
result = (datestr);
}
{
}
return result;
sdf = new SimpleDateFormat("yyyy-MM-dd");
result = (datestr);
public static String getFileName(String filepath)
{
}
public static void copy(ServletContext context,String form,String
filepath = e('/', '');
String filename = filepath;
if (dexOf('')>0)
{
}
return filename;
filename = ing(dexOf('')+1);
to ,boolean removeold) throws ption
copy(getFullPath(form,context),getFullPath(to,context),removeold);
{
}
//�ļ��������ƶ�����removeold �Ƿ�ɾ��ԭ�ļ�
public static void copy( String from, String to,boolean
removeold )throws ption
{
int BUFF_SIZE = 1024;
byte[] buffer = new byte[ BUFF_SIZE ];
tream in = null;
. OutputStream out = null;
try
{
in = new FileInputStream( from );
out = new FileOutputStream( to );
while ( true )
{
synchronized ( buffer )
{
int amountRead = ( buffer );
if ( amountRead == -1 )
{
break;
}
( buffer, 0, amountRead );
}
}
}
finally
{
if ( in != null )
{
();
}
if ( out != null )
{
();
}
}
n("..........................Copy Finish");
if (removeold)
{
file =new (from);
if (())
{
n("delete file:"+h());
}
}
}
();
public static String getUploadPath(String path)
{
}
//������·���õ����·��
public static String getFullPath(String url,ServletContext context)
{
url = e('', '/');
String path = lPath(url);
return e('/', '');
if (f("/")!=0)
{
}
if (dexOf("/")!=()-1)
{
}
return path;
path = path+"/";
path = "/"+path;
}
//������·��ɾ���ļ�
public static boolean deleteFile(String url,ServletContext context)
throws Exception
{
file =new (getFullPath(e('/',
''),context));
String filepath = h();
String ext = ing(dexOf(".")+1);
String minifilepath =
ing(0,dexOf("."))+"_mini."+ext;
minifile = new (minifilepath);
if (())
{
}
if (())
{
}
return false;
("delete file:"+h());
return ();
();
}
public static String createPathByDate(String url,ServletContext
context)
if (dexOf('')!=()-1 &&
{
String fullpath = getFullPath(url,context);
if (dexOf('')!=()-1)
Calendar nowtime = tance();
fullpath +=f(())+"";
SimpleDateFormat sdf = new SimpleDateFormat("MMdd");
fullpath+=(e())+"";
File file = new File(fullpath);
if (!())
();
fullpath +="";
dexOf('/')!=()-1)
url +="/";
url += f(())+"/";
url += (e())+"/";
}
return url;
public static boolean createPath(String path,ServletContext context)
throws Exception
ַ
public static String getContextUrl(String url,HttpServletRequest
{
}
//����һ����Ե�ַ�õ��ڵ�ǰ���������µ�һ����Ե�String fullpath = getFullPath(path,context);
File file = new File(fullpath);
boolean result= true;
if (!())
{
}
return result;
result =();
request,ServletContext context)
{
if (textPath()!=null &&
textPath().length()>0)
}
//���request �����Ƿ��ж�Ӧ��ֵ
public static Object getValue(HttpServletRequest request,String key)
{
}
public static void createNewFile(String savepath) throws
if (notEmpty(request, key))
return ameter(key);
{
}
return url;
if (f("/")!=0)
url = "/"+url;
url = textPath()+url;
else
return ribute(key);
ption
{
int pos = dexOf("");
}
}
String dir = ing(0,pos);
File directory =new File(dir);
if (!())
();
File file = new File(savepath);
if (())
NewFile();
RenderClass:
package on;
import ;
/*��ݿ�ӳ��������*/
public class RenderClass {
/*����*/
private String className;
/*��Ӧ����*/
private String tableName;
private String classPath;
/*����*/
private List properties;
public RenderClass()
{
properties = null;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
ame = className;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
ame = tableName;
}
public List getProperties() {
return properties;
}
public void setProperties(List properties) {
ties = properties;
}
public void setClassPath(String classPath) {
ath = classPath;
}
public String getClassPath() {
return classPath;
}
}
使用例子:
POBuildUtil util = new POBuildUtil();
(ssName(),ssPath()+""+leName()+".class",perties());
//在这里成功生成。class文件
注:以上代码均来自于网上资料。可直接复制即可使用
版权声明:本文标题:java代码动态生成java的.class文件 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/free/1703163001h440521.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论