Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.compscicenter.java2014.implementor;
- import java.io.File;
- import java.io.IOException;
- import java.lang.reflect.Method;
- import java.lang.reflect.Modifier;
- import java.lang.reflect.Parameter;
- import java.net.URL;
- import java.net.URLClassLoader;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.util.HashSet;
- import java.util.Set;
- /**
- * Created by Филипп on 24.11.2014.
- */
- public class SimpleImplementor implements Implementor {
- private ClassLoader classLoader = SimpleImplementor.class.getClassLoader();
- private Class inputClass;
- private String source;
- private String outputClassName;
- private String outputDirectory;
- private String inputClassName;
- public SimpleImplementor(String outputDirectory) {
- this.outputDirectory = outputDirectory;
- }
- private String generatePossibleReturnValue(Method method) {
- if (!method.getReturnType().isPrimitive()) {
- return "null";
- }
- String returnType = method.getReturnType().getCanonicalName();
- if (returnType.equals("boolean")) {
- return "false"; // ничто не истина, всё дозволено
- }
- if (returnType.equals("double") || returnType.equals("float")) {
- return "0.0";
- }
- if (returnType.equals("void")) {
- return "";
- }
- return "0";
- }
- private String generateSourceForParameters(Method method) {
- Parameter[] parameters = method.getParameters();
- String source = "";
- for (Parameter parameter : parameters) {
- source += " " + parameter.getType().getCanonicalName() + " " + parameter.getName() + ",";
- }
- if (source.length() > 1) {
- source = source.substring(0, source.length() - 1);
- }
- return source;
- }
- private String generateSourceForMethod(Method method) {
- String source = "";
- generateSourceForParameters(method);
- source += Modifier.toString(method.getModifiers()).replace("abstract", "") + " "
- + method.getReturnType().getCanonicalName() + " "
- + method.getName()
- + "( " + generateSourceForParameters(method) + ")"
- + " { return " + generatePossibleReturnValue(method) + "; } ";
- return source;
- }
- private void generateSourceForClass() {
- outputClassName = inputClass.getSimpleName()+ "Impl";
- source += "public class " + outputClassName + (inputClass.isInterface() ? " implements " : " extends ") + inputClassName + " { ";
- Method[] methods = inputClass.getMethods();
- Set<Method> hashSet= new HashSet<>();
- Set<String> generatedMethods = new HashSet<>();
- for (Class clazz = inputClass; clazz != null; clazz = clazz.getSuperclass()) {
- // System.out.println(clazz.getCanonicalName());
- for (Method method: clazz.getDeclaredMethods()) {
- if (Modifier.isAbstract(method.getModifiers())) {
- hashSet.add(method);
- generatedMethods.add(generateSourceForMethod(method));
- }
- }
- for (Class interfase: clazz.getInterfaces()) {
- for (Method method: interfase.getDeclaredMethods()) {
- hashSet.add(method);
- generatedMethods.add(generateSourceForMethod(method));
- }
- }
- }
- // for (Method method: hashSet) {
- // source += generateSourceForMethod(method);
- // }
- for (String method: generatedMethods) {
- source += method;
- }
- source += " } ";
- }
- @Override
- public String implementFromDirectory(String directoryPath, String className) throws ImplementorException {
- inputClassName = className;
- URL directoryPathh = null;
- File outputFile = new File(directoryPath);
- try {
- directoryPathh = outputFile.toURI().toURL();
- }
- catch (Exception e){
- }
- ClassLoader classLoader = new URLClassLoader(new URL[] {directoryPathh});
- // System.out.println(Paths.get(directoryPath).toAbsolutePath().toString());
- Class clazz = null;
- try {
- clazz = classLoader.loadClass(className);
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- // System.out.println(clazz.getCanonicalName());
- // System.out.println(clazz.getPackage().getName());
- try {
- source = "package " + clazz.getPackage().getName() + ";" + "\n";
- }
- catch (Exception e) {
- source = "";
- }
- inputClass = clazz;
- generateSourceForClass();
- String pack = inputClassName.replace(inputClass.getSimpleName(), "");
- // System.out.println(inputClassName);
- String currentDirectory = outputDirectory;
- Path currentPath = Paths.get(currentDirectory);
- while (pack.length() > 0) {
- int index = pack.indexOf('.');
- // System.out.println(pack.substring(0, index));
- currentDirectory += "/" + pack.substring(0, index);
- try {
- Files.createDirectory(Paths.get(currentDirectory));
- // newFilePath = Files.createFile(newFilePath);
- }
- catch (Exception e) {
- }
- pack = pack.substring(index + 1);
- }
- // System.out.println(currentDirectory);
- Path newFilePath = Paths.get(currentDirectory, outputClassName + ".java");
- try {
- newFilePath = Files.createFile(newFilePath);
- }
- catch (Exception e) {
- }
- try {
- Files.write(newFilePath, source.getBytes());
- } catch(IOException e) {
- throw new RuntimeException(e);
- }
- // System.out.println("out class name" + outputClassName);
- // return outputClassName;
- return className;
- }
- @Override
- public String implementFromStandardLibrary(String className) throws ImplementorException {
- inputClassName = className;
- try {
- inputClass = classLoader.loadClass(className);
- }
- catch (ClassNotFoundException e) {
- throw new ImplementorException(e.getMessage());
- }
- source = "";
- generateSourceForClass();
- Path newFilePath = Paths.get(outputDirectory , outputClassName + ".java");
- try {
- // Files.createDirectory(Paths.get(outputDirectory));
- newFilePath = Files.createFile(newFilePath);
- }
- catch (Exception e) {
- }
- // System.out.println("File created " + newFilePath.toAbsolutePath().toString());
- try {
- Files.write(newFilePath, source.getBytes());
- } catch(IOException e) {
- throw new RuntimeException(e);
- }
- return outputClassName;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement