java 动态编译

package com.fyh.generate.test;

 

import java.beans.BeanDescriptor;

import java.beans.BeanInfo;

import java.beans.IntrospectionException;

import java.beans.Introspector;

import java.beans.PropertyDescriptor;

import java.io.File;

import java.io.IOException;

import java.net.MalformedURLException;

import java.net.URL;

import java.net.URLClassLoader;

import java.util.Arrays;

import java.util.Iterator;

import java.util.Map;

 

import javax.tools.DiagnosticCollector;

import javax.tools.JavaCompiler;

import javax.tools.StandardJavaFileManager;

import javax.tools.ToolProvider;

 

import org.apache.commons.beanutils.BeanUtils;

import org.apache.commons.lang3.StringUtils;

import org.junit.Test;

 

import sun.tools.jar.Main;

 

public class MappingObjectProperty {

public Map<String, String> putPropertyToMap(Class cls) throws Exception {

Person person = new Person();

person.setId(1);

person.setEmail("asdfsdf");

person.setName("zhangsan");

/*

* Map str = BeanUtils.describe(person); Iterator<Map.Entry> it =

* str.entrySet().iterator(); while(it.hasNext()) { Map.Entry en =

* it.next(); System.out.println("res="+en.getKey()+":"+en.getValue());

* }

*/

BeanInfo beanInfo = Introspector.getBeanInfo(cls);

PropertyDescriptor pds[] = beanInfo.getPropertyDescriptors();

BeanDescriptor bd = beanInfo.getBeanDescriptor();

System.out.println(bd.getName());

System.out.println(StringUtils.capitalize(bd.getName()));

System.out.println(StringUtils.uncapitalize(bd.getName()));

for (PropertyDescriptor propertyDescriptor : pds) {

System.out.println("property:" + propertyDescriptor.getName() + ":"

+ propertyDescriptor.getShortDescription()+":"+propertyDescriptor.getPropertyType()+":"+propertyDescriptor.getPropertyEditorClass());

 

}

return null;

}

 

public Class loadClass(File file) throws MalformedURLException, ClassNotFoundException

{

//URLClassLoader loader = new URLClassLoader(new URL[]{new URL("file:/"+file.getPath()+"/")});

URLClassLoader loader = new URLClassLoader(new URL[]{file.toURI().toURL()});

 

return loader.loadClass(StringUtils.substringBefore("com.fyh.generate.test.Person", ".class"));

}

 

public boolean compileJavaFile(File file) throws IOException

{

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

DiagnosticCollector diagnosticCollector = new DiagnosticCollector();

StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticCollector, null, null);

Iterable compilationUnits = fileManager.getJavaFileObjectsFromStrings(Arrays.asList(file

                 .getAbsolutePath()));

JavaCompiler.CompilationTask task = compiler.getTask(null,

                 fileManager, diagnosticCollector, null, null, compilationUnits);

         boolean success = task.call();

         fileManager.close();

         System.out.println((success) ? "编译成功" : "编译失败");

         return success;

}

 

public static void main(String[] args) throws Exception {

MappingObjectProperty m = new MappingObjectProperty();

//m.putPropertyToMap(Person.class);

m.compileJavaFile(new File("F:\\workspace\\generateCode\\src\\com\\fyh\\generate\\test\\Person.java"));

Class cls = m.loadClass(new File("F:\\workspace\\generateCode\\src\\com\\fyh\\generate\\test\\Person.java"));

m.putPropertyToMap(cls);

 

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值