1 Reply Latest reply: Jun 21, 2014 1:46 PM by Staffan Larsen-Oracle RSS

    How to set a Object value by JDI

    821bcfe9-ad4a-4829-8441-68643978a4df

      How to set a Object value using JDI?

      For example:

       

      class User{

        private String name;

        private String id;

      //

      set and get method;

      ...

      }

       

      public static void main(String[] args) throws Exception {

       

       

              VirtualMachineManager vmm = Bootstrap.virtualMachineManager();

              List<AttachingConnector> connectors = vmm.attachingConnectors();

              SocketAttachingConnector sac = null;

              for (AttachingConnector ac : connectors) {

                  if (ac instanceof SocketAttachingConnector) {

                      sac = (SocketAttachingConnector) ac;

                      break;

                  }

              }

       

       

              if (sac == null) {

                  System.out.println("JDI error");

                  return;

              }

              ///

              Map<String, Connector.Argument> arguments = sac.defaultArguments();

              Connector.Argument hostArg = arguments.get("hostname");

              Connector.Argument portArg = arguments.get("port");

       

       

              hostArg.setValue(HOST);

              portArg.setValue(String.valueOf(PORT));

       

       

              vmMachine = sac.attach(arguments);

       

       

              List<ReferenceType> classesByName = vmMachine.classesByName(CLSNAME);

              if (classesByName == null || classesByName.size() == 0) {

                  System.out.println("No class found");

                  return;

              }

       

       

              ReferenceType rt = classesByName.get(0);

              List<Method> methodsByName = rt.methodsByName(METHODNAME);

              if (methodsByName == null || methodsByName.size() == 0) {

                  System.out.println("No method found");

                  return;

              }

              Method method = methodsByName.get(0);

          . 

      ...

      //

      }//

       

      I will connect to server and monitor the remote server JVM , There is a object of User, I want to change its value by the Java Debugger Interface by client.

      so how to set its value by the client, and I can not got the User bean at client.

       

      I know the basic type filed value changed as follows:

       

      Value newValue = vmMachine.mirrorOf("change var value");// this field is a string.

      stackFrame.thisObject().setValue(field, newValue);

       

      But a Object , how can I change its value.

       

      Thanks Advanced.