Skip to content

Commit

Permalink
Remove finalize, use a Cleaner instead
Browse files Browse the repository at this point in the history
  • Loading branch information
octylFractal committed Nov 16, 2021
1 parent ff29595 commit da3d684
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,7 @@ private Extent wrapExtent(Extent extent, EventBus eventBus, EditSessionEvent eve
return event.getExtent();
}

// pkg private for TracedEditSession only, may later become public API
boolean commitRequired() {
private boolean commitRequired() {
if (reorderExtent != null && reorderExtent.commitRequired()) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,59 @@

import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extent.inventory.BlockBag;
import com.sk89q.worldedit.internal.util.ErrorReporting;
import com.sk89q.worldedit.util.eventbus.EventBus;
import com.sk89q.worldedit.world.World;

import java.lang.ref.Cleaner;
import javax.annotation.Nullable;

/**
* Internal use only.
*/
class TracedEditSession extends EditSession {

private static final Cleaner cleaner = Cleaner.create();

private static final class TraceRecord implements Runnable {
private final Throwable stacktrace = new Throwable("An EditSession was not closed.");
private final Actor actor;

private volatile boolean committed = false;

private TraceRecord(Actor actor) {
this.actor = actor;
}

@Override
public void run() {
if (!committed) {
WorldEdit.logger.warn("####### EDIT SESSION NOT CLOSED #######");
WorldEdit.logger.warn("This means that some code did not close their EditSession.");
WorldEdit.logger.warn("Here is a stacktrace from the creation of this EditSession:", stacktrace);
ErrorReporting.trigger(actor, stacktrace);
}
}
}

private final TraceRecord record;
private final Cleaner.Cleanable cleanable;

TracedEditSession(EventBus eventBus, @Nullable World world, int maxBlocks, @Nullable BlockBag blockBag,
@Nullable Actor actor,
boolean tracing) {
super(eventBus, world, maxBlocks, blockBag, actor, tracing);
this.record = new TraceRecord(actor);
this.cleanable = cleaner.register(this, record);
}

private final Throwable stacktrace = new Throwable("Creation trace.");

@Override
protected void finalize() throws Throwable {
super.finalize();

if (commitRequired()) {
WorldEdit.logger.warn("####### LEFTOVER BUFFER BLOCKS DETECTED #######");
WorldEdit.logger.warn("This means that some code did not flush their EditSession.");
WorldEdit.logger.warn("Here is a stacktrace from the creation of this EditSession:", stacktrace);
public void close() {
try {
super.close();
} finally {
this.record.committed = true;
cleanable.clean();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package com.sk89q.worldedit.command.tool;

import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.entity.Player;
Expand Down Expand Up @@ -53,9 +52,8 @@ public boolean canUse(Actor player) {
public boolean actPrimary(Platform server, LocalConfiguration config, Player player, LocalSession session, Location clicked, @Nullable Direction face) {

World world = (World) clicked.getExtent();
EditSession editSession = session.createEditSession(player);
BlockVector3 blockPoint = clicked.toVector().toBlockPoint();
BaseBlock block = editSession.getFullBlock(blockPoint);
BaseBlock block = player.getExtent().getFullBlock(blockPoint);

TextComponent.Builder builder = TextComponent.builder();
builder.append(TextComponent.of("@" + clicked.toVector().toBlockPoint() + ": ", TextColor.BLUE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
import com.sk89q.worldedit.internal.command.CommandRegistrationHandler;
import com.sk89q.worldedit.internal.command.exception.ExceptionConverter;
import com.sk89q.worldedit.internal.command.exception.WorldEditExceptionConverter;
import com.sk89q.worldedit.internal.util.ErrorReporting;
import com.sk89q.worldedit.internal.util.LogManagerCompat;
import com.sk89q.worldedit.internal.util.Substring;
import com.sk89q.worldedit.regions.Region;
Expand Down Expand Up @@ -591,9 +592,8 @@ private MemoizingValueAccess initializeInjectedValues(Arguments arguments, Actor
}

private void handleUnknownException(Actor actor, Throwable t) {
actor.printError(TranslatableComponent.of("worldedit.command.error.report"));
actor.print(TextComponent.of(t.getClass().getName() + ": " + t.getMessage()));
LOGGER.error("An unexpected error while handling a WorldEdit command", t);
ErrorReporting.trigger(actor, t);
}

@Subscribe
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.sk89q.worldedit.internal.util;

import com.google.common.base.Throwables;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent;
import com.sk89q.worldedit.util.formatting.text.event.HoverEvent;

/**
* Simple class for handling error reporting to users.
*/
public class ErrorReporting {
public static void trigger(Actor actor, Throwable error) {
actor.printError(TranslatableComponent.of("worldedit.command.error.report"));
actor.print(
TextComponent.builder(error.getClass().getName() + ": " + error.getMessage())
.hoverEvent(HoverEvent.showText(TextComponent.of(Throwables.getStackTraceAsString(error))))
.build()
);
}

private ErrorReporting() {
}
}

0 comments on commit da3d684

Please sign in to comment.