misc minor changes

This commit is contained in:
Tyler Benson 2018-10-19 14:26:39 +10:00
parent 0f02264e50
commit 192b0fd478
6 changed files with 129 additions and 105 deletions

View File

@ -6,7 +6,7 @@ import net.bytebuddy.build.Plugin;
import net.bytebuddy.description.type.TypeDefinition;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.DynamicType.Builder;
import net.bytebuddy.dynamic.DynamicType;
/** Bytebuddy gradle plugin which creates muzzle-references at compile time. */
public class MuzzleGradlePlugin implements Plugin {
@ -32,8 +32,8 @@ public class MuzzleGradlePlugin implements Plugin {
}
@Override
public Builder<?> apply(
final Builder<?> builder,
public DynamicType.Builder<?> apply(
final DynamicType.Builder<?> builder,
final TypeDescription typeDescription,
final ClassFileLocator classFileLocator) {
return builder.visit(new MuzzleVisitor());
@ -50,8 +50,8 @@ public class MuzzleGradlePlugin implements Plugin {
}
@Override
public Builder<?> apply(
final Builder<?> builder,
public DynamicType.Builder<?> apply(
final DynamicType.Builder<?> builder,
final TypeDescription typeDescription,
final ClassFileLocator classFileLocator) {
return builder;

View File

@ -29,7 +29,7 @@ public class MuzzleVersionScanPlugin {
});
}
public static void assertInstrumentationMuzzled(ClassLoader cl, boolean assertPass)
public static void assertInstrumentationMuzzled(final ClassLoader cl, final boolean assertPass)
throws Exception {
// muzzle validate all instrumenters
for (Instrumenter instrumenter :
@ -49,17 +49,20 @@ public class MuzzleVersionScanPlugin {
try {
m = instrumenter.getClass().getDeclaredMethod("getInstrumentationMuzzle");
m.setAccessible(true);
ReferenceMatcher muzzle = (ReferenceMatcher) m.invoke(instrumenter);
List<Reference.Mismatch> mismatches = muzzle.getMismatchedReferenceSources(cl);
boolean passed = mismatches.size() == 0;
final ReferenceMatcher muzzle = (ReferenceMatcher) m.invoke(instrumenter);
final List<Reference.Mismatch> mismatches = muzzle.getMismatchedReferenceSources(cl);
final boolean passed = mismatches.size() == 0;
if (mismatches.size() > 0) {}
if (passed && !assertPass) {
System.err.println("MUZZLE PASSED BUT FAILURE WAS EXPECTED");
System.err.println(
"MUZZLE PASSED "
+ instrumenter.getClass().getSimpleName()
+ " BUT FAILURE WAS EXPECTED");
throw new RuntimeException("Instrumentation unexpectedly passed Muzzle validation");
} else if (!passed && assertPass) {
System.err.println(
"FAILED MUZZLE VALIDATION: " + instrumenter.getClass().getName() + " mismatches:");
for (Reference.Mismatch mismatch : mismatches) {
for (final Reference.Mismatch mismatch : mismatches) {
System.err.println("-- " + mismatch);
}
throw new RuntimeException("Instrumentation failed Muzzle validation");
@ -95,7 +98,7 @@ public class MuzzleVersionScanPlugin {
new HelperInjector(helperClassNames).transform(null, null, cl, null);
}
}
} catch (Exception e) {
} catch (final Exception e) {
System.err.println(
"FAILED HELPER INJECTION. Are Helpers being injected in the correct order?");
throw e;
@ -105,7 +108,7 @@ public class MuzzleVersionScanPlugin {
}
public static void printMuzzleReferences() {
for (Instrumenter instrumenter :
for (final Instrumenter instrumenter :
ServiceLoader.load(Instrumenter.class, MuzzleGradlePlugin.class.getClassLoader())) {
if (instrumenter instanceof Instrumenter.Default) {
try {
@ -119,10 +122,10 @@ public class MuzzleVersionScanPlugin {
getMuzzleMethod.setAccessible(false);
}
System.out.println(instrumenter.getClass().getName());
for (Reference ref : muzzle.getReferences()) {
for (final Reference ref : muzzle.getReferences()) {
System.out.println(prettyPrint(" ", ref));
}
} catch (Exception e) {
} catch (final Exception e) {
System.out.println(
"Unexpected exception printing references for " + instrumenter.getClass().getName());
throw new RuntimeException(e);
@ -136,14 +139,14 @@ public class MuzzleVersionScanPlugin {
}
}
private static String prettyPrint(String prefix, Reference ref) {
private static String prettyPrint(final String prefix, final Reference ref) {
final StringBuilder builder = new StringBuilder(prefix).append(ref.getClassName());
if (ref.getSuperName() != null) {
builder.append(" extends<").append(ref.getSuperName()).append(">");
}
if (ref.getInterfaces().size() > 0) {
builder.append(" implements ");
for (String iface : ref.getInterfaces()) {
for (final String iface : ref.getInterfaces()) {
builder.append(" <").append(iface).append(">");
}
}
@ -154,7 +157,7 @@ public class MuzzleVersionScanPlugin {
for (final Reference.Field field : ref.getFields()) {
builder.append("\n").append(prefix).append(prefix);
builder.append("Field: ");
for (Reference.Flag flag : field.getFlags()) {
for (final Reference.Flag flag : field.getFlags()) {
builder.append(flag).append(" ");
}
builder.append(field.toString());
@ -162,7 +165,7 @@ public class MuzzleVersionScanPlugin {
for (final Reference.Method method : ref.getMethods()) {
builder.append("\n").append(prefix).append(prefix);
builder.append("Method: ");
for (Reference.Flag flag : method.getFlags()) {
for (final Reference.Flag flag : method.getFlags()) {
builder.append(flag).append(" ");
}
builder.append(method.toString());

View File

@ -22,13 +22,13 @@ public class Reference {
private final Set<Method> methods;
private Reference(
Set<Source> sources,
Set<Flag> flags,
String className,
String superName,
Set<String> interfaces,
Set<Field> fields,
Set<Method> methods) {
final Set<Source> sources,
final Set<Flag> flags,
final String className,
final String superName,
final Set<String> interfaces,
final Set<Field> fields,
final Set<Method> methods) {
this.sources = sources;
this.flags = flags;
this.className = Utils.getClassName(className);
@ -59,11 +59,11 @@ public class Reference {
}
public Set<Method> getMethods() {
return this.methods;
return methods;
}
public Set<Field> getFields() {
return this.fields;
return fields;
}
/**
@ -72,11 +72,11 @@ public class Reference {
* @param anotherReference A reference to the same class
* @return a new Reference which merges the two references
*/
public Reference merge(Reference anotherReference) {
public Reference merge(final Reference anotherReference) {
if (!anotherReference.getClassName().equals(className)) {
throw new IllegalStateException("illegal merge " + this + " != " + anotherReference);
}
String superName = null == this.superName ? anotherReference.superName : this.superName;
final String superName = null == this.superName ? anotherReference.superName : this.superName;
return new Reference(
merge(sources, anotherReference.sources),
@ -88,17 +88,17 @@ public class Reference {
mergeMethods(methods, anotherReference.methods));
}
private static <T> Set<T> merge(Set<T> set1, Set<T> set2) {
private static <T> Set<T> merge(final Set<T> set1, final Set<T> set2) {
final Set<T> set = new HashSet<>();
set.addAll(set1);
set.addAll(set2);
return set;
}
private static Set<Method> mergeMethods(Set<Method> methods1, Set<Method> methods2) {
List<Method> merged = new ArrayList<>(methods1);
for (Method method : methods2) {
int i = merged.indexOf(method);
private static Set<Method> mergeMethods(final Set<Method> methods1, final Set<Method> methods2) {
final List<Method> merged = new ArrayList<>(methods1);
for (final Method method : methods2) {
final int i = merged.indexOf(method);
if (i == -1) {
merged.add(method);
} else {
@ -108,10 +108,10 @@ public class Reference {
return new HashSet<>(merged);
}
private static Set<Field> mergeFields(Set<Field> fields1, Set<Field> fields2) {
List<Field> merged = new ArrayList<>(fields1);
for (Field field : fields2) {
int i = merged.indexOf(field);
private static Set<Field> mergeFields(final Set<Field> fields1, final Set<Field> fields2) {
final List<Field> merged = new ArrayList<>(fields1);
for (final Field field : fields2) {
final int i = merged.indexOf(field);
if (i == -1) {
merged.add(field);
} else {
@ -121,8 +121,8 @@ public class Reference {
return new HashSet<>(merged);
}
private static Set<Flag> mergeFlags(Set<Flag> flags1, Set<Flag> flags2) {
Set<Flag> merged = merge(flags1, flags2);
private static Set<Flag> mergeFlags(final Set<Flag> flags1, final Set<Flag> flags2) {
final Set<Flag> merged = merge(flags1, flags2);
// TODO: Assert flags are non-contradictory and resolve
// public > protected > package-private > private
return merged;
@ -137,7 +137,7 @@ public class Reference {
private final String name;
private final int line;
public Source(String name, int line) {
public Source(final String name, final int line) {
this.name = name;
this.line = line;
}
@ -156,9 +156,9 @@ public class Reference {
}
@Override
public boolean equals(Object o) {
public boolean equals(final Object o) {
if (o instanceof Source) {
Source other = (Source) o;
final Source other = (Source) o;
return name.equals(other.name) && line == other.line;
}
return false;
@ -180,7 +180,7 @@ public class Reference {
/** Instrumentation sources which caused the mismatch. */
private final Source[] mismatchSources;
Mismatch(Source[] mismatchSources) {
Mismatch(final Source[] mismatchSources) {
this.mismatchSources = mismatchSources;
}
@ -199,7 +199,7 @@ public class Reference {
public static class MissingClass extends Mismatch {
private final String className;
public MissingClass(Source[] sources, String className) {
public MissingClass(final Source[] sources, final String className) {
super(sources);
this.className = className;
}
@ -216,7 +216,10 @@ public class Reference {
private final int foundAccess;
public MissingFlag(
Source[] sources, String classMethodOrFieldDesc, Flag expectedFlag, int foundAccess) {
final Source[] sources,
final String classMethodOrFieldDesc,
final Flag expectedFlag,
final int foundAccess) {
super(sources);
this.classMethodOrFieldDesc = classMethodOrFieldDesc;
this.expectedFlag = expectedFlag;
@ -236,9 +239,11 @@ public class Reference {
private final ClassLoader classLoaderBeingChecked;
public ReferenceCheckError(
Exception e, Reference referenceBeingChecked, ClassLoader classLoaderBeingChecked) {
final Exception e,
final Reference referenceBeingChecked,
final ClassLoader classLoaderBeingChecked) {
super(new Source[0]);
this.referenceCheckException = e;
referenceCheckException = e;
this.referenceBeingChecked = referenceBeingChecked;
this.classLoaderBeingChecked = classLoaderBeingChecked;
}
@ -263,7 +268,11 @@ public class Reference {
private final String fieldName;
private final String fieldDesc;
public MissingField(Source[] sources, String className, String fieldName, String fieldDesc) {
public MissingField(
final Source[] sources,
final String className,
final String fieldName,
final String fieldDesc) {
super(sources);
this.className = className;
this.fieldName = fieldName;
@ -280,7 +289,7 @@ public class Reference {
private final String className;
private final String method;
public MissingMethod(Source[] sources, String className, String method) {
public MissingMethod(final Source[] sources, final String className, final String method) {
super(sources);
this.className = className;
this.method = method;
@ -297,7 +306,7 @@ public class Reference {
public enum Flag {
PUBLIC {
@Override
public boolean supersedes(Flag anotherFlag) {
public boolean supersedes(final Flag anotherFlag) {
switch (anotherFlag) {
case PRIVATE_OR_HIGHER:
case PROTECTED_OR_HIGHER:
@ -309,112 +318,112 @@ public class Reference {
}
@Override
public boolean matches(int asmFlags) {
public boolean matches(final int asmFlags) {
return (Opcodes.ACC_PUBLIC & asmFlags) != 0;
}
},
PACKAGE_OR_HIGHER {
@Override
public boolean supersedes(Flag anotherFlag) {
public boolean supersedes(final Flag anotherFlag) {
return anotherFlag == PRIVATE_OR_HIGHER;
}
@Override
public boolean matches(int asmFlags) {
public boolean matches(final int asmFlags) {
return (Opcodes.ACC_PUBLIC & asmFlags) != 0
|| ((Opcodes.ACC_PRIVATE & asmFlags) == 0 && (Opcodes.ACC_PROTECTED & asmFlags) == 0);
}
},
PROTECTED_OR_HIGHER {
@Override
public boolean supersedes(Flag anotherFlag) {
public boolean supersedes(final Flag anotherFlag) {
return anotherFlag == PRIVATE_OR_HIGHER;
}
@Override
public boolean matches(int asmFlags) {
public boolean matches(final int asmFlags) {
return PUBLIC.matches(asmFlags) || (Opcodes.ACC_PROTECTED & asmFlags) != 0;
}
},
PRIVATE_OR_HIGHER {
@Override
public boolean matches(int asmFlags) {
public boolean matches(final int asmFlags) {
// you can't out-private a private
return true;
}
},
NON_FINAL {
@Override
public boolean contradicts(Flag anotherFlag) {
public boolean contradicts(final Flag anotherFlag) {
return anotherFlag == FINAL;
}
@Override
public boolean matches(int asmFlags) {
public boolean matches(final int asmFlags) {
return (Opcodes.ACC_FINAL & asmFlags) == 0;
}
},
FINAL {
@Override
public boolean contradicts(Flag anotherFlag) {
public boolean contradicts(final Flag anotherFlag) {
return anotherFlag == NON_FINAL;
}
@Override
public boolean matches(int asmFlags) {
public boolean matches(final int asmFlags) {
return (Opcodes.ACC_FINAL & asmFlags) != 0;
}
},
STATIC {
@Override
public boolean contradicts(Flag anotherFlag) {
public boolean contradicts(final Flag anotherFlag) {
return anotherFlag == NON_STATIC;
}
@Override
public boolean matches(int asmFlags) {
public boolean matches(final int asmFlags) {
return (Opcodes.ACC_STATIC & asmFlags) != 0;
}
},
NON_STATIC {
@Override
public boolean contradicts(Flag anotherFlag) {
public boolean contradicts(final Flag anotherFlag) {
return anotherFlag == STATIC;
}
@Override
public boolean matches(int asmFlags) {
public boolean matches(final int asmFlags) {
return (Opcodes.ACC_STATIC & asmFlags) == 0;
}
},
INTERFACE {
@Override
public boolean contradicts(Flag anotherFlag) {
public boolean contradicts(final Flag anotherFlag) {
return anotherFlag == NON_INTERFACE;
}
@Override
public boolean matches(int asmFlags) {
public boolean matches(final int asmFlags) {
return (Opcodes.ACC_INTERFACE & asmFlags) != 0;
}
},
NON_INTERFACE {
@Override
public boolean contradicts(Flag anotherFlag) {
public boolean contradicts(final Flag anotherFlag) {
return anotherFlag == INTERFACE;
}
@Override
public boolean matches(int asmFlags) {
public boolean matches(final int asmFlags) {
return (Opcodes.ACC_INTERFACE & asmFlags) == 0;
}
};
public boolean contradicts(Flag anotherFlag) {
public boolean contradicts(final Flag anotherFlag) {
return false;
}
public boolean supersedes(Flag anotherFlag) {
public boolean supersedes(final Flag anotherFlag) {
return false;
}
@ -428,7 +437,7 @@ public class Reference {
private final Type returnType;
private final List<Type> parameterTypes;
public Method(String name, String descriptor) {
public Method(final String name, final String descriptor) {
this(
new Source[0],
new Flag[0],
@ -438,7 +447,11 @@ public class Reference {
}
public Method(
Source[] sources, Flag[] flags, String name, Type returnType, Type[] parameterTypes) {
final Source[] sources,
final Flag[] flags,
final String name,
final Type returnType,
final Type[] parameterTypes) {
this(
new HashSet<>(Arrays.asList(sources)),
new HashSet<>(Arrays.asList(flags)),
@ -448,11 +461,11 @@ public class Reference {
}
public Method(
Set<Source> sources,
Set<Flag> flags,
String name,
Type returnType,
List<Type> parameterTypes) {
final Set<Source> sources,
final Set<Flag> flags,
final String name,
final Type returnType,
final List<Type> parameterTypes) {
this.sources = sources;
this.flags = flags;
this.name = name;
@ -480,8 +493,8 @@ public class Reference {
return parameterTypes;
}
public Method merge(Method anotherMethod) {
if (!this.equals(anotherMethod)) {
public Method merge(final Method anotherMethod) {
if (!equals(anotherMethod)) {
throw new IllegalStateException("illegal merge " + this + " != " + anotherMethod);
}
@ -506,7 +519,7 @@ public class Reference {
}
@Override
public boolean equals(Object o) {
public boolean equals(final Object o) {
if (o instanceof Method) {
final Method m = (Method) o;
return name.equals(m.name) && getDescriptor().equals(m.getDescriptor());
@ -526,11 +539,12 @@ public class Reference {
private final String name;
private final Type type;
public Field(Source[] sources, Flag[] flags, String name, Type fieldType) {
public Field(
final Source[] sources, final Flag[] flags, final String name, final Type fieldType) {
this.sources = new HashSet<>(Arrays.asList(sources));
this.flags = new HashSet<>(Arrays.asList(flags));
this.name = name;
this.type = fieldType;
type = fieldType;
}
public String getName() {
@ -549,8 +563,8 @@ public class Reference {
return type;
}
public Field merge(Field anotherField) {
if (!this.equals(anotherField) || (!type.equals(anotherField.type))) {
public Field merge(final Field anotherField) {
if (!equals(anotherField) || !type.equals(anotherField.type)) {
throw new IllegalStateException("illegal merge " + this + " != " + anotherField);
}
return new Field(
@ -566,9 +580,9 @@ public class Reference {
}
@Override
public boolean equals(Object o) {
public boolean equals(final Object o) {
if (o instanceof Field) {
Field other = (Field) o;
final Field other = (Field) o;
return name.equals(other.name);
}
return false;
@ -593,30 +607,33 @@ public class Reference {
this.className = className;
}
public Builder withSuperName(String superName) {
public Builder withSuperName(final String superName) {
this.superName = superName;
return this;
}
public Builder withInterface(String interfaceName) {
public Builder withInterface(final String interfaceName) {
interfaces.add(interfaceName);
return this;
}
public Builder withSource(String sourceName, int line) {
public Builder withSource(final String sourceName, final int line) {
sources.add(new Source(sourceName, line));
return this;
}
public Builder withFlag(Flag flag) {
public Builder withFlag(final Flag flag) {
flags.add(flag);
return this;
}
public Builder withField(
Source[] sources, Flag[] fieldFlags, String fieldName, Type fieldType) {
final Source[] sources,
final Flag[] fieldFlags,
final String fieldName,
final Type fieldType) {
final Field field = new Field(sources, fieldFlags, fieldName, fieldType);
int existingIndex = fields.indexOf(field);
final int existingIndex = fields.indexOf(field);
if (existingIndex == -1) {
fields.add(field);
} else {
@ -626,13 +643,13 @@ public class Reference {
}
public Builder withMethod(
Source[] sources,
Flag[] methodFlags,
String methodName,
Type returnType,
Type... methodArgs) {
final Source[] sources,
final Flag[] methodFlags,
final String methodName,
final Type returnType,
final Type... methodArgs) {
final Method method = new Method(sources, methodFlags, methodName, returnType, methodArgs);
int existingIndex = methods.indexOf(method);
final int existingIndex = methods.indexOf(method);
if (existingIndex == -1) {
methods.add(method);
} else {

View File

@ -21,6 +21,7 @@ import io.opentracing.noop.NoopScopeManager.NoopScope;
import io.opentracing.tag.Tags;
import io.opentracing.util.GlobalTracer;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collections;
@ -92,12 +93,13 @@ public final class PreparedStatementInstrumentation extends Instrumenter.Default
{
if (dbInfo == null) {
try {
final String url = connection.getMetaData().getURL();
final DatabaseMetaData metaData = connection.getMetaData();
final String url = metaData.getURL();
if (url != null) {
// Remove end of url to prevent passwords from leaking:
final String sanitizedURL = url.replaceAll("[?;].*", "");
final String type = url.split(":", -1)[1];
String user = connection.getMetaData().getUserName();
String user = metaData.getUserName();
if (user != null && user.trim().equals("")) {
user = null;
}

View File

@ -21,6 +21,7 @@ import io.opentracing.noop.NoopScopeManager.NoopScope;
import io.opentracing.tag.Tags;
import io.opentracing.util.GlobalTracer;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collections;
@ -93,12 +94,13 @@ public final class StatementInstrumentation extends Instrumenter.Default {
{
if (dbInfo == null) {
try {
final String url = connection.getMetaData().getURL();
final DatabaseMetaData metaData = connection.getMetaData();
final String url = metaData.getURL();
if (url != null) {
// Remove end of url to prevent passwords from leaking:
final String sanitizedURL = url.replaceAll("[?;].*", "");
final String type = url.split(":", -1)[1];
String user = connection.getMetaData().getUserName();
String user = metaData.getUserName();
if (user != null && user.trim().equals("")) {
user = null;
}

View File

@ -56,7 +56,7 @@ public class LettuceReactiveCommandsInstrumentation extends Instrumenter.Default
.and(nameStartsWith("create"))
.and(nameEndsWith("Flux"))
.and(takesArgument(0, named("java.util.function.Supplier")))
.and(returns(named(("reactor.core.publisher.Flux")))),
.and(returns(named("reactor.core.publisher.Flux"))),
// Cannot reference class directly here because it would lead to class load failure on Java7
PACKAGE + ".rx.LettuceFluxCreationAdvice");