jeudi 27 avril 2017

Runtime File Creation

I am trying to create the method named "doit" in runtime using "RuntimeCreation.java" class specified below and append it to my existing class in src/test/java and package com.abc.def.ghi.java(ghi.java is specified at the end of this . ghi.java is already an existing class with some existing methods already already present. I wanted to add this "doit" method to the end of the last method in ghi.class. Please help me to resolve this, i have been struggling very hard to find solution for hsi but couldn't get anything helpful

package com.abc.def.Runtime;

import java.io.*;
import java.lang.reflect.Method;
import java.util.Arrays;

import javax.tools.JavaCompiler;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import javax.tools.ToolProvider;

import org.testng.annotations.Test;

public class RuntimeCreation {

  public static void main(String[] args) throws Exception {
    // create the source
    File sourceFile   = new File("Temp/Hello.java");
    FileWriter writer = new FileWriter(sourceFile);

    writer.write(
            " public void doit() { \n" +
            "   System.out.println(\"Hello world\") ;\n" +
            " }"
    );

    writer.close();

    JavaCompiler compiler    = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager =
        compiler.getStandardFileManager(null, null, null);

    fileManager.setLocation(StandardLocation.CLASS_OUTPUT,
                            Arrays.asList(new File("/temp")));
    // Compile the file
    compiler.getTask(null,
               fileManager,
               null,
               null,
               null,
               fileManager.getJavaFileObjectsFromFiles(Arrays.asList(sourceFile)))
            .call();
    fileManager.close();


    // delete the source file
    // sourceFile.deleteOnExit();

    runIt();
  }

  @SuppressWarnings("unchecked")
  public static void runIt() {
    try {
      Class params[] = {};
      Object paramsObj[] = {};
      Class thisClass = Class.forName("Hello");
      Object iClass = thisClass.newInstance();
      Method thisMethod = thisClass.getDeclaredMethod("doit", params);
      thisMethod.invoke(iClass, paramsObj);
      }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}

ghi.java is specified below

package com.abc.def;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.util.EntityUtils;

public class ghi{

    private WebServiceHelper webServiceHelper = new WebServiceHelper();

    public final int ttyHeartBeatWebService(final String sUrl) {
        HttpResponse response = webServiceHelper.invokeGetRequest(sUrl,null);
        int sStatusCode = response.getStatusLine().getStatusCode();
        return sStatusCode;
    }

    public final int bbyMDSWebService(final String sUrl) {
        HttpResponse response = webServiceHelper.invokeGetRequest(sUrl,null);
        int sStatusCode = response.getStatusLine().getStatusCode();
        return sStatusCode;
    }
   ----> "doit" method should be added here 
}





Aucun commentaire:

Enregistrer un commentaire