List of usage examples for org.objectweb.asm.tree ClassNode ClassNode
public ClassNode()
From source file:org.evosuite.instrumentation.DescriptorMapping.java
License:Open Source License
private boolean isOutsideField(String className, String fieldName, String desc) { Set<String> visited = new HashSet<String>(); Queue<String> parents = new LinkedList<String>(); parents.add(className);/*from w ww . j ava 2s . c om*/ while (!parents.isEmpty()) { String name = parents.poll(); if (name == null) continue; visited.add(name); logger.info("Checking class {} while looking for definition of field {}", name, fieldName); ClassReader reader; try { reader = new ClassReader(name); ClassNode parent = new ClassNode(); reader.accept(parent, ClassReader.EXPAND_FRAMES); boolean isInside = isInside(parent.name); // boolean isInside = parent.name.startsWith(Properties.PROJECT_PREFIX.replace(".", // "/")) // | parent.name.startsWith(Properties.TARGET_CLASS_PREFIX.replace(".", // "/")); for (Object o : parent.fields) { FieldNode mn2 = (FieldNode) o; if (mn2.name.equals(fieldName) && mn2.desc.equals(desc)) { //if ((mn2.access & Opcodes.ACC_SYNTHETIC) == Opcodes.ACC_SYNTHETIC) { // logger.info("Not transforming synthetic field " + mn2.name); // return true; //} if (!isInside) { logger.info("Field {} was defined outside the test package - {}", name, parent.name); return true; } else { logger.info("Field {} was defined inside the test package {}", name, parent.name); return false; } } } for (Object o : parent.interfaces) { String par = (String) o; if (!visited.contains(par) && !parents.contains(par)) { parents.add(par); } } if (!visited.contains(parent.superName) && !parents.contains(parent.superName)) { parents.add(parent.superName); } } catch (IOException e) { logger.info("Error reading class {}", name); } } return false; }
From source file:org.evosuite.instrumentation.testability.DescriptorMapping.java
License:Open Source License
private boolean isOutsideMethod(String className, String methodName, String desc) { Set<String> visited = new HashSet<String>(); Queue<String> parents = new LinkedList<String>(); parents.add(className);//from www . jav a 2s . c o m while (!parents.isEmpty()) { String name = parents.poll(); if (name == null) continue; visited.add(name); logger.info("Visiting class " + name + " while looking for source of " + className + "." + methodName); ClassReader reader; try { reader = new ClassReader( ResourceList.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()) .getClassAsStream(name)); ClassNode parent = new ClassNode(); reader.accept(parent, ClassReader.EXPAND_FRAMES); boolean isInside = isInside(parent.name); //boolean isInside = parent.name.startsWith(Properties.PROJECT_PREFIX.replace(".", // "/")) // || (!Properties.TARGET_CLASS_PREFIX.isEmpty() && parent.name.startsWith(Properties.TARGET_CLASS_PREFIX.replace(".", // "/"))); logger.info("Checking " + parent.name); for (Object o : parent.methods) { MethodNode mn2 = (MethodNode) o; if (mn2.name.equals(methodName) && mn2.desc.equals(desc)) { if (!isInside) { logger.info("Method " + name + " was defined outside the test package"); return true; } else { logger.info("Method " + name + " was defined outside the test package"); //return false; } } } for (Object o : parent.interfaces) { String par = (String) o; if (!visited.contains(par) && !parents.contains(par)) { parents.add(par); } } if (!visited.contains(parent.superName) && !parents.contains(parent.superName)) { parents.add(parent.superName); } } catch (IOException e) { logger.info("Error reading class " + name); } } return false; }
From source file:org.evosuite.instrumentation.testability.DescriptorMapping.java
License:Open Source License
private String transformMethodName(String className, String methodName, String desc, String transformedDesc) { Set<String> visited = new HashSet<String>(); Queue<String> parents = new LinkedList<String>(); parents.add(className);//from w w w . j ava 2 s . c om while (!parents.isEmpty()) { String name = parents.poll(); if (name == null) continue; visited.add(name); logger.info("Visiting class " + name + " while looking for name clashes of " + className + "." + methodName + transformedDesc); ClassReader reader; try { reader = new ClassReader( ResourceList.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()) .getClassAsStream(name)); ClassNode parent = new ClassNode(); reader.accept(parent, ClassReader.EXPAND_FRAMES); if (originalDesc.containsKey(className + "." + methodName + transformedDesc)) { logger.info("Method " + methodName + " has conflicting transformed method"); return methodName + "_transformed" + (id++); } for (Object o : parent.methods) { MethodNode mn2 = (MethodNode) o; //logger.info("Checking " + parent.name + "." + mn2.name + mn2.desc); if (mn2.name.equals(methodName) && mn2.desc.equals(transformedDesc)) { logger.info("Method " + methodName + " has conflicting method"); if (methodName.equals("<init>")) return null; // TODO: This should be a bit nicer return methodName + "_transformed" + (id++); } } for (Object o : parent.interfaces) { String par = (String) o; if (!visited.contains(par) && !parents.contains(par)) { parents.add(par); } } if (!visited.contains(parent.superName) && !parents.contains(parent.superName)) { parents.add(parent.superName); } } catch (IOException e) { logger.info("Error reading class " + name); } } return methodName; }
From source file:org.evosuite.instrumentation.testability.DescriptorMapping.java
License:Open Source License
private boolean isOutsideField(String className, String fieldName, String desc) { Set<String> visited = new HashSet<String>(); Queue<String> parents = new LinkedList<String>(); parents.add(className);//from w w w. ja va 2s .co m while (!parents.isEmpty()) { String name = parents.poll(); if (name == null) continue; visited.add(name); logger.info("Checking class " + name + " while looking for definition of field " + fieldName); ClassReader reader; try { reader = new ClassReader( ResourceList.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()) .getClassAsStream(name)); ClassNode parent = new ClassNode(); reader.accept(parent, ClassReader.EXPAND_FRAMES); boolean isInside = isInside(parent.name); // boolean isInside = parent.name.startsWith(Properties.PROJECT_PREFIX.replace(".", // "/")) // | parent.name.startsWith(Properties.TARGET_CLASS_PREFIX.replace(".", // "/")); for (Object o : parent.fields) { FieldNode mn2 = (FieldNode) o; if (mn2.name.equals(fieldName) && mn2.desc.equals(desc)) { //if ((mn2.access & Opcodes.ACC_SYNTHETIC) == Opcodes.ACC_SYNTHETIC) { // logger.info("Not transforming synthetic field " + mn2.name); // return true; //} if (!isInside) { logger.info("Field " + name + " was defined outside the test package - " + parent.name); return true; } else { logger.info("Field " + name + " was defined inside the test package " + parent.name); return false; } } } for (Object o : parent.interfaces) { String par = (String) o; if (!visited.contains(par) && !parents.contains(par)) { parents.add(par); } } if (!visited.contains(parent.superName) && !parents.contains(parent.superName)) { parents.add(parent.superName); } } catch (IOException e) { logger.info("Error reading class " + name); } } return false; }
From source file:org.evosuite.instrumentation.testability.TestabilityTransformationClassLoader.java
License:Open Source License
private Class<?> instrumentClass(String fullyQualifiedTargetClass) throws ClassNotFoundException { logger.info("Instrumenting class '" + fullyQualifiedTargetClass + "'."); try {/* w ww . j a va 2s .c o m*/ String className = fullyQualifiedTargetClass.replace('.', '/'); InputStream is = ResourceList.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()) .getClassAsStream(className); if (is == null) { throw new ClassNotFoundException("Class '" + className + ".class" + "' should be in target project, but could not be found!"); } ClassReader reader = new ClassReader(is); ClassNode classNode = new ClassNode(); reader.accept(classNode, ClassReader.SKIP_FRAMES); ClassVisitor cv = new CFGClassAdapter(classLoader, null, className); classNode.accept(cv); BooleanTestabilityTransformation tt = new BooleanTestabilityTransformation(classNode, this); // cv = new TraceClassVisitor(writer, new // PrintWriter(System.out)); //cv = new TraceClassVisitor(cv, new PrintWriter(System.out)); //cv = new CheckClassAdapter(cv); try { //tt.transform().accept(cv); //if (isTargetClassName(classNameWithDots)) classNode = tt.transform(); } catch (Throwable t) { throw new Error(t); } ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES); classNode.accept(writer); byte[] byteBuffer = writer.toByteArray(); Class<?> result = defineClass(fullyQualifiedTargetClass, byteBuffer, 0, byteBuffer.length); classes.put(fullyQualifiedTargetClass, result); logger.info("Keeping class: " + fullyQualifiedTargetClass); return result; } catch (Throwable t) { throw new ClassNotFoundException(t.getMessage(), t); } }
From source file:org.evosuite.instrumentation.TestabilityTransformationClassLoader.java
License:Open Source License
private Class<?> instrumentClass(String fullyQualifiedTargetClass) throws ClassNotFoundException { logger.info("Instrumenting class '{}'.", fullyQualifiedTargetClass); try {/* ww w. j a v a 2 s .c o m*/ String className = fullyQualifiedTargetClass.replace('.', '/'); InputStream is = ResourceList.getClassAsStream(className); if (is == null) { throw new ClassNotFoundException("Class '" + className + ".class" + "' should be in target project, but could not be found!"); } ClassReader reader = new ClassReader(is); ClassNode classNode = new ClassNode(); reader.accept(classNode, ClassReader.SKIP_FRAMES); ClassVisitor cv = new CFGClassAdapter(classLoader, null, className); classNode.accept(cv); BooleanTestabilityTransformation tt = new BooleanTestabilityTransformation(classNode, this); // cv = new TraceClassVisitor(writer, new // PrintWriter(System.out)); //cv = new TraceClassVisitor(cv, new PrintWriter(System.out)); //cv = new CheckClassAdapter(cv); try { //tt.transform().accept(cv); //if (isTargetClassName(classNameWithDots)) classNode = tt.transform(); } catch (Throwable t) { throw new Error(t); } ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES); classNode.accept(writer); byte[] byteBuffer = writer.toByteArray(); Class<?> result = defineClass(fullyQualifiedTargetClass, byteBuffer, 0, byteBuffer.length); classes.put(fullyQualifiedTargetClass, result); logger.info("Keeping class: {}", fullyQualifiedTargetClass); return result; } catch (Throwable t) { throw new ClassNotFoundException(t.getMessage(), t); } }
From source file:org.evosuite.junit.DetermineSUT.java
License:Open Source License
public Set<String> determineCalledClasses(String fullyQualifiedTargetClass, Set<String> targetClasses) throws ClassNotFoundException, NoJUnitClassException { Set<String> calledClasses = new HashSet<String>(); String className = fullyQualifiedTargetClass.replace('.', '/'); try {/*from w w w .j a va2 s . c o m*/ InputStream is = ClassLoader.getSystemResourceAsStream(className + ".class"); if (is == null) { throw new ClassNotFoundException("Class '" + className + ".class" + "' should be in target project, but could not be found!"); } ClassReader reader = new ClassReader(is); ClassNode classNode = new ClassNode(); reader.accept(classNode, ClassReader.SKIP_FRAMES); superClasses = getSuperClasses(classNode); if (isJUnitTest(classNode)) { handleClassNode(calledClasses, classNode, targetClasses); } else { throw new NoJUnitClassException(); } } catch (IOException e) { e.printStackTrace(); } calledClasses.remove("java.lang.Object"); calledClasses.remove(fullyQualifiedTargetClass); return calledClasses; }
From source file:org.evosuite.junit.DetermineSUT.java
License:Open Source License
private ClassNode loadClassNode(String className) throws IOException { ClassReader reader = new ClassReader( ResourceList.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()) .getClassAsStream(className)); ClassNode cn = new ClassNode(); reader.accept(cn, ClassReader.SKIP_FRAMES); // | ClassReader.SKIP_DEBUG); return cn;/*w ww. j a v a 2 s. co m*/ }
From source file:org.evosuite.regression.bytecode.RegressionClassDiff.java
License:Open Source License
private static Map<String, List<Integer>> getClassInstructions(InputStream classAsInputStream) { HashMap<String, List<Integer>> methodInstructionsMap = new HashMap<>(); try {//from w w w .j a va 2 s. c om ClassReader reader = new ClassReader(classAsInputStream); ClassNode classNode = new ClassNode(); reader.accept(classNode, 0); @SuppressWarnings("unchecked") final List<MethodNode> methods = classNode.methods; Printer printer = new Textifier(); TraceMethodVisitor mp = new TraceMethodVisitor(printer); for (MethodNode m : methods) { List<Integer> instructions = new ArrayList<>(); InsnList inList = m.instructions; String mathodID = m.name + ": " + m.desc; System.out.println(mathodID); int[] methodInstructions = new int[inList.size()]; for (int i = 0; i < inList.size(); i++) { int op = inList.get(i).getOpcode(); methodInstructions[i] = op; AbstractInsnNode insn = inList.get(i); insn.accept(mp); // Uncomment the following comment block to print the bytecode // instructions // StringWriter sw = new StringWriter(); // printer.print(new PrintWriter(sw)); // printer.getText().clear(); // System.out.println(sw.toString()); // logger.warn("{} -> {}", sw.toString(), op); if (op != -1) instructions.add(op); } methodInstructionsMap.put(mathodID, instructions); } } catch (IOException e) { // Will fail if ClassReader fails e.printStackTrace(); } return methodInstructionsMap; }
From source file:org.evosuite.regression.RegressionClassDiff.java
License:Open Source License
public static Map<String, List<Integer>> getClassInstructions(InputStream classAsInputStream) { HashMap<String, List<Integer>> methodInstructionsMap = new HashMap<>(); try {//from w ww. java2 s .co m ClassReader reader = new ClassReader(classAsInputStream); ClassNode classNode = new ClassNode(); reader.accept(classNode, 0); @SuppressWarnings("unchecked") final List<MethodNode> methods = classNode.methods; Printer printer = new Textifier(); TraceMethodVisitor mp = new TraceMethodVisitor(printer); for (MethodNode m : methods) { List<Integer> instructions = new ArrayList<>(); InsnList inList = m.instructions; String mathodID = m.name + ": " + m.desc; System.out.println(mathodID); int[] methodInstructions = new int[inList.size()]; for (int i = 0; i < inList.size(); i++) { int op = inList.get(i).getOpcode(); methodInstructions[i] = op; AbstractInsnNode insn = inList.get(i); insn.accept(mp); // Uncomment the following comment block to print the bytecode // instructions // StringWriter sw = new StringWriter(); // printer.print(new PrintWriter(sw)); // printer.getText().clear(); // System.out.println(sw.toString()); // logger.warn("{} -> {}", sw.toString(), op); if (op != -1) instructions.add(op); } methodInstructionsMap.put(mathodID, instructions); } } catch (IOException e) { // Will fail if ClassReader fails e.printStackTrace(); } return methodInstructionsMap; }