How to use provider method of com.sun.tools.attach.VirtualMachine class

Best Powermock code snippet using com.sun.tools.attach.VirtualMachine.provider

Source:VirtualMachine.java Github

copy

Full Screen

...98 * @since 1.699 */100public abstract class VirtualMachine101{102 private final AttachProvider provider;103 private final String id;104 private volatile int hash; // 0 => not computed105 /**106 * Initializes a new instance of this class.107 *108 * @param provider The attach provider creating this class.109 * @param id The abstract identifier that identifies the Java virtual machine.110 */111 protected VirtualMachine(AttachProvider provider, String id)112 {113 this.provider = provider;114 this.id = id;115 }116 /**117 * Return a list of Java virtual machines.118 * <p/>119 * This method returns a list of Java {@link com.sun.tools.attach.VirtualMachineDescriptor}120 * elements. The list is an aggregation of the virtual machine descriptor lists obtained by121 * invoking the {@link com.sun.tools.attach.spi.AttachProvider#listVirtualMachines122 * listVirtualMachines} method of all installed {@link com.sun.tools.attach.spi.AttachProvider123 * attach providers}. If there are no Java virtual machines known to any provider then an empty124 * list is returned.125 *126 * @return The list of virtual machine descriptors.127 */128 public static List<VirtualMachineDescriptor> list()129 {130 List<VirtualMachineDescriptor> l = new ArrayList<VirtualMachineDescriptor>();131 List<AttachProvider> providers = AttachProvider.providers();132 for (AttachProvider provider : providers) {133 l.addAll(provider.listVirtualMachines());134 }135 return l;136 }137 /**138 * Attaches to a Java virtual machine.139 * <p/>140 * This method obtains the list of attach providers by invoking the141 * {@link com.sun.tools.attach.spi.AttachProvider#providers() AttachProvider.providers()} method.142 * It then iterates overs the list and invokes each provider's {@link143 * com.sun.tools.attach.spi.AttachProvider#attachVirtualMachine(java.lang.String)144 * attachVirtualMachine} method in turn. If a provider successfully145 * attaches then the iteration terminates, and the VirtualMachine created146 * by the provider that successfully attached is returned by this method.147 * If the <code>attachVirtualMachine</code> method of all providers throws148 * {@link com.sun.tools.attach.AttachNotSupportedException AttachNotSupportedException}149 * then this method also throws <code>AttachNotSupportedException</code>.150 * This means that <code>AttachNotSupportedException</code> is thrown when151 * the identifier provided to this method is invalid, or the identifier152 * corresponds to a Java virtual machine that does not exist, or none153 * of the providers can attach to it. This exception is also thrown if154 * {@link com.sun.tools.attach.spi.AttachProvider#providers()155 * AttachProvider.providers()} returns an empty list. </p>156 *157 * @param id The abstract identifier that identifies the Java virtual machine.158 * @return A VirtualMachine representing the target VM.159 * @throws SecurityException If a security manager has been installed and it denies160 * {@link com.sun.tools.attach.AttachPermission AttachPermission}161 * <tt>("attachVirtualMachine")</tt>, or another permission162 * required by the implementation.163 * @throws IOException If an I/O error occurs164 */165 public static VirtualMachine attach(String id) throws AttachNotSupportedException, IOException166 {167 List<AttachProvider> providers = AttachProvider.providers();168 AttachNotSupportedException lastExc = null;169 for (AttachProvider provider : providers) {170 try {171 return provider.attachVirtualMachine(id);172 }173 catch (AttachNotSupportedException x) {174 lastExc = x;175 }176 }177 throw lastExc;178 }179 /**180 * Attaches to a Java virtual machine.181 * <p/>182 * This method first invokes the {@link com.sun.tools.attach.VirtualMachineDescriptor#provider()183 * provider()} method of the given virtual machine descriptor to obtain the attach provider.184 * It then invokes the attach provider's {@link185 * com.sun.tools.attach.spi.AttachProvider#attachVirtualMachine(VirtualMachineDescriptor)186 * attachVirtualMachine} to attach to the target VM.187 *188 * @param vmd The virtual machine descriptor.189 *190 * @return A VirtualMachine representing the target VM.191 *192 * @throws SecurityException193 * If a security manager has been installed and it denies194 * {@link com.sun.tools.attach.AttachPermission AttachPermission}195 * <tt>("attachVirtualMachine")</tt>, or another permission196 * required by the implementation.197 *198 * @throws AttachNotSupportedException199 * If the attach provider's <code>attachVirtualmachine</code>200 * throws <code>AttachNotSupportedException</code>.201 *202 * @throws IOException If an I/O error occurs203 *204 * @throws NullPointerException If <code>vmd</code> is <code>null</code>.205 */206 public static VirtualMachine attach(VirtualMachineDescriptor vmd)207 throws AttachNotSupportedException, IOException208 {209 return vmd.provider().attachVirtualMachine(vmd);210 }211 /**212 * Detach from the virtual machine.213 * <p/>214 * After detaching from the virtual machine, any further attempt to invoke215 * operations on that virtual machine will cause an {@link java.io.IOException216 * IOException} to be thrown. If an operation (such as {@link #loadAgent217 * loadAgent} for example) is in progress when this method is invoked then218 * the behaviour is implementation dependent. In other words, it is219 * implementation specific if the operation completes or throws <tt>IOException</tt>.220 * <p/>221 * If already detached from the virtual machine then invoking this method has no effect.222 *223 * @throws IOException If an I/O error occurs224 */225 public abstract void detach() throws IOException;226 /**227 * Returns the provider that created this virtual machine.228 */229 public final AttachProvider provider()230 {231 return provider;232 }233 /**234 * Returns the identifier for this Java virtual machine.235 */236 public final String id()237 {238 return id;239 }240 /**241 * Loads an agent library.242 * <p/>243 * A <a href="../../../../../../../../technotes/guides/jvmti/index.html">JVM244 * TI</a> client is called an <i>agent</i>. It is developed in a native language.245 * A JVM TI agent is deployed in a platform specific manner but it is typically the246 * platform equivalent of a dynamic library. This method causes the given agent247 * library to be loaded into the target VM (if not already loaded).248 * It then causes the target VM to invoke the <code>Agent_OnAttach</code> function249 * as specified in the250 * <a href="../../../../../../../../technotes/guides/jvmti/index.html"> JVM Tools251 * Interface</a> specification. Note that the <code>Agent_OnAttach</code>252 * function is invoked even if the agent library was loaded prior to invoking253 * this method.254 * <p/>255 * The agent library provided is the name of the agent library. It is interpreted256 * in the target virtual machine in an implementation-dependent manner. Typically an257 * implementation will expand the library name into an operating system specific file258 * name. For example, on UNIX systems, the name <tt>foo</tt> might be expanded to259 * <tt>libfoo.so</tt>, and located using the search path specified by the260 * <tt>LD_LIBRARY_PATH</tt> environment variable.261 * <p/>262 * If the <code>Agent_OnAttach</code> function in the agent library returns263 * an error then an {@link com.sun.tools.attach.AgentInitializationException} is264 * thrown. The return value from the <code>Agent_OnAttach</code> can then be265 * obtained by invoking the {@link266 * com.sun.tools.attach.AgentInitializationException#returnValue() returnValue}267 * method on the exception.268 *269 * @param agentLibrary The name of the agent library.270 * @param options The options to provide to the <code>Agent_OnAttach</code>271 * function (can be <code>null</code>).272 * @throws AgentLoadException If the agent library does not exist, or cannot be loaded for273 * another reason.274 * @throws AgentInitializationException If the <code>Agent_OnAttach</code> function returns an error275 * @throws IOException If an I/O error occurs276 * @throws NullPointerException If <code>agentLibrary</code> is <code>null</code>.277 * @see com.sun.tools.attach.AgentInitializationException#returnValue()278 */279 public abstract void loadAgentLibrary(String agentLibrary, String options)280 throws AgentLoadException, AgentInitializationException, IOException;281 /**282 * Loads an agent library.283 * <p/>284 * This convenience method works as if by invoking:285 * <p/>286 * <blockquote><tt>287 * {@link #loadAgentLibrary(String, String) loadAgentLibrary}(agentLibrary,&nbsp;null);288 * </tt></blockquote>289 *290 * @param agentLibrary The name of the agent library.291 * @throws AgentLoadException If the agent library does not exist, or cannot be loaded for292 * another reason.293 * @throws AgentInitializationException If the <code>Agent_OnAttach</code> function returns an error294 * @throws IOException If an I/O error occurs295 * @throws NullPointerException If <code>agentLibrary</code> is <code>null</code>.296 */297 public void loadAgentLibrary(String agentLibrary)298 throws AgentLoadException, AgentInitializationException, IOException299 {300 loadAgentLibrary(agentLibrary, null);301 }302 /**303 * Load a native agent library by full pathname.304 * <p/>305 * A <a href="../../../../../../../../technotes/guides/jvmti/index.html">JVM TI</a> client is306 * called an <i>agent</i>. It is developed in a native language.307 * A JVM TI agent is deployed in a platform specific manner but it is typically the308 * platform equivalent of a dynamic library. This method causes the given agent309 * library to be loaded into the target VM (if not already loaded).310 * It then causes the target VM to invoke the <code>Agent_OnAttach</code> function311 * as specified in the312 * <a href="../../../../../../../../technotes/guides/jvmti/index.html"> JVM Tools313 * Interface</a> specification. Note that the <code>Agent_OnAttach</code>314 * function is invoked even if the agent library was loaded prior to invoking315 * this method.316 * <p/>317 * The agent library provided is the absolute path from which to load the318 * agent library. Unlike {@link #loadAgentLibrary loadAgentLibrary}, the library name319 * is not expanded in the target virtual machine.320 * <p/>321 * If the <code>Agent_OnAttach</code> function in the agent library returns322 * an error then an {@link com.sun.tools.attach.AgentInitializationException} is323 * thrown. The return value from the <code>Agent_OnAttach</code> can then be324 * obtained by invoking the {@link325 * com.sun.tools.attach.AgentInitializationException#returnValue() returnValue}326 * method on the exception.327 *328 * @param agentPath The full path of the agent library.329 * @param options The options to provide to the <code>Agent_OnAttach</code>330 * function (can be <code>null</code>).331 * @throws AgentLoadException If the agent library does not exist, or cannot be loaded for332 * another reason.333 * @throws AgentInitializationException If the <code>Agent_OnAttach</code> function returns an error334 * @throws IOException If an I/O error occurs335 * @throws NullPointerException If <code>agentPath</code> is <code>null</code>.336 * @see com.sun.tools.attach.AgentInitializationException#returnValue()337 */338 public abstract void loadAgentPath(String agentPath, String options)339 throws AgentLoadException, AgentInitializationException, IOException;340 /**341 * Load a native agent library by full pathname.342 * <p/>343 * This convenience method works as if by invoking:344 * <p/>345 * <blockquote><tt>346 * {@link #loadAgentPath(String, String) loadAgentPath}(agentLibrary,&nbsp;null);347 * </tt></blockquote>348 *349 * @param agentPath The full path to the agent library.350 * @throws AgentLoadException If the agent library does not exist, or cannot be loaded for351 * another reason.352 * @throws AgentInitializationException If the <code>Agent_OnAttach</code> function returns an error353 * @throws IOException If an I/O error occurs354 * @throws NullPointerException If <code>agentPath</code> is <code>null</code>.355 */356 public void loadAgentPath(String agentPath)357 throws AgentLoadException, AgentInitializationException, IOException358 {359 loadAgentPath(agentPath, null);360 }361 /**362 * Loads an agent.363 * <p/>364 * <p> The agent provided to this method is a path name to a JAR file on the file365 * system of the target virtual machine. This path is passed to the target virtual366 * machine where it is interpreted. The target virtual machine attempts to start367 * the agent as specified by the {@link java.lang.instrument} specification.368 * That is, the specified JAR file is added to the system class path (of the target369 * virtual machine), and the <code>agentmain</code> method of the agent class, specified370 * by the <code>Agent-Class</code> attribute in the JAR manifest, is invoked. This371 * method completes when the <code>agentmain</code> method completes.372 *373 * @param agent Path to the JAR file containing the agent.374 * @param options The options to provide to the agent's <code>agentmain</code>375 * method (can be <code>null</code>).376 * @throws AgentLoadException If the agent does not exist, or cannot be started in the manner377 * specified in the {@link java.lang.instrument} specification.378 * @throws AgentInitializationException If the <code>agentmain</code> throws an exception379 * @throws IOException If an I/O error occurs380 * @throws NullPointerException If <code>agent</code> is <code>null</code>.381 */382 public abstract void loadAgent(String agent, String options)383 throws AgentLoadException, AgentInitializationException, IOException;384 /**385 * Loads an agent.386 * <p/>387 * This convenience method works as if by invoking:388 * <p/>389 * <blockquote><tt>390 * {@link #loadAgent(String, String) loadAgent}(agent,&nbsp;null);391 * </tt></blockquote>392 *393 * @param agent Path to the JAR file containing the agent.394 * @throws AgentLoadException If the agent does not exist, or cannot be started in the manner395 * specified in the {@link java.lang.instrument} specification.396 * @throws AgentInitializationException If the <code>agentmain</code> throws an exception397 * @throws IOException If an I/O error occurs398 * @throws NullPointerException If <code>agent</code> is <code>null</code>.399 */400 public void loadAgent(String agent)401 throws AgentLoadException, AgentInitializationException, IOException402 {403 loadAgent(agent, null);404 }405 /**406 * Returns the current system properties in the target virtual machine.407 * <p/>408 * This method returns the system properties in the target virtual409 * machine. Properties whose key or value is not a <tt>String</tt> are410 * omitted. The method is approximately equivalent to the invocation of the411 * method {@link java.lang.System#getProperties System.getProperties}412 * in the target virtual machine except that properties with a key or413 * value that is not a <tt>String</tt> are not included.414 * <p/>415 * This method is typically used to decide which agent to load into416 * the target virtual machine with {@link #loadAgent loadAgent}, or417 * {@link #loadAgentLibrary loadAgentLibrary}. For example, the418 * <code>java.home</code> or <code>user.dir</code> properties might be419 * use to create the path to the agent library or JAR file.420 *421 * @return The system properties422 * @throws IOException If an I/O error occurs423 * @see java.lang.System#getProperties424 * @see #loadAgentLibrary425 * @see #loadAgent426 */427 public abstract Properties getSystemProperties() throws IOException;428 /**429 * Returns the current <i>agent properties</i> in the target virtual430 * machine.431 * <p/>432 * The target virtual machine can maintain a list of properties on433 * behalf of agents. The manner in which this is done, the names of the434 * properties, and the types of values that are allowed, is implementation435 * specific. Agent properties are typically used to store communication436 * end-points and other agent configuration details. For example, a debugger437 * agent might create an agent property for its transport address.438 * <p/>439 * This method returns the agent properties whose key and value is a440 * <tt>String</tt>. Properties whose key or value is not a <tt>String</tt>441 * are omitted. If there are no agent properties maintained in the target442 * virtual machine then an empty property list is returned.443 *444 * @return The agent properties445 * @throws IOException If an I/O error occurs446 */447 public abstract Properties getAgentProperties() throws IOException;448 /**449 * Returns a hash-code value for this VirtualMachine. The hash450 * code is based upon the VirtualMachine's components, and satisfies451 * the general contract of the Object.hashCode method.452 *453 * @return A hash-code value for this virtual machine454 */455 public int hashCode()456 {457 if (hash != 0) {458 return hash;459 }460 hash = provider.hashCode() * 127 + id.hashCode();461 return hash;462 }463 /**464 * Tests this VirtualMachine for equality with another object.465 * <p/>466 * <p> If the given object is not a VirtualMachine then this467 * method returns <tt>false</tt>. For two VirtualMachines to468 * be considered equal requires that they both reference the same469 * provider, and their {@link VirtualMachineDescriptor#id() identifiers} are equal. </p>470 * <p/>471 * <p> This method satisfies the general contract of the {@link472 * java.lang.Object#equals(Object) Object.equals} method. </p>473 *474 * @param ob The object to which this object is to be compared475 * @return <tt>true</tt> if, and only if, the given object is476 * a VirtualMachine that is equal to this477 * VirtualMachine.478 */479 public boolean equals(Object ob)480 {481 if (ob == this) {482 return true;483 }484 if (!(ob instanceof VirtualMachine)) {485 return false;486 }487 VirtualMachine other = (VirtualMachine) ob;488 return other.provider() == provider() && other.id().equals(id());489 }490 /**491 * Returns the string representation of the <code>VirtualMachine</code>.492 */493 public String toString()494 {495 return provider.toString() + ": " + id;496 }497}...

Full Screen

Full Screen

provider

Using AI Code Generation

copy

Full Screen

1import com.sun.tools.attach.VirtualMachine;2import com.sun.tools.attach.VirtualMachineDescriptor;3import java.io.IOException;4import java.util.List;5public class AttachTest {6 public static void main(String[] args) throws IOException {7 List<VirtualMachineDescriptor> vms = VirtualMachine.list();8 for (VirtualMachineDescriptor vmd : vms) {9 System.out.println(vmd.displayName());10 System.out.println(vmd.id());11 System.out.println(vmd.provider());12 }13 VirtualMachine vm = VirtualMachine.attach("15108");14 System.out.println(vm.getSystemProperties());15 System.out.println(vm.getAgentProperties());16 System.out.println(vm.getSystemProperties());17 System.out.println(vm.getAgentProperties());18 vm.detach();19 }20}

Full Screen

Full Screen

provider

Using AI Code Generation

copy

Full Screen

1import com.sun.tools.attach.VirtualMachine;2public class AttachTest {3 public static void main(String[] args) throws Exception {4 String pid = "12345";5 String agent = "C:\\path\\to\\agent.jar";6 VirtualMachine vm = VirtualMachine.attach(pid);7 vm.loadAgent(agent);8 vm.detach();9 }10}11import com.sun.tools.attach.VirtualMachine;12import com.sun.tools.attach.VirtualMachineDescriptor;13public class AttachTest {14 public static void main(String[] args) throws Exception {15 String agent = "C:\\path\\to\\agent.jar";16 for (VirtualMachineDescriptor vmd : VirtualMachine.list()) {17 if (vmd.displayName().startsWith("MyApp")) {18 VirtualMachine vm = VirtualMachine.attach(vmd);19 vm.loadAgent(agent);20 vm.detach();21 break;22 }23 }24 }25}26import com.sun.tools.attach.VirtualMachine;27import com.sun.tools.attach.VirtualMachineDescriptor;28public class AttachTest {29 public static void main(String[] args) throws Exception {30 String agent = "C:\\path\\to\\agent.jar";31 for (VirtualMachineDescriptor vmd : VirtualMachine.list()) {32 if (vmd.displayName().startsWith("MyApp")) {33 VirtualMachine vm = VirtualMachine.attach(vmd.id());34 vm.loadAgent(agent);35 vm.detach();36 break;37 }38 }39 }40}41import com.sun.tools.attach.VirtualMachine;42import com.sun.tools.attach.VirtualMachineDescriptor;43public class AttachTest {44 public static void main(String[] args) throws Exception {45 String agent = "C:\\path\\to\\agent.jar";46 for (VirtualMachineDescriptor vmd : VirtualMachine.list()) {47 if (vmd.displayName().startsWith("MyApp")) {48 VirtualMachine vm = VirtualMachine.attach(vmd.id());49 for (String agentPath : vm.getSystemProperties().get("java.class.path").split(";")) {50 if (agentPath.endsWith("agent.jar")) {51 vm.loadAgent(agentPath);52 break;53 }54 }55 vm.detach();56 break;57 }58 }59 }60}

Full Screen

Full Screen

provider

Using AI Code Generation

copy

Full Screen

1import java.io.IOException;2import java.util.List;3import java.util.ArrayList;4import java.util.Arrays;5import com.sun.tools.attach.AttachNotSupportedException;6import com.sun.tools.attach.VirtualMachine;7import com.sun.tools.attach.VirtualMachineDescriptor;8public class JvmClassLister {9 public static void main(String[] args) throws IOException, AttachNotSupportedException {10 List<String> classList = new ArrayList<String>();11 VirtualMachineDescriptor vmd = null;12 VirtualMachine vm = null;13 String pid = null;14 String[] classListArray = null;15 if (args.length == 0) {16 pid = getPid();17 } else {18 pid = args[0];19 }20 if (pid == null) {21 System.out.println("No pid found");22 System.exit(1);23 }24 vmd = VirtualMachineDescriptor.findByPid(Integer.parseInt(pid));25 if (vmd == null) {26 System.out.println("No vm found for pid " + pid);27 System.exit(1);28 }29 vm = VirtualMachine.attach(vmd);30 classListArray = vm.getSystemProperties().getProperty("java.class.path").split(":");31 vm.detach();32 classList = Arrays.asList(classListArray);33 System.out.println(classList);34 }35 public static String getPid() {36 String pid = null;37 String vmName = ManagementFactory.getRuntimeMXBean().getName();38 if (vmName != null) {39 pid = vmName.split("@")[0];40 }41 return pid;42 }43}44[java] [C:\Users\user\Documents\java\java\jvm\jvmclasslister\bin] java -cp "C:\Program Files\Java\jdk1.8.0_121\lib\tools.jar;." jvmclasslister.JvmClassLister

Full Screen

Full Screen

provider

Using AI Code Generation

copy

Full Screen

1import java.io.IOException;2import java.util.List;3import com.sun.tools.attach.*;4public class AttachProviderTest {5 public static void main(String[] args) throws IOException, AttachNotSupportedException {6 List<VirtualMachineDescriptor> list = VirtualMachine.list();7 String pid = list.get(0).id();8 System.out.println("pid: " + pid);9 VirtualMachine vm = VirtualMachine.attach(pid);10 System.out.println("vm: " + vm);11 }12}13import java.io.IOException;14import java.util.List;15import com.sun.tools.attach.*;16public class AttachTest {17 public static void main(String[] args) throws IOException, AttachNotSupportedException {18 List<VirtualMachineDescriptor> list = VirtualMachine.list();19 String pid = list.get(0).id();20 System.out.println("pid: " + pid);21 VirtualMachine vm = VirtualMachine.attach(pid);22 System.out.println("vm: " + vm);23 vm.detach();24 }25}26import java.io.IOException;27import java.util.List;28import com.sun.tools.attach.*;29public class AttachTest {30 public static void main(String[] args) throws IOException, AttachNotSupportedException {31 List<VirtualMachineDescriptor> list = VirtualMachine.list();32 String pid = list.get(0).id();33 System.out.println("pid: " + pid);34 VirtualMachine vm = VirtualMachine.attach(pid);35 System.out.println("vm: " + vm);36 vm.detach();37 }38}39import java.io.IOException;40import java.util.List;41import com.sun.tools.attach.*;42public class AttachTest {43 public static void main(String[] args) throws IOException, AttachNotSupportedException {44 List<VirtualMachineDescriptor> list = VirtualMachine.list();

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Powermock automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful