Guest User

Untitled

a guest
May 26th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. public <T> T getInstance(Class<T> clazz, String code,String packageName)
  2. throws IOException, InstantiationException, IllegalAccessException, NameNotFoundException {
  3. final String name = getClassName(code);
  4. final Path wrappedCodede = wrapCode(packageName, name, code);
  5. final Path classFile = compile(wrappedCodede);
  6. final ClassLoaderFromTemp loader = new ClassLoaderFromTemp();
  7. final Class<?> p1Class = loader.findClassInTemp(classFile);
  8. final Object instace = p1Class.newInstance();
  9.  
  10. cleanupTmp();
  11.  
  12. return clazz.cast(instace);
  13. }
  14.  
  15. public static void main(String[] args) {
  16. try {
  17. AgentBuilder.addToClassPath("src");
  18. AgentBuilder ab = new AgentBuilder();
  19. ab.getInstance(Ship.class, CODE, "Teste");
  20. } catch (Exception e) {
  21. System.out.println(e.getMessage());
  22. }
  23. }
  24.  
  25. void checkCode() throws SoncException {
  26. try {
  27. AgentBuilder agentBuilder = new AgentBuilder();
  28. agentBuilder.getInstance(Ship.class, this.code, this.nick);
  29. } catch (IOException | InstantiationException | IllegalAccessException | NameNotFoundException
  30. | NullPointerException | ClassCastException e) {
  31. throw new SoncException("An error occurred on checking player's code.", e);
  32. }
  33. }
Add Comment
Please, Sign In to add comment