Stack Trace Cut

This commit is contained in:
Olof Larsson 2016-05-18 11:05:20 +02:00
parent d4d95b5642
commit 3a894cb8a4
No known key found for this signature in database
GPG Key ID: BBEF14F97DA52474
4 changed files with 158 additions and 1 deletions

View File

@ -0,0 +1,26 @@
package com.massivecraft.massivecore.predicate;
import java.util.regex.Pattern;
public class PredicateElementGarbage extends PredicateElementRegexes
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static PredicateElementGarbage i = new PredicateElementGarbage();
public static PredicateElementGarbage get() { return i; }
public PredicateElementGarbage()
{
super(
"^" + Pattern.quote("com.massivecraft.massivelag.inject") + ".*$", // MassiveLag - Injected stuff for commands, tasks and events.
"^" + Pattern.quote("com.massivecraft.massivecore.command.MassiveCoreBukkitCommand.execute") + ".*$", // MassiveCommand - Execution Garbage
"^" + Pattern.quote("com.massivecraft.massivecore.command.MassiveCommand.execute") + ".*$", // MassiveCommand - Execution Garbage
"^" + Pattern.quote("sun.reflect") + ".*$", // Event Reflection
"^" + Pattern.quote("java.lang.reflect") + ".*$" // Event Reflection
);
}
}

View File

@ -0,0 +1,67 @@
package com.massivecraft.massivecore.predicate;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
import com.massivecraft.massivecore.collections.MassiveList;
public class PredicateElementRegexes implements Predicate<StackTraceElement>
{
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private List<Pattern> patterns = Collections.emptyList();
public List<Pattern> getPatterns() { return this.patterns; }
public void setPatterns(Collection<Pattern> patterns) { this.patterns = new MassiveList<>(patterns); }
public void setPatterns(Iterable<String> regexes) { this.setPatterns(asPatterns(regexes));}
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public PredicateElementRegexes(String... regexes)
{
this.setPatterns(Arrays.asList(regexes));
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public boolean apply(StackTraceElement element)
{
if (element == null) return false;
String string = element.toString();
for (Pattern pattern : this.getPatterns())
{
if (pattern.matcher(string).matches()) return true;
}
return false;
}
// -------------------------------------------- //
// UTIL
// -------------------------------------------- //
protected List<Pattern> asPatterns(Iterable<String> regexes)
{
// Create
List<Pattern> ret = new MassiveList<>();
// Fill
for (String regex : regexes)
{
Pattern pattern = Pattern.compile(regex);
ret.add(pattern);
}
// Return
return ret;
}
}

View File

@ -0,0 +1,29 @@
package com.massivecraft.massivecore.predicate;
import java.util.regex.Pattern;
public class PredicateElementSignificant extends PredicateElementRegexes
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static PredicateElementSignificant i = new PredicateElementSignificant();
public static PredicateElementSignificant get() { return i; }
public PredicateElementSignificant()
{
super(
// Command
"^" + Pattern.quote("org.bukkit.craftbukkit.") + ".*" +Pattern.quote(".CraftServer.dispatchCommand") + ".*$", // CraftBukkit
// Task
"^" + Pattern.quote("org.bukkit.craftbukkit.") + ".*" +Pattern.quote(".scheduler.CraftTask.run") + ".*$", // CraftBukkit
// Event
"^" + Pattern.quote("org.bukkit.plugin.RegisteredListener.callEvent") + ".*$", // Bukkit
"^" + Pattern.quote("us.Myles.PWP.TransparentListeners") + ".*$", // PerWorldPlugins <= 1.2.4
"^" + Pattern.quote("me.incomprehendable.dev.pwp.transparentlisteners") + ".*$" // PerWorldPlugins >= 1.2.5
);
}
}

View File

@ -68,6 +68,8 @@ import com.massivecraft.massivecore.engine.EngineMassiveCoreWorldNameSet;
import com.massivecraft.massivecore.mixin.MixinMessage;
import com.massivecraft.massivecore.nms.NmsEntityGet;
import com.massivecraft.massivecore.predicate.Predicate;
import com.massivecraft.massivecore.predicate.PredicateElementGarbage;
import com.massivecraft.massivecore.predicate.PredicateElementSignificant;
import com.massivecraft.massivecore.util.extractor.Extractor;
import com.massivecraft.massivecore.util.extractor.ExtractorPlayer;
import com.massivecraft.massivecore.util.extractor.ExtractorPlayerName;
@ -344,7 +346,7 @@ public class MUtil
StackTraceElement[] elements = thread.getStackTrace();
elements = Arrays.copyOfRange(elements, skip, elements.length);
return new ArrayList<StackTraceElement>(Arrays.asList(elements));
return new MassiveList<>(Arrays.asList(elements));
}
public static List<StackTraceElement> getStackTrace(Thread thread)
@ -380,6 +382,39 @@ public class MUtil
return getStackTrace(thread, skip);
}
// -------------------------------------------- //
// STACK TRACE: CUT
// -------------------------------------------- //
public static void cutStackTrace(List<StackTraceElement> trace)
{
// Cut Significant
int index = 0;
while (index < trace.size())
{
StackTraceElement element = trace.get(index);
if (PredicateElementSignificant.get().apply(element)) break;
index++;
}
trace.subList(index, trace.size()).clear();
// Reverse
Collections.reverse(trace);
// Eat Garbage
for (Iterator<StackTraceElement> iterator = trace.iterator(); iterator.hasNext();)
{
StackTraceElement element = iterator.next();
if (PredicateElementGarbage.get().apply(element))
{
iterator.remove();
}
}
// Unreverse
Collections.reverse(trace);
}
// -------------------------------------------- //
// STACK TRACE STRING: MANY
// -------------------------------------------- //