private boolean isTransformed(Class clazz, String name, boolean isInterface) { if (isBytemanClass(name) || !isTransformable(name)) { return false; } boolean found = false; List<RuleScript> scripts; if (isInterface) { scripts = scriptRepository.scriptsForInterfaceName(name); } else { scripts = scriptRepository.scriptsForClassName(name); } if (scripts != null) { for (RuleScript script : scripts) { if (!script.hasTransform(clazz)) { found = true; if (isVerbose()) { System.out.println("Retransforming loaded bootstrap class " + clazz.getName()); } break; } } } return found; }
private byte[] tryTransform( byte[] buffer, String name, ClassLoader loader, String key, boolean isInterface, boolean isOverride) { List<RuleScript> ruleScripts; if (isInterface) { ruleScripts = scriptRepository.scriptsForInterfaceName(key); } else { ruleScripts = scriptRepository.scriptsForClassName(key); } byte[] newBuffer = buffer; if (ruleScripts != null) { // if (isVerbose()) { // System.out.println("tryTransform : " + name + " for " + key); // } int counter = 0; for (RuleScript ruleScript : ruleScripts) { try { // we only transform via isOverride rules if isOverride is true // we tarsnform via any matchign rules if isOverride is false if (!isOverride || ruleScript.isOverride()) { // only do the transform if the script has not been deleted synchronized (ruleScript) { if (!ruleScript.isDeleted()) { maybeDumpClassIntermediate(name, newBuffer); newBuffer = transform(ruleScript, loader, name, newBuffer); } } } } catch (Throwable th) { // yeeeurgh I know this looks ugly with no rethrow but it is appropriate // we do not want to pass on any errors or runtime exceptions // if a transform fails then we should still allow the load to continue // with whatever other transforms succeed. we tarce the throwable to // System.err just to ensure it can be seen. System.err.println("Transformer.transform : caught throwable " + th); th.printStackTrace(System.err); } } } return newBuffer; }
/** * constructor allowing this transformer to be provided with access to the JVM's instrumentation * implementation * * @param inst the instrumentation object used to interface to the JVM * @param scriptPaths list of file paths for each input script * @param scriptTexts the text of each input script * @param isRedefine true if class redefinition is allowed false if not * @throws Exception if a script is in error */ public Transformer( Instrumentation inst, List<String> scriptPaths, List<String> scriptTexts, boolean isRedefine) throws Exception { this.inst = inst; this.isRedefine = isRedefine; scriptRepository = new ScriptRepository(skipOverrideRules); loadCache = new LoadCache(inst); helperManager = new HelperManager(inst); Iterator<String> scriptsIter = scriptTexts.iterator(); Iterator<String> filesIter = scriptPaths.iterator(); while (scriptsIter.hasNext()) { String scriptText = scriptsIter.next(); String file = filesIter.next(); List<RuleScript> ruleScripts = scriptRepository.processScripts(scriptText, file); for (RuleScript ruleScript : ruleScripts) { String name = ruleScript.getName(); RuleScript previous = scriptRepository.scriptForRuleName(name); if (previous == null) { scriptRepository.addScript(ruleScript); } else { StringBuffer buffer = new StringBuffer(); buffer.append("Transformer : duplicate script name "); buffer.append(name); buffer.append("in file "); buffer.append(ruleScript.getFile()); buffer.append(" line "); buffer.append(ruleScript.getLine()); buffer.append("\n previously defined in file "); buffer.append(previous.getFile()); buffer.append(" line "); buffer.append(previous.getLine()); Exception ex = new Exception(buffer.toString()); throw ex; } } } }
protected void dumpScript(RuleScript ruleScript) { String file = ruleScript.getFile(); int line = ruleScript.getLine(); if (file != null) { System.out.println("# " + file + " line " + line); } System.out.println("RULE " + ruleScript.getName()); if (ruleScript.isInterface()) { System.out.println("INTERFACE " + ruleScript.getTargetClass()); } else { System.out.println("CLASS " + ruleScript.getTargetClass()); } System.out.println("METHOD " + ruleScript.getTargetMethod()); if (ruleScript.getTargetHelper() != null) { System.out.println("HELPER " + ruleScript.getTargetHelper()); } System.out.println(ruleScript.getTargetLocation()); System.out.println(ruleScript.getRuleText()); System.out.println("ENDRULE"); }