Java Pojo 회전 flex vo 도구 류
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();
}
}
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.