@Override public Optional<byte[]> getBytes(String clazz) { Optional<byte[]> maybeBytes = this.cache.get(clazz); if (maybeBytes != null) { return maybeBytes; } maybeBytes = this.child.getBytes(clazz); this.cache.put(clazz, maybeBytes); return maybeBytes; }
private ClassByteArraySource fallbackToClassLoader(final ClassByteArraySource bas) { final ClassByteArraySource clSource = ClassloaderByteArraySource.fromContext(); return clazz -> { final Optional<byte[]> maybeBytes = bas.getBytes(clazz); if (maybeBytes.isPresent()) { return maybeBytes; } LOG.log(Level.FINE, "Could not find " + clazz + " on classpath for analysis. Falling back to classloader"); return clSource.getBytes(clazz); }; } }
public Optional<byte[]> querySource(final ClassName name) { if (this.unknownClasses.contains(name)) { return Optional.empty(); } final Optional<byte[]> option = this.source.getBytes(name.asJavaName()); if (option.isPresent()) { return option; } this.unknownClasses.add(name); return option; }
public Optional<byte[]> querySource(final ClassName name) { if (this.unknownClasses.contains(name)) { return Optional.empty(); } final Optional<byte[]> option = this.source.getBytes(name.asJavaName()); if (option.isPresent()) { return option; } this.unknownClasses.add(name); return option; }
private byte[] classBytes(final String className) { final Optional<byte[]> bytes = this.classByteSource.getBytes(className); if (bytes.isPresent()) { return bytes.get(); } throw new PitError("Unable to load class content for " + className); }
/** * Returns a ClassReader corresponding to the given class or interface. * * @param type * the internal name of a class or interface. * @return the ClassReader corresponding to 'type'. */ private ClassReader typeInfo(final String type) { final Optional<byte[]> maybeBytes = this.bytes.getBytes(type); if (!maybeBytes.isPresent()) { throw new PitError("Could not find class defintiion for " + type); } return new ClassReader(maybeBytes.get()); } }
@Override public List<MutationDetails> findMutations( final ClassName classToMutate) { final ClassContext context = new ClassContext(); context.setTargetMutation(Optional.<MutationIdentifier> empty()); Optional<byte[]> bytes = GregorMutater.this.byteSource.getBytes( classToMutate.asInternalName()); return bytes.map(findMutations(context)) .orElse(Collections.<MutationDetails>emptyList()); }
byte[] bytesFor(Class<?> clazz) { return this.source.getBytes(clazz.getName()).get(); }
@Override public void begin(ClassTree clazz) { this.currentClass = clazz.name(); final String[] classLocation = ("export." + clazz.name().asJavaName()) .split("\\."); final Path classDir = this.fileSystem.getPath(this.outDir, classLocation); this.mutantsDir = classDir.resolve("mutants"); try { Files.createDirectories(this.mutantsDir); writeBytecodeToDisk(this.source.getBytes(clazz.name().asJavaName()).get(), classDir); } catch (final IOException e) { throw new RuntimeException("Couldn't create direectory for " + clazz, e); } }
private InstructionTrackingMethodVisitor analyse(final Class<?> clazz, final String targetMethod) { final ClassReader reader = new ClassReader(this.byteSource.getBytes( clazz.getName()).get()); final Analyser cv = new Analyser(targetMethod); reader.accept(cv, 0); return cv.testee; }
private boolean atLeastOneSampleExists(String sample) { for (final String compiler : COMPILERS) { final String clazz = makeClassName(sample, compiler); if (this.source.getBytes(clazz).isPresent()) { return true; } } throw new RuntimeException("No samples found for any compiler for " + sample); }
ClassTree forClass(Class<?> clazz) { final byte[] bs = this.source.getBytes(clazz.getName()).get(); return ClassTree.fromBytes(bs); }
ClassTree forClass(Class<?> clazz) { final byte[] bs = this.source.getBytes(clazz.getName()).get(); return ClassTree.fromBytes(bs); } }
private List<DependencyAccess> extract(final String clazz, final Predicate<DependencyAccess> filter) throws IOException { final Optional<byte[]> bytes = this.classToBytes.getBytes(clazz); if (!bytes.isPresent()) { LOG.warning("No bytes found for " + clazz); return Collections.emptyList(); } final ClassReader reader = new ClassReader(bytes.get()); final List<DependencyAccess> dependencies = new ArrayList<>(); final SideEffect1<DependencyAccess> se = constructCollectingSideEffectForVisitor( dependencies, and(nameIsEqual(clazz).negate(), filter)); final DependencyClassVisitor dcv = new DependencyClassVisitor( new NullVisitor(), se); reader.accept(dcv, ClassReader.EXPAND_FRAMES); return dependencies; }
@Before public void setUp() { MockitoAnnotations.initMocks(this); when(this.byteSource.getBytes(anyString())).thenReturn( Optional.ofNullable(new byte[1])); this.testee = new JarCreatingJarFinder(this.byteSource); }
private byte[] transform(final Class<?> clazz) throws IllegalClassFormatException { final CoverageTransformer testee = new CoverageTransformer( s -> true); final byte[] bs = testee.transform(this.loader, clazz.getName(), null, null, this.bytes.getBytes(clazz.getName()).get()); return bs; }
@Test public void shouldReturnNoneWhenNoTesteeExistsMatchingNamingConvention() { final byte[] bytes = null; when(this.source.getBytes("com.example.Foo")) .thenReturn(Optional.ofNullable(bytes)); assertEquals(Optional.empty(), this.testee.findTestee("com.example.TestFoo")); }
private MethodNode makeTree(final Class<?> clazz, final String name) { final ClassReader reader = new ClassReader(this.byteSource.getBytes( ClassName.fromClass(clazz).asJavaName()).get()); final ClassNode tree = new ClassNode(); reader.accept(tree, 0); for (final Object m : tree.methods) { final MethodNode mn = (MethodNode) m; if (mn.name.equals(name)) { return mn; } } throw new RuntimeException("Method " + name + " not found in " + clazz); }
@Test public void shouldReturnNoneWhenNoTesteeExistsMatchingNamingConvention() { final byte[] bytes = null; when(this.source.getBytes("com.example.Foo")) .thenReturn(Optional.ofNullable(bytes)); assertEquals(Optional.empty(), this.testee.findTestee("com.example.TestFoo")); }
private void restoreLastClass(final ClassByteArraySource byteSource, final ClassName clazzName, final ClassLoader loader) throws ClassNotFoundException { if ((this.lastMutatedClass != null) && !this.lastMutatedClass.equals(clazzName)) { restoreForLoader(this.lastUsedLoader); restoreForLoader(loader); } if ((this.lastMutatedClass == null) || !this.lastMutatedClass.equals(clazzName)) { this.lastClassPreMutation = byteSource.getBytes(clazzName.asJavaName()) .get(); } this.lastMutatedClass = clazzName; }