|
| 1 | +package com.github.ltsopensource.core.compiler; |
| 2 | + |
| 3 | +import com.github.ltsopensource.core.commons.utils.ClassHelper; |
| 4 | +import javassist.*; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.regex.Matcher; |
| 11 | +import java.util.regex.Pattern; |
| 12 | + |
| 13 | +/** |
| 14 | + * @author william.liangf |
| 15 | + */ |
| 16 | +public class JavassistCompiler extends AbstractCompiler { |
| 17 | + |
| 18 | + private static final Pattern IMPORT_PATTERN = Pattern.compile("import\\s+([\\w\\.\\*]+);\n"); |
| 19 | + |
| 20 | + private static final Pattern EXTENDS_PATTERN = Pattern.compile("\\s+extends\\s+([\\w\\.]+)[^\\{]*\\{\n"); |
| 21 | + |
| 22 | + private static final Pattern IMPLEMENTS_PATTERN = Pattern.compile("\\s+implements\\s+([\\w\\.]+)\\s*\\{\n"); |
| 23 | + |
| 24 | + private static final Pattern METHODS_PATTERN = Pattern.compile("\n(private|public|protected)\\s+"); |
| 25 | + |
| 26 | + private static final Pattern FIELD_PATTERN = Pattern.compile("[^\n]+=[^\n]+;"); |
| 27 | + |
| 28 | + @Override |
| 29 | + public Class<?> doCompile(String name, String source) throws Throwable { |
| 30 | + int i = name.lastIndexOf('.'); |
| 31 | + String className = i < 0 ? name : name.substring(i + 1); |
| 32 | + ClassPool pool = new ClassPool(true); |
| 33 | + pool.appendClassPath(new LoaderClassPath(ClassHelper.getCallerClassLoader(getClass()))); |
| 34 | + Matcher matcher = IMPORT_PATTERN.matcher(source); |
| 35 | + List<String> importPackages = new ArrayList<String>(); |
| 36 | + Map<String, String> fullNames = new HashMap<String, String>(); |
| 37 | + while (matcher.find()) { |
| 38 | + String pkg = matcher.group(1); |
| 39 | + if (pkg.endsWith(".*")) { |
| 40 | + String pkgName = pkg.substring(0, pkg.length() - 2); |
| 41 | + pool.importPackage(pkgName); |
| 42 | + importPackages.add(pkgName); |
| 43 | + } else { |
| 44 | + int pi = pkg.lastIndexOf('.'); |
| 45 | + if (pi > 0) { |
| 46 | + String pkgName = pkg.substring(0, pi); |
| 47 | + pool.importPackage(pkgName); |
| 48 | + importPackages.add(pkgName); |
| 49 | + fullNames.put(pkg.substring(pi + 1), pkg); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + String[] packages = importPackages.toArray(new String[importPackages.size()]); |
| 54 | + matcher = EXTENDS_PATTERN.matcher(source); |
| 55 | + CtClass cls; |
| 56 | + if (matcher.find()) { |
| 57 | + String extend = matcher.group(1).trim(); |
| 58 | + String extendClass; |
| 59 | + if (extend.contains(".")) { |
| 60 | + extendClass = extend; |
| 61 | + } else if (fullNames.containsKey(extend)) { |
| 62 | + extendClass = fullNames.get(extend); |
| 63 | + } else { |
| 64 | + extendClass = ClassHelper.forName(packages, extend).getName(); |
| 65 | + } |
| 66 | + cls = pool.makeClass(name, pool.get(extendClass)); |
| 67 | + } else { |
| 68 | + cls = pool.makeClass(name); |
| 69 | + } |
| 70 | + matcher = IMPLEMENTS_PATTERN.matcher(source); |
| 71 | + if (matcher.find()) { |
| 72 | + String[] ifaces = matcher.group(1).trim().split("\\,"); |
| 73 | + for (String iface : ifaces) { |
| 74 | + iface = iface.trim(); |
| 75 | + String ifaceClass; |
| 76 | + if (iface.contains(".")) { |
| 77 | + ifaceClass = iface; |
| 78 | + } else if (fullNames.containsKey(iface)) { |
| 79 | + ifaceClass = fullNames.get(iface); |
| 80 | + } else { |
| 81 | + ifaceClass = ClassHelper.forName(packages, iface).getName(); |
| 82 | + } |
| 83 | + cls.addInterface(pool.get(ifaceClass)); |
| 84 | + } |
| 85 | + } |
| 86 | + String body = source.substring(source.indexOf("{") + 1, source.length() - 1); |
| 87 | + String[] methods = METHODS_PATTERN.split(body); |
| 88 | + for (String method : methods) { |
| 89 | + method = method.trim(); |
| 90 | + if (method.length() > 0) { |
| 91 | + if (method.startsWith(className)) { |
| 92 | + cls.addConstructor(CtNewConstructor.make("public " + method, cls)); |
| 93 | + } else if (FIELD_PATTERN.matcher(method).matches()) { |
| 94 | + cls.addField(CtField.make("private " + method, cls)); |
| 95 | + } else { |
| 96 | + cls.addMethod(CtNewMethod.make("public " + method, cls)); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + return cls.toClass(ClassHelper.getCallerClassLoader(getClass()), JavassistCompiler.class.getProtectionDomain()); |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments