jshrink/jshrink-lib/src/main/java/edu/ucla/cs/jshrinklib/reachability/TamiFlexRunner.java
/**
	 * 
	 * Inject TamiFlex as a java agent in the test plugin in the given POM file
	 * 
	 * @param path
	 */
Inject TamiFlex as a java agent in the test plugin in the given POM file

@param path
Inject TamiFlex as a java agent in the test plugin in the given POM file @param path
public void injectTamiFlex(String path) {
		File pom_file = new File(path);
		try {
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(pom_file);
			doc.getDocumentElement().normalize();
			XPathFactory xPathfactory = XPathFactory.newInstance();
			XPath xpath = xPathfactory.newXPath();
			XPathExpression expr = xpath.compile("/project/build/plugins/plugin/artifactId[text()=\"maven-surefire-plugin\"]");
            NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
            if(nodes.getLength() == 0) {
            	Node plugin_node = doc.createElement("plugin");
            	Node groupId_node = doc.createElement("groupId");
            	groupId_node.setTextContent("org.apache.maven.plugins");
            	Node artifactId_node = doc.createElement("artifactId");
            	artifactId_node.setTextContent("maven-surefire-plugin");
            	Node version_node = doc.createElement("version");
            	version_node.setTextContent("2.20.1");
            	Node config_node = doc.createElement("configuration");
        		Node arg_node = doc.createElement("argLine");
        		arg_node.setTextContent("-javaagent:" + tamiflex_path);
        		config_node.appendChild(arg_node);
        		plugin_node.appendChild(groupId_node);
        		plugin_node.appendChild(artifactId_node);
        		plugin_node.appendChild(version_node);
        		plugin_node.appendChild(config_node);
        		XPathExpression expr2 = xpath.compile("/project/build/plugins");
        		NodeList nodes2 = (NodeList) expr2.evaluate(doc, XPathConstants.NODESET);
        		if(nodes2.getLength() == 0) {
        			Node plugins_node = doc.createElement("plugins");
        			plugins_node.appendChild(plugin_node);
        			XPathExpression expr3 = xpath.compile("/project/build");
        			NodeList nodes3 = (NodeList) expr3.evaluate(doc, XPathConstants.NODESET);
        			if(nodes3.getLength() == 0) {
        				Node build_node = doc.createElement("build");
        				build_node.appendChild(plugins_node);
        				XPathExpression expr4 = xpath.compile("/project");
        				NodeList nodes4 = (NodeList) expr4.evaluate(doc, XPathConstants.NODESET);
        				if(nodes4.getLength() == 1) {
        					Node project_node = nodes4.item(0);
        					project_node.appendChild(build_node);
        				} else {
        					System.out.println("[TamiFlexRunner] There are zero or multiple project nodes in the POM file. "
        							+ "Please double check if it is correct.");
        				}
        			} else if (nodes3.getLength() == 1){
        				Node build_node = nodes3.item(0);
        				build_node.appendChild(plugins_node);
        			} else {
        				System.out.println("[TamiFlexRunner] There are multiple build nodes within the project node in the POM file. "
    							+ "Please double check if it is correct.");
        			}
        		} else if (nodes2.getLength() == 1) {
        			Node plugins_node = nodes2.item(0);
        			plugins_node.appendChild(plugin_node);
        		} else {
        			System.out.println("[TamiFlexRunner] There are multiple plugins nodes within the build node in the POM file. "
							+ "Please double check if the POM file is correct.");
        		}
            } else if (nodes.getLength() == 1) {
            	Node plugin_node = nodes.item(0).getParentNode();
            	NodeList children = plugin_node.getChildNodes();
            	Node config_node = null;
            	for(int i = 0; i < children.getLength(); i++) {
            		Node child = children.item(i);
            		if(child.getNodeName().equals("configuration")) {
            			config_node = child;
            			break;
            		}
            	}
            	if(config_node != null) {
            		NodeList config_children = config_node.getChildNodes();
        			Node arg_node = null;
        			for(int j = 0; j < config_children.getLength(); j++) {
        				Node config_child = config_children.item(j);
        				if(config_child.getNodeName().equals("argLine")) {
        					arg_node = config_child;
        					break;
        				}
        			}
        			if(arg_node == null) {
        				arg_node = doc.createElement("argLine");
        				arg_node.setTextContent("-javaagent:" + tamiflex_path);
        				config_node.appendChild(arg_node);
        			} else {
        				String arg_option = arg_node.getTextContent();
        				if(!arg_option.contains(tamiflex_path)) {
        					if(arg_option.isEmpty()) {
        						arg_node.setTextContent("-javaagent:" + tamiflex_path);
        					} else {
        						arg_option += " -javaagent:" + tamiflex_path;
        						arg_node.setTextContent(arg_option);
        					}
        				}
        			}
            	} else {
            		config_node = doc.createElement("configuration");
            		Element arg_node = doc.createElement("argLine");
            		arg_node.setTextContent("-javaagent:" + tamiflex_path);
            		config_node.appendChild(arg_node);
            		plugin_node.appendChild(config_node);
            	}
            } else {
            	System.out.println("[TamiFlexRunner] There are more than one sunfire plugin in the POM file. "
            			+ "Please double check if it is correct.");
            }
    		TransformerFactory transformerFactory = TransformerFactory.newInstance();
    		Transformer transformer = transformerFactory.newTransformer();
    		DOMSource source = new DOMSource(doc);
    		StreamResult result = new StreamResult(pom_file);
    		transformer.transform(source, result);
		} catch (SAXException | ParserConfigurationException 
				| IOException | XPathExpressionException | TransformerException e) {
			e.printStackTrace();
		}
	}
public void injectTamiFlex ( String path ) { File pom_file = new File ( path ) ; try { DocumentBuilderFactory dbFactory = DocumentBuilderFactory . newInstance ( ) ; DocumentBuilder dBuilder = dbFactory . newDocumentBuilder ( ) ; Document doc = dBuilder . parse ( pom_file ) ; doc . getDocumentElement ( ) . normalize ( ) ; XPathFactory xPathfactory = XPathFactory . newInstance ( ) ; XPath xpath = xPathfactory . newXPath ( ) ; XPathExpression expr = xpath . compile ( "/project/build/plugins/plugin/artifactId[text()=\"maven-surefire-plugin\"]" ) ; NodeList nodes = ( NodeList ) expr . evaluate ( doc , XPathConstants . NODESET ) ; if ( nodes . getLength ( ) == 0 ) { Node plugin_node = doc . createElement ( "plugin" ) ; Node groupId_node = doc . createElement ( "groupId" ) ; groupId_node . setTextContent ( "org.apache.maven.plugins" ) ; Node artifactId_node = doc . createElement ( "artifactId" ) ; artifactId_node . setTextContent ( "maven-surefire-plugin" ) ; Node version_node = doc . createElement ( "version" ) ; version_node . setTextContent ( "2.20.1" ) ; Node config_node = doc . createElement ( "configuration" ) ; Node arg_node = doc . createElement ( "argLine" ) ; arg_node . setTextContent ( "-javaagent:" + tamiflex_path ) ; config_node . appendChild ( arg_node ) ; plugin_node . appendChild ( groupId_node ) ; plugin_node . appendChild ( artifactId_node ) ; plugin_node . appendChild ( version_node ) ; plugin_node . appendChild ( config_node ) ; XPathExpression expr2 = xpath . compile ( "/project/build/plugins" ) ; NodeList nodes2 = ( NodeList ) expr2 . evaluate ( doc , XPathConstants . NODESET ) ; if ( nodes2 . getLength ( ) == 0 ) { Node plugins_node = doc . createElement ( "plugins" ) ; plugins_node . appendChild ( plugin_node ) ; XPathExpression expr3 = xpath . compile ( "/project/build" ) ; NodeList nodes3 = ( NodeList ) expr3 . evaluate ( doc , XPathConstants . NODESET ) ; if ( nodes3 . getLength ( ) == 0 ) { Node build_node = doc . createElement ( "build" ) ; build_node . appendChild ( plugins_node ) ; XPathExpression expr4 = xpath . compile ( "/project" ) ; NodeList nodes4 = ( NodeList ) expr4 . evaluate ( doc , XPathConstants . NODESET ) ; if ( nodes4 . getLength ( ) == 1 ) { Node project_node = nodes4 . item ( 0 ) ; project_node . appendChild ( build_node ) ; } else { System . out . println ( "[TamiFlexRunner] There are zero or multiple project nodes in the POM file. " + "Please double check if it is correct." ) ; } } else if ( nodes3 . getLength ( ) == 1 ) { Node build_node = nodes3 . item ( 0 ) ; build_node . appendChild ( plugins_node ) ; } else { System . out . println ( "[TamiFlexRunner] There are multiple build nodes within the project node in the POM file. " + "Please double check if it is correct." ) ; } } else if ( nodes2 . getLength ( ) == 1 ) { Node plugins_node = nodes2 . item ( 0 ) ; plugins_node . appendChild ( plugin_node ) ; } else { System . out . println ( "[TamiFlexRunner] There are multiple plugins nodes within the build node in the POM file. " + "Please double check if the POM file is correct." ) ; } } else if ( nodes . getLength ( ) == 1 ) { Node plugin_node = nodes . item ( 0 ) . getParentNode ( ) ; NodeList children = plugin_node . getChildNodes ( ) ; Node config_node = null ; for ( int i = 0 ; i < children . getLength ( ) ; i ++ ) { Node child = children . item ( i ) ; if ( child . getNodeName ( ) . equals ( "configuration" ) ) { config_node = child ; break ; } } if ( config_node != null ) { NodeList config_children = config_node . getChildNodes ( ) ; Node arg_node = null ; for ( int j = 0 ; j < config_children . getLength ( ) ; j ++ ) { Node config_child = config_children . item ( j ) ; if ( config_child . getNodeName ( ) . equals ( "argLine" ) ) { arg_node = config_child ; break ; } } if ( arg_node == null ) { arg_node = doc . createElement ( "argLine" ) ; arg_node . setTextContent ( "-javaagent:" + tamiflex_path ) ; config_node . appendChild ( arg_node ) ; } else { String arg_option = arg_node . getTextContent ( ) ; if ( ! arg_option . contains ( tamiflex_path ) ) { if ( arg_option . isEmpty ( ) ) { arg_node . setTextContent ( "-javaagent:" + tamiflex_path ) ; } else { arg_option += " -javaagent:" + tamiflex_path ; arg_node . setTextContent ( arg_option ) ; } } } } else { config_node = doc . createElement ( "configuration" ) ; Element arg_node = doc . createElement ( "argLine" ) ; arg_node . setTextContent ( "-javaagent:" + tamiflex_path ) ; config_node . appendChild ( arg_node ) ; plugin_node . appendChild ( config_node ) ; } } else { System . out . println ( "[TamiFlexRunner] There are more than one sunfire plugin in the POM file. " + "Please double check if it is correct." ) ; } TransformerFactory transformerFactory = TransformerFactory . newInstance ( ) ; Transformer transformer = transformerFactory . newTransformer ( ) ; DOMSource source = new DOMSource ( doc ) ; StreamResult result = new StreamResult ( pom_file ) ; transformer . transform ( source , result ) ; } catch ( SAXException | ParserConfigurationException | IOException | XPathExpressionException | TransformerException e ) { e . printStackTrace ( ) ; } }
Inject TamiFlex as a java agent in the test plugin in the given POM file
Inject TamiFlex as a java agent in the test plugin in the given POM file
// parse the pom file as xml // build the xpath to locate the artifact id // note that the parent node also contains a artifactId node // so we need to specify that the artifactId node we are looking  // for is under the project node // This POM does not declare the sunfire plugin // We must declare one explicitly together with the java agent argument // no plugins node, must inject one // no build node, must inject one // found the build node // found the plugins node // This POM declares the sunfire plugin explicitly // We only need to inject the java agent argument // configuration node exists // no argLine option, insert it // already have argLine option, append the java agent after the existing options // ignore the case where tamiflex has been added as a java agent // add the java agent option directly // append the new java agent option // no configuration node, insert it // is it possible to have two sunfire plugins？ // rewrite the POM file