Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.01 KB | None | 0 0
  1. package ru.compscicenter.java2014.implementor;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.lang.reflect.Method;
  6. import java.lang.reflect.Modifier;
  7. import java.lang.reflect.Parameter;
  8. import java.net.URL;
  9. import java.net.URLClassLoader;
  10. import java.nio.file.Files;
  11. import java.nio.file.Path;
  12. import java.nio.file.Paths;
  13. import java.util.HashSet;
  14. import java.util.Set;
  15.  
  16. /**
  17. * Created by Филипп on 24.11.2014.
  18. */
  19. public class SimpleImplementor implements Implementor {
  20. private ClassLoader classLoader = SimpleImplementor.class.getClassLoader();
  21. private Class inputClass;
  22. private String source;
  23. private String outputClassName;
  24. private String outputDirectory;
  25. private String inputClassName;
  26.  
  27. public SimpleImplementor(String outputDirectory) {
  28. this.outputDirectory = outputDirectory;
  29. }
  30.  
  31. private String generatePossibleReturnValue(Method method) {
  32. if (!method.getReturnType().isPrimitive()) {
  33. return "null";
  34. }
  35. String returnType = method.getReturnType().getCanonicalName();
  36. if (returnType.equals("boolean")) {
  37. return "false"; // ничто не истина, всё дозволено
  38. }
  39.  
  40. if (returnType.equals("double") || returnType.equals("float")) {
  41. return "0.0";
  42. }
  43. if (returnType.equals("void")) {
  44. return "";
  45. }
  46. return "0";
  47. }
  48.  
  49. private String generateSourceForParameters(Method method) {
  50. Parameter[] parameters = method.getParameters();
  51. String source = "";
  52.  
  53. for (Parameter parameter : parameters) {
  54. source += " " + parameter.getType().getCanonicalName() + " " + parameter.getName() + ",";
  55. }
  56. if (source.length() > 1) {
  57. source = source.substring(0, source.length() - 1);
  58. }
  59. return source;
  60. }
  61.  
  62. private String generateSourceForMethod(Method method) {
  63. String source = "";
  64. generateSourceForParameters(method);
  65. source += Modifier.toString(method.getModifiers()).replace("abstract", "") + " "
  66. + method.getReturnType().getCanonicalName() + " "
  67. + method.getName()
  68. + "( " + generateSourceForParameters(method) + ")"
  69. + " { return " + generatePossibleReturnValue(method) + "; } ";
  70. return source;
  71. }
  72. private void generateSourceForClass() {
  73. outputClassName = inputClass.getSimpleName()+ "Impl";
  74. source += "public class " + outputClassName + (inputClass.isInterface() ? " implements " : " extends ") + inputClassName + " { ";
  75. Method[] methods = inputClass.getMethods();
  76. Set<Method> hashSet= new HashSet<>();
  77.  
  78. Set<String> generatedMethods = new HashSet<>();
  79.  
  80. for (Class clazz = inputClass; clazz != null; clazz = clazz.getSuperclass()) {
  81. // System.out.println(clazz.getCanonicalName());
  82. for (Method method: clazz.getDeclaredMethods()) {
  83. if (Modifier.isAbstract(method.getModifiers())) {
  84. hashSet.add(method);
  85. generatedMethods.add(generateSourceForMethod(method));
  86. }
  87. }
  88. for (Class interfase: clazz.getInterfaces()) {
  89. for (Method method: interfase.getDeclaredMethods()) {
  90. hashSet.add(method);
  91. generatedMethods.add(generateSourceForMethod(method));
  92. }
  93. }
  94. }
  95.  
  96. // for (Method method: hashSet) {
  97. // source += generateSourceForMethod(method);
  98. // }
  99. for (String method: generatedMethods) {
  100. source += method;
  101. }
  102. source += " } ";
  103.  
  104.  
  105. }
  106.  
  107. @Override
  108. public String implementFromDirectory(String directoryPath, String className) throws ImplementorException {
  109. inputClassName = className;
  110. URL directoryPathh = null;
  111. File outputFile = new File(directoryPath);
  112. try {
  113. directoryPathh = outputFile.toURI().toURL();
  114. }
  115. catch (Exception e){
  116.  
  117. }
  118. ClassLoader classLoader = new URLClassLoader(new URL[] {directoryPathh});
  119. // System.out.println(Paths.get(directoryPath).toAbsolutePath().toString());
  120. Class clazz = null;
  121. try {
  122. clazz = classLoader.loadClass(className);
  123. }
  124. catch (Exception e) {
  125. e.printStackTrace();
  126. }
  127. // System.out.println(clazz.getCanonicalName());
  128. // System.out.println(clazz.getPackage().getName());
  129. try {
  130. source = "package " + clazz.getPackage().getName() + ";" + "\n";
  131. }
  132. catch (Exception e) {
  133. source = "";
  134. }
  135. inputClass = clazz;
  136. generateSourceForClass();
  137.  
  138. String pack = inputClassName.replace(inputClass.getSimpleName(), "");
  139. // System.out.println(inputClassName);
  140.  
  141. String currentDirectory = outputDirectory;
  142. Path currentPath = Paths.get(currentDirectory);
  143. while (pack.length() > 0) {
  144. int index = pack.indexOf('.');
  145. // System.out.println(pack.substring(0, index));
  146. currentDirectory += "/" + pack.substring(0, index);
  147.  
  148. try {
  149. Files.createDirectory(Paths.get(currentDirectory));
  150. // newFilePath = Files.createFile(newFilePath);
  151. }
  152. catch (Exception e) {
  153.  
  154. }
  155. pack = pack.substring(index + 1);
  156.  
  157. }
  158. // System.out.println(currentDirectory);
  159.  
  160. Path newFilePath = Paths.get(currentDirectory, outputClassName + ".java");
  161. try {
  162. newFilePath = Files.createFile(newFilePath);
  163. }
  164. catch (Exception e) {
  165.  
  166. }
  167.  
  168. try {
  169. Files.write(newFilePath, source.getBytes());
  170. } catch(IOException e) {
  171. throw new RuntimeException(e);
  172. }
  173. // System.out.println("out class name" + outputClassName);
  174. // return outputClassName;
  175. return className;
  176. }
  177.  
  178. @Override
  179. public String implementFromStandardLibrary(String className) throws ImplementorException {
  180. inputClassName = className;
  181. try {
  182. inputClass = classLoader.loadClass(className);
  183. }
  184. catch (ClassNotFoundException e) {
  185. throw new ImplementorException(e.getMessage());
  186. }
  187. source = "";
  188. generateSourceForClass();
  189.  
  190. Path newFilePath = Paths.get(outputDirectory , outputClassName + ".java");
  191.  
  192. try {
  193. // Files.createDirectory(Paths.get(outputDirectory));
  194. newFilePath = Files.createFile(newFilePath);
  195. }
  196. catch (Exception e) {
  197.  
  198. }
  199.  
  200. // System.out.println("File created " + newFilePath.toAbsolutePath().toString());
  201. try {
  202. Files.write(newFilePath, source.getBytes());
  203. } catch(IOException e) {
  204. throw new RuntimeException(e);
  205. }
  206.  
  207. return outputClassName;
  208. }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement