Skip to content

Commit

Permalink
Code inspection fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry Coles committed Jan 15, 2021
1 parent 9a749fc commit 6bae7b3
Show file tree
Hide file tree
Showing 31 changed files with 117 additions and 169 deletions.
2 changes: 1 addition & 1 deletion pitest/src/main/java/org/pitest/boot/HotSwapAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static void addTransformer(final ClassFileTransformer transformer) {
}

public static void agentmain(final String agentArguments, // NO_UCD
final Instrumentation inst) throws Exception {
final Instrumentation inst) {
instrumentation = inst;
}

Expand Down
2 changes: 1 addition & 1 deletion pitest/src/main/java/org/pitest/bytecode/NullVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public void visitTypeInsn(final int arg0, final String arg1) {
public void visitVarInsn(final int arg0, final int arg1) {
}

};
}

@Override
public void visit(final int arg0, final int arg1, final String arg2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ class FixedSizeHashMap<K,V> extends LinkedHashMap<K,V> {
protected boolean removeEldestEntry(Map.Entry <K,V> eldest) {
return size() > this.maxsize;
}
};
}
16 changes: 6 additions & 10 deletions pitest/src/main/java/org/pitest/classpath/ClassPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@

package org.pitest.classpath;

import org.pitest.functional.FCollection;
import org.pitest.util.Log;
import org.pitest.util.ManifestUtils;
import org.pitest.util.PitError;
import org.pitest.util.StreamUtil;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -31,12 +37,6 @@
import java.util.logging.Logger;
import java.util.zip.ZipException;

import org.pitest.functional.FCollection;
import org.pitest.util.Log;
import org.pitest.util.ManifestUtils;
import org.pitest.util.PitError;
import org.pitest.util.StreamUtil;

public class ClassPath {

private static final Logger LOG = Log.getLogger();
Expand Down Expand Up @@ -106,11 +106,7 @@ public byte[] getClassData(final String classname) throws IOException {
}

public URL findResource(final String name) {
try {
return this.root.getResource(name);
} catch (final IOException exception) {
return null;
}
}

public static Collection<String> getClassPathElementsAsPaths() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package org.pitest.classpath;

import org.pitest.functional.FCollection;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.pitest.functional.FCollection;
import java.util.Optional;

public class CompoundClassPathRoot implements ClassPathRoot,
Expand Down Expand Up @@ -43,7 +42,7 @@ public Collection<String> classNames() {
}

@Override
public URL getResource(String name) throws MalformedURLException {
public URL getResource(String name) {
try {
return findRootForResource(name);
} catch (final IOException exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@
*/
package org.pitest.classpath;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;

import java.util.Optional;

public class OtherClassLoaderClassPathRoot implements ClassPathRoot {
Expand All @@ -36,13 +33,13 @@ public Collection<String> classNames() {
}

@Override
public InputStream getData(final String name) throws IOException {
public InputStream getData(final String name) {
// TODO will this work for archives? Need to consider remote hetrogenous os
return this.loader.getResourceAsStream(name.replace(".", "/") + ".class");
}

@Override
public URL getResource(final String name) throws MalformedURLException {
public URL getResource(final String name) {
return this.loader.getResource(name);
}

Expand Down
17 changes: 7 additions & 10 deletions pitest/src/main/java/org/pitest/coverage/CoverageTransformer.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package org.pitest.coverage;

import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.pitest.bytecode.FrameOptions;
import org.pitest.classinfo.ComputeClassWriter;
import org.pitest.classpath.ClassloaderByteArraySource;

import sun.pitest.CodeCoverageStore;

import java.lang.instrument.ClassFileTransformer;
import java.security.ProtectionDomain;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;

public class CoverageTransformer implements ClassFileTransformer {

private final Predicate<String> filter;
Expand All @@ -27,8 +25,7 @@ public CoverageTransformer(final Predicate<String> filter) {
@Override
public byte[] transform(final ClassLoader loader, final String className,
final Class<?> classBeingRedefined,
final ProtectionDomain protectionDomain, final byte[] classfileBuffer)
throws IllegalClassFormatException {
final ProtectionDomain protectionDomain, final byte[] classfileBuffer) {
final boolean include = shouldInclude(className);
if (include) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
package org.pitest.coverage.analysis;

import static org.objectweb.asm.Opcodes.ARETURN;
import static org.objectweb.asm.Opcodes.ATHROW;
import static org.objectweb.asm.Opcodes.DRETURN;
import static org.objectweb.asm.Opcodes.FRETURN;
import static org.objectweb.asm.Opcodes.IRETURN;
import static org.objectweb.asm.Opcodes.LRETURN;
import static org.objectweb.asm.Opcodes.RETURN;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.FrameNode;
import org.objectweb.asm.tree.InsnList;
Expand All @@ -24,6 +11,19 @@
import org.objectweb.asm.tree.TableSwitchInsnNode;
import org.objectweb.asm.tree.TryCatchBlockNode;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.objectweb.asm.Opcodes.ARETURN;
import static org.objectweb.asm.Opcodes.ATHROW;
import static org.objectweb.asm.Opcodes.DRETURN;
import static org.objectweb.asm.Opcodes.FRETURN;
import static org.objectweb.asm.Opcodes.IRETURN;
import static org.objectweb.asm.Opcodes.LRETURN;
import static org.objectweb.asm.Opcodes.RETURN;

public class ControlFlowAnalyser {

private static final int LIKELY_NUMBER_OF_LINES_PER_BLOCK = 7;
Expand All @@ -33,16 +33,6 @@ public static List<Block> analyze(final MethodNode mn) {

final Set<LabelNode> jumpTargets = findJumpTargets(mn.instructions);

/*
* Some projects/libraries have gigantic static initializer methods
* that load up huge constant arrays. These methods are nearly at the
* size limit - and adding a probe at each store blows it up.
*
* So, for methods with many instructions, we'll ignore array stores
* as ending a block.
*/
final boolean ignoreArrayStores = mn.instructions.size() > 10000;

// not managed to construct bytecode to show need for this
// as try catch blocks usually have jumps at their boundaries anyway.
// so possibly useless, but here for now. Because fear.
Expand Down Expand Up @@ -70,7 +60,7 @@ public static List<Block> analyze(final MethodNode mn) {
blocks.add(new Block(blockStart, i - 1, blockLines));
blockStart = i;
blockLines = smallSet();
} else if (endsBlock(ins, ignoreArrayStores)) {
} else if (endsBlock(ins)) {
if (blockLines.isEmpty() && blocks.size() > 0 && !blocks
.get(blocks.size() - 1).getLines().isEmpty()) {
blockLines.addAll(blocks.get(blocks.size() - 1).getLines());
Expand Down Expand Up @@ -112,21 +102,13 @@ private static void addtryCatchBoundaries(final MethodNode mn,
}
}

private static boolean endsBlock(final AbstractInsnNode ins,
final boolean ignoreArrayStores) {
private static boolean endsBlock(final AbstractInsnNode ins) {
return (ins instanceof JumpInsnNode) || isReturn(ins)
|| isMightThrowException(ins, ignoreArrayStores);
|| isMightThrowException(ins);
}

private static boolean isMightThrowException(final AbstractInsnNode ins,
final boolean ignoreArrayStores) {
switch (ins.getType()) {
case AbstractInsnNode.METHOD_INSN:
return true;
default:
return false;
}

private static boolean isMightThrowException(final AbstractInsnNode ins) {
return ins.getType() == AbstractInsnNode.METHOD_INSN;
}

private static boolean isReturn(final AbstractInsnNode ins) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ private static Predicate<String> convertToJVMClassFilter(
}

private static List<TestUnit> getTestsFromParent(
final SafeDataInputStream dis, final CoverageOptions paramsFromParent)
throws IOException {
final SafeDataInputStream dis, final CoverageOptions paramsFromParent) {
final List<ClassName> classes = receiveTestClassesFromParent(dis);
Collections.sort(classes); // ensure classes loaded in a consistent order

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private static Comparator<DependencyAccess> equalDestinationComparator() {
}

private List<DependencyAccess> extract(final String clazz,
final Predicate<DependencyAccess> filter) throws IOException {
final Predicate<DependencyAccess> filter) {
final Optional<byte[]> bytes = this.classToBytes.getBytes(clazz);
if (!bytes.isPresent()) {
LOG.warning("No bytes found for " + clazz);
Expand Down
6 changes: 1 addition & 5 deletions pitest/src/main/java/org/pitest/functional/Streams.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

public class Streams {
public static <T> Stream<T> fromOptional(java.util.Optional<T> opt) {
if (opt.isPresent()) {
return Stream.of(opt.get());
} else {
return Stream.empty();
}
return opt.map(Stream::of).orElseGet(Stream::empty);
}
}
9 changes: 3 additions & 6 deletions pitest/src/main/java/org/pitest/junit/TestInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
*/
package org.pitest.junit;

import java.util.function.Predicate;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.pitest.classinfo.ClassInfo;

import java.util.Optional;
import java.util.function.Predicate;

abstract class TestInfo {

Expand All @@ -37,10 +37,7 @@ private static boolean isATest(final ClassInfo clazz) {
}

private static boolean isATest(final Optional<ClassInfo> clazz) {
if (clazz.isPresent()) {
return isATest(clazz.get());
}
return false;
return clazz.filter(TestInfo::isATest).isPresent();
}

public static Predicate<ClassInfo> isATest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class AdaptingRunListener extends RunListener {
}

@Override
public void testFailure(final Failure failure) throws Exception {
public void testFailure(final Failure failure) {
this.rc.notifyEnd(this.description, failure.getException());
this.failed = true;
}
Expand All @@ -31,12 +31,12 @@ public void testAssumptionFailure(final Failure failure) {
}

@Override
public void testIgnored(final Description description) throws Exception {
public void testIgnored(final Description description) {
this.rc.notifySkipped(this.description);
}

@Override
public void testStarted(final Description description) throws Exception {
public void testStarted(final Description description) {
if (this.failed) {
// If the JUnit test has been annotated with @BeforeClass or @AfterClass
// need to force the exit after the first failure as tests will be run as
Expand All @@ -49,7 +49,7 @@ public void testStarted(final Description description) throws Exception {
}

@Override
public void testFinished(final Description description) throws Exception {
public void testFinished(final Description description) {
if (!this.failed) {
this.rc.notifyEnd(this.description);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,4 @@ public boolean isDetected() {
public boolean hasCoverage() {
return this != NO_COVERAGE;
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,8 @@ public AvoidStringSwitchedMethodAdapter(final MutationContext context,
@Override
public void visitMethodInsn(final int opcode, final String owner,
final String name, final String desc, boolean itf) {
if ((opcode == Opcodes.INVOKEVIRTUAL) && "java/lang/String".equals(owner)
&& "hashCode".equals(name)) {
this.hashCodeWasLastCall = true;
} else {
this.hashCodeWasLastCall = false;
}
this.hashCodeWasLastCall = (opcode == Opcodes.INVOKEVIRTUAL) && "java/lang/String".equals(owner)
&& "hashCode".equals(name);
super.visitMethodInsn(opcode, owner, name, desc, itf);
}

Expand Down
Loading

0 comments on commit 6bae7b3

Please sign in to comment.