@Test public void build() { SyncFilter searcher = JavaMethod.build("searcher", new SyncerFilterMeta(), " public void filter(List<SyncData> list) {\n" + " for (SyncData d : list) {\n" + " assert d.getEventId().equals(\"123/1\");\n" + " }\n" + " }\n"); searcher.filter(Lists.newArrayList(data)); }
public static SyncFilter build(String consumerId, SyncerFilterMeta filterMeta, String method) { String source = "import com.github.zzt93.syncer.data.*;\n" + "import com.github.zzt93.syncer.data.util.*;\n" + "import java.util.*;\n" + "import java.math.BigDecimal;\n" + "import java.sql.Timestamp;\n" + "import org.slf4j.Logger;\n" + "import org.slf4j.LoggerFactory;\n" + "\n" + "public class MethodFilterTemplate implements SyncFilter<SyncData> {\n" + "\n" + " private final Logger logger = LoggerFactory.getLogger(getClass());\n" + "\n" + addNewline(method) + "\n" + "}\n"; String className = "Filter" + consumerId; source = source.replaceFirst("MethodFilterTemplate", className); Path path = Paths.get(filterMeta.getSrc(), className + ".java"); try { Files.write(path, source.getBytes(StandardCharsets.UTF_8)); } catch (IOException e) { logger.error("No permission", e); } compile(path.toString()); Class<?> cls; try { URLClassLoader classLoader = URLClassLoader.newInstance(new URL[]{path.getParent().toUri().toURL()}, JavaMethod.class.getClassLoader()); cls = Class.forName(className, true, classLoader); return (SyncFilter) cls.newInstance(); } catch (ClassNotFoundException | IllegalAccessException | InstantiationException | MalformedURLException e) { ShutDownCenter.initShutDown(e); return null; } }
JavaMethod { public static SyncFilter build(String consumerId, SyncerFilterMeta filterMeta, String method) { String source = "import com.github.zzt93.syncer.data.*;\n" + "import com.github.zzt93.syncer.data.util.*;\n" + "import java.util.*;\n" + "import java.math.BigDecimal;\n" + "import java.sql.Timestamp;\n" + "import org.slf4j.Logger;\n" + "import org.slf4j.LoggerFactory;\n" + "\n" + "public class MethodFilterTemplate implements SyncFilter<SyncData> {\n" + "\n" + " private final Logger logger = LoggerFactory.getLogger(getClass());\n" + "\n" + addNewline(method) + "\n" + "}\n"; String className = "Filter" + consumerId; source = source.replaceFirst("MethodFilterTemplate", className); Path path = Paths.get(filterMeta.getSrc(), className + ".java"); try { Files.write(path, source.getBytes(StandardCharsets.UTF_8)); } catch (IOException e) { logger.error("No permission", e); } compile(path.toString()); Class<?> cls; try { URLClassLoader classLoader = URLClassLoader.newInstance(new URL[]{path.getParent().toUri().toURL()}, JavaMethod.class.getClassLoader()); cls = Class.forName(className, true, classLoader); return (SyncFilter) cls.newInstance(); } catch (ClassNotFoundException | IllegalAccessException | InstantiationException | MalformedURLException e) { ShutDownCenter.initShutDown(e); return null; } } }
JavaMethod { public static SyncFilter build(String consumerId, SyncerFilterMeta filterMeta, String method) { String source = "import com.github.zzt93.syncer.data.*;\n" + "import com.github.zzt93.syncer.data.util.*;\n" + "import java.util.*;\n" + "import java.math.BigDecimal;\n" + "import java.sql.Timestamp;\n" + "import org.slf4j.Logger;\n" + "import org.slf4j.LoggerFactory;\n" + "\n" + "public class MethodFilterTemplate implements SyncFilter<SyncData> {\n" + "\n" + " private final Logger logger = LoggerFactory.getLogger(getClass());\n" + "\n" + addNewline(method) + "\n" + "}\n"; String className = "Filter" + consumerId; source = source.replaceFirst("MethodFilterTemplate", className); Path path = Paths.get(filterMeta.getSrc(), className + ".java"); try { Files.write(path, source.getBytes(StandardCharsets.UTF_8)); } catch (IOException e) { logger.error("No permission", e); } compile(path.toString()); Class<?> cls; try { URLClassLoader classLoader = URLClassLoader.newInstance(new URL[]{path.getParent().toUri().toURL()}, JavaMethod.class.getClassLoader()); cls = Class.forName(className, true, classLoader); return (SyncFilter) cls.newInstance(); } catch (ClassNotFoundException | IllegalAccessException | InstantiationException | MalformedURLException e) { ShutDownCenter.initShutDown(e); return null; } }  }
JavaMethod { public static SyncFilter build(String consumerId, SyncerFilterMeta filterMeta, String method) { String source = "import com.github.zzt93.syncer.data.*;\n" + "import com.github.zzt93.syncer.data.util.*;\n" + "import java.util.*;\n" + "import java.math.BigDecimal;\n" + "import java.sql.Timestamp;\n" + "import org.slf4j.Logger;\n" + "import org.slf4j.LoggerFactory;\n" + "\n" + "public class MethodFilterTemplate implements SyncFilter<SyncData> {\n" + "\n" + " private final Logger logger = LoggerFactory.getLogger(getClass());\n" + "\n" + addNewline(method) + "\n" + "}\n"; String className = "Filter" + consumerId; source = source.replaceFirst("MethodFilterTemplate", className); Path path = Paths.get(filterMeta.getSrc(), className + ".java"); try { Files.write(path, source.getBytes(StandardCharsets.UTF_8)); } catch (IOException e) { logger.error("No permission", e); } compile(path.toString()); Class<?> cls; try { URLClassLoader classLoader = URLClassLoader.newInstance(new URL[]{path.getParent().toUri().toURL()}, JavaMethod.class.getClassLoader()); cls = Class.forName(className, true, classLoader); return (SyncFilter) cls.newInstance(); } catch (ClassNotFoundException | IllegalAccessException | InstantiationException | MalformedURLException e) { ShutDownCenter.initShutDown(e); return null; } }  static SyncFilter build(String consumerId, SyncerFilterMeta filterMeta, String method); }
JavaMethod { public static SyncFilter build(String consumerId, SyncerFilterMeta filterMeta, String method) { String source = "import com.github.zzt93.syncer.data.*;\n" + "import com.github.zzt93.syncer.data.util.*;\n" + "import java.util.*;\n" + "import java.math.BigDecimal;\n" + "import java.sql.Timestamp;\n" + "import org.slf4j.Logger;\n" + "import org.slf4j.LoggerFactory;\n" + "\n" + "public class MethodFilterTemplate implements SyncFilter<SyncData> {\n" + "\n" + " private final Logger logger = LoggerFactory.getLogger(getClass());\n" + "\n" + addNewline(method) + "\n" + "}\n"; String className = "Filter" + consumerId; source = source.replaceFirst("MethodFilterTemplate", className); Path path = Paths.get(filterMeta.getSrc(), className + ".java"); try { Files.write(path, source.getBytes(StandardCharsets.UTF_8)); } catch (IOException e) { logger.error("No permission", e); } compile(path.toString()); Class<?> cls; try { URLClassLoader classLoader = URLClassLoader.newInstance(new URL[]{path.getParent().toUri().toURL()}, JavaMethod.class.getClassLoader()); cls = Class.forName(className, true, classLoader); return (SyncFilter) cls.newInstance(); } catch (ClassNotFoundException | IllegalAccessException | InstantiationException | MalformedURLException e) { ShutDownCenter.initShutDown(e); return null; } }  static SyncFilter build(String consumerId, SyncerFilterMeta filterMeta, String method);  }