Java Pojo 회전 flex vo 도구 류

9873 단어
package dev.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.apache.commons.lang.StringUtils;

public class JavaPojo2FlexVO {

	public JavaPojo2FlexVO() {
	}

	//   java        vo  
	public static String getClassType(Class<?> c) {
		String typeName = c.getSimpleName();

		if (typeName.equals("String") || typeName.equals("Date")) {
			return typeName;
		} else if (typeName.equalsIgnoreCase("BigDecimal")
				|| typeName.equalsIgnoreCase("Decimal")
				|| typeName.equalsIgnoreCase("Double")
				|| typeName.equalsIgnoreCase("Long")) {
			return "Number";
		} else if (typeName.equals("Integer") || typeName.equals("int")) {
			return "int";
		} else if (typeName.equals("Boolean")||typeName.equals("boolean")) {
			return "Boolean";
		} else if (typeName.equals("Timestamp")) {
			return "Date";
		} else if (typeName.equals("List") || typeName.equals("ArrayList")
				|| typeName.equals("LinkedList") || typeName.equals("Set")
				|| typeName.equals("HashSet")) {
			return "ArrayCollection";

		} else if (!c.isPrimitive()) {
			return typeName;
		} else {
			return "*";//             
		}

	}

	//   c  count ,        as  
	public static String repeat(String c, int count) {
		StringBuffer temp = new StringBuffer();
		for (int i = 0; i < count; i++) {
			temp.append(c);
		}

		return temp.toString();
	}

	/**
	 *   as  
	 * 
	 * @param pojoName
	 *            java    
	 * @param packageName
	 *            flex vo     ,      javabean     
	 * @throws ClassNotFoundException
	 * @throws IOException
	 */
	public static void generateAsFile(String pojoName, String packageName,
			String folder, boolean bindable) throws ClassNotFoundException,
			IOException {
		Class<?> c = Class.forName(pojoName);
		Field[] fields = c.getDeclaredFields();

		// as vo        VO  
		File f = new File(folder + c.getSimpleName() + ".as");
		BufferedWriter bw = new BufferedWriter(new FileWriter(f));
		//   ,         java pojo     
		if (StringUtils.isEmpty(packageName)) {
			packageName = c.getPackage().getName();
		}
		bw.write("package " + packageName + "
{
"); // as vo java pojo if (bindable) { bw.write(repeat(" ", 4) + "[Bindable]
"); } bw.write(repeat(" ", 4) + "[RemoteClass(alias=\"" + c.getPackage().getName() + "." + c.getSimpleName() + "\")]
"); // bw.write(repeat(" ", 4) + "public class " + c.getSimpleName() + "
"); bw.write(repeat(" ", 4) + "{
"); // for (int i = 0; i < fields.length; i++) { Class<?> fieldType = fields[i].getType(); String typeName = getClassType(fieldType); bw.write(repeat(" ", 8) + "private var _" + fields[i].getName() + ":" + typeName + ";
"); } bw.write("


"); // bw.write(repeat(" ", 8) + "public function " + c.getSimpleName() + "(){}

"); // setter/getter for (int i = 0; i < fields.length; i++) { Class<?> fieldType = fields[i].getType(); String typeName = getClassType(fieldType); // setter bw.write(repeat(" ", 8) + "public function set " + fields[i].getName() + "(value:" + typeName + "):void{
"); bw.write(repeat(" ", 12) + "this._" + fields[i].getName() + " = value;
"); bw.write(repeat(" ", 8) + "}

"); // getter bw.write(repeat(" ", 8) + "public function get " + fields[i].getName() + "():" + typeName + "{
"); bw.write(repeat(" ", 12) + "return this._" + fields[i].getName() + ";
"); bw.write(repeat(" ", 8) + "}


"); } bw.write(repeat(" ", 4) + "}
"); bw.write("}"); bw.close(); } public static void main(String[] args) throws ClassNotFoundException, IOException { String pack = ""; String asPpack = ""; Set<Class<?>> clasess = null; //orm // pack = "com.then"; // asPpack = "chinat"; // clasess = getClasses(pack); // for (Class<?> clazz : clasess) { // JavaPojo2FlexVO.generateAsFile(clazz.getName(), asPpack, "f:/pojo2vo/vo/", // true); // } pack= "com.the4"; asPpack = "chitown.coo"; clasess = getClasses(pack); for (Class<?> clazz : clasess) { JavaPojo2FlexVO.generateAsFile(clazz.getName(), asPpack, "f:/pojo2vo/vo/", true); } } /** * package Class * * @param pack * @return */ public static Set<Class<?>> getClasses(String pack) { // class Set<Class<?>> classes = new LinkedHashSet<Class<?>>(); // boolean recursive = true; // String packageName = pack; String packageDirName = packageName.replace('.', '/'); // things Enumeration<URL> dirs; try { dirs = Thread.currentThread().getContextClassLoader() .getResources(packageDirName); // while (dirs.hasMoreElements()) { // URL url = dirs.nextElement(); // String protocol = url.getProtocol(); // if ("file".equals(protocol)) { System.err.println("file "); // String filePath = URLDecoder.decode(url.getFile(), "UTF-8"); // findAndAddClassesInPackageByFile(packageName, filePath, recursive, classes); } else if ("jar".equals(protocol)) { // jar // JarFile System.err.println("jar "); JarFile jar; try { // jar jar = ((JarURLConnection) url.openConnection()) .getJarFile(); // jar Enumeration<JarEntry> entries = jar.entries(); // while (entries.hasMoreElements()) { // jar jar META-INF JarEntry entry = entries.nextElement(); String name = entry.getName(); // / if (name.charAt(0) == '/') { // name = name.substring(1); } // if (name.startsWith(packageDirName)) { int idx = name.lastIndexOf('/'); // "/" if (idx != -1) { // "/" "." packageName = name.substring(0, idx) .replace('/', '.'); } // if ((idx != -1) || recursive) { // .class if (name.endsWith(".class") && !entry.isDirectory()) { // ".class" String className = name.substring( packageName.length() + 1, name.length() - 6); try { // classes System.out.println(className); classes.add(Class .forName(packageName + '.' + className)); } catch (ClassNotFoundException e) { // log // .error(" .class "); e.printStackTrace(); } } } } } } catch (IOException e) { // log.error(" jar "); e.printStackTrace(); } } } } catch (IOException e) { e.printStackTrace(); } return classes; } /** * Class * * @param packageName * @param packagePath * @param recursive * @param classes */ public static void findAndAddClassesInPackageByFile(String packageName, String packagePath, final boolean recursive, Set<Class<?>> classes) { // File File dir = new File(packagePath); // if (!dir.exists() || !dir.isDirectory()) { // log.warn(" " + packageName + " "); System.out.println(" " + packageName + " "); return; } // File[] dirfiles = dir.listFiles(new FileFilter() { // ( ) .class ( java ) public boolean accept(File file) { return (recursive && file.isDirectory()) || (file.getName().endsWith(".class")); } }); // for (File file : dirfiles) { // if (file.isDirectory()) { findAndAddClassesInPackageByFile( packageName + "." + file.getName(), file.getAbsolutePath(), recursive, classes); } else { // java .class String className = file.getName().substring(0, file.getName().length() - 6); try { // // classes.add(Class.forName(packageName + '.' + // className)); // , forName , static , classLoader load classes.add(Thread.currentThread().getContextClassLoader() .loadClass(packageName + '.' + className)); } catch (ClassNotFoundException e) { // log.error(" .class "); e.printStackTrace(); } } } } }

좋은 웹페이지 즐겨찾기