Archiviert
13
0

Improve the toString()-functionality of each fuzzy contract.

Dieser Commit ist enthalten in:
Kristian S. Stangeland 2013-02-03 04:08:30 +01:00
Ursprung 58969b26f6
Commit 75f05732bb
6 geänderte Dateien mit 116 neuen und 2 gelöschten Zeilen

Datei anzeigen

@ -39,7 +39,6 @@ import com.comphenix.protocol.injector.ListenerInvoker;
import com.comphenix.protocol.injector.PlayerLoggedOutException;
import com.comphenix.protocol.injector.PacketFilterManager.PlayerInjectHooks;
import com.comphenix.protocol.injector.player.TemporaryPlayerFactory.InjectContainer;
import com.comphenix.protocol.utility.MinecraftReflection;
import com.google.common.base.Predicate;
import com.google.common.collect.Maps;

Datei anzeigen

@ -1,10 +1,13 @@
package com.comphenix.protocol.reflect.fuzzy;
import java.lang.reflect.Member;
import java.util.Map;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;
import com.google.common.collect.Maps;
/**
* Represents a matcher that matches members.
*
@ -201,12 +204,17 @@ public abstract class AbstractFuzzyMember<T extends Member> extends AbstractFuzz
int mods = value.getModifiers();
// Match accessibility and name
return (mods & modifiersRequired) != 0 &&
return (mods & modifiersRequired) == modifiersRequired &&
(mods & modifiersBanned) == 0 &&
declaringMatcher.isMatch(value.getDeclaringClass(), value) &&
isNameMatch(value.getName());
}
/**
* Determine if a given name matches the current member matcher.
* @param name - the name to match.
* @return TRUE if the name matches, FALSE otherwise.
*/
private boolean isNameMatch(String name) {
if (nameRegex == null)
return true;
@ -223,4 +231,44 @@ public abstract class AbstractFuzzyMember<T extends Member> extends AbstractFuzz
// NULL is zero
return declaringMatcher.getRoundNumber();
}
@Override
public String toString() {
return getKeyValueView().toString();
}
/**
* Generate a view of this matcher as a key-value map.
* <p>
* Used by {@link #toString()} to print a representation of this object.
* @return A modifiable key-value view.
*/
protected Map<String, Object> getKeyValueView() {
Map<String, Object> map = Maps.newLinkedHashMap();
// Build our representation
if (modifiersRequired != Integer.MAX_VALUE || modifiersBanned != 0) {
map.put("modifiers", String.format("[required: %s, banned: %s]",
getBitView(modifiersRequired, 16),
getBitView(modifiersBanned, 16))
);
}
if (nameRegex != null) {
map.put("name", nameRegex.pattern());
}
if (declaringMatcher != ExactClassMatcher.MATCH_ALL) {
map.put("declaring", declaringMatcher);
}
return map;
}
private static String getBitView(int value, int bits) {
if (bits < 0 || bits > 31)
throw new IllegalArgumentException("Bits must be a value between 0 and 32");
// Extract our needed bits
int snipped = value & ((1 << bits) - 1);
return Integer.toBinaryString(snipped);
}
}

Datei anzeigen

@ -4,11 +4,14 @@ import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.comphenix.protocol.reflect.FuzzyReflection;
import com.comphenix.protocol.reflect.MethodInfo;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
/**
* Determine if a given class implements a given fuzzy (duck typed) contract.
@ -218,4 +221,20 @@ public class FuzzyClassContract extends AbstractFuzzyMatcher<Class<?>> {
// Failure
return -1;
}
@Override
public String toString() {
Map<String, Object> params = Maps.newLinkedHashMap();
if (fieldContracts.size() > 0) {
params.put("fields", fieldContracts);
}
if (methodContracts.size() > 0) {
params.put("methods", methodContracts);
}
if (constructorContracts.size() > 0) {
params.put("constructors", constructorContracts);
}
return "{\n " + Joiner.on(", \n ").join(params.entrySet()) + "\n}";
}
}

Datei anzeigen

@ -1,6 +1,7 @@
package com.comphenix.protocol.reflect.fuzzy;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;
@ -150,4 +151,14 @@ public class FuzzyFieldContract extends AbstractFuzzyMember<Field> {
return combineRounds(super.calculateRoundNumber(),
typeMatcher.calculateRoundNumber());
}
@Override
protected Map<String, Object> getKeyValueView() {
Map<String, Object> member = super.getKeyValueView();
if (typeMatcher != ExactClassMatcher.MATCH_ALL) {
member.put("type", typeMatcher);
}
return member;
}
}

Datei anzeigen

@ -4,6 +4,7 @@ import java.lang.reflect.Member;
import java.util.Set;
import java.util.regex.Pattern;
import com.google.common.base.Joiner;
import com.google.common.collect.Sets;
/**
@ -56,6 +57,11 @@ public class FuzzyMatchers {
}
return roundNumber;
}
@Override
public String toString() {
return String.format("match any: %s", Joiner.on(",").join(classes));
}
};
}
@ -97,6 +103,11 @@ public class FuzzyMatchers {
protected int calculateRoundNumber() {
return -priority;
}
@Override
public String toString() {
return "class name of " + regex.toString();
}
};
}
@ -133,6 +144,11 @@ public class FuzzyMatchers {
// We match a very specific type
return -100;
}
@Override
public String toString() {
return "match parent class";
}
};
}
}

Datei anzeigen

@ -2,6 +2,7 @@ package com.comphenix.protocol.reflect.fuzzy;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;
@ -493,4 +494,24 @@ public class FuzzyMethodContract extends AbstractFuzzyMember<MethodInfo> {
return combineRounds(super.calculateRoundNumber(), current);
}
@Override
protected Map<String, Object> getKeyValueView() {
Map<String, Object> member = super.getKeyValueView();
// Only add fields that are actual contraints
if (returnMatcher != ExactClassMatcher.MATCH_ALL) {
member.put("return", returnMatcher);
}
if (paramMatchers.size() > 0) {
member.put("params", paramMatchers);
}
if (exceptionMatchers.size() > 0) {
member.put("exceptions", exceptionMatchers);
}
if (paramCount != null) {
member.put("paramCount", paramCount);
}
return member;
}
}