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);

 

}

}

你可能感兴趣的:(java,动态)