From 0d531ecc4b4268f81ae2dcf239bdbe34dd52cf5b Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Thu, 13 Sep 2012 14:42:37 +0200 Subject: [PATCH] Added a custom exception. Simplified the event API. --- .../protocol/events/PacketEvent.java | 33 +++++------- .../reflect/FieldAccessException.java | 34 ++++++++++++ .../protocol/reflect/StructureModifier.java | 52 ++++++++++++------- 3 files changed, 78 insertions(+), 41 deletions(-) create mode 100644 ProtocolLib/src/com/comphenix/protocol/reflect/FieldAccessException.java diff --git a/ProtocolLib/src/com/comphenix/protocol/events/PacketEvent.java b/ProtocolLib/src/com/comphenix/protocol/events/PacketEvent.java index e403f658..d04fb8ec 100644 --- a/ProtocolLib/src/com/comphenix/protocol/events/PacketEvent.java +++ b/ProtocolLib/src/com/comphenix/protocol/events/PacketEvent.java @@ -11,8 +11,8 @@ public class PacketEvent extends EventObject { private static final long serialVersionUID = -5360289379097430620L; private PacketContainer packet; - private Player sender; - private Player reciever; + private Player player; + private boolean serverPacket; private boolean cancel; /** @@ -23,11 +23,11 @@ public class PacketEvent extends EventObject { super(source); } - private PacketEvent(Object source, PacketContainer packet, Player sender, Player reciever) { + private PacketEvent(Object source, PacketContainer packet, Player player, boolean serverPacket) { super(source); this.packet = packet; - this.sender = sender; - this.reciever = reciever; + this.player = player; + this.serverPacket = serverPacket; } /** @@ -38,7 +38,7 @@ public class PacketEvent extends EventObject { * @return The event. */ public static PacketEvent fromClient(Object source, PacketContainer packet, Player client) { - return new PacketEvent(source, packet, client, null); + return new PacketEvent(source, packet, client, false); } /** @@ -49,7 +49,7 @@ public class PacketEvent extends EventObject { * @return The event. */ public static PacketEvent fromServer(Object source, PacketContainer packet, Player recipient) { - return new PacketEvent(source, packet, null, recipient); + return new PacketEvent(source, packet, recipient, true); } /** @@ -93,27 +93,18 @@ public class PacketEvent extends EventObject { } /** - * Retrieves the player that has sent the packet. - * @return The sender, or NULL if the server is sending the packet. + * Retrieves the player that has sent the packet or is recieving it. + * @return The player associated with this event. */ - public Player getSender() { - return sender; + public Player getPlayer() { + return player; } - /** - * Retrieves the player that will recieve the packet. - * @return The reciever, or NULL if the server is recieving the packet. - */ - public Player getReciever() { - return reciever; - } - - /** * Whether or not this packet was created by the server. * @return TRUE if the packet was created by the server, FALSE if it was created by a client. */ public boolean isServerPacket() { - return getReciever() != null; + return serverPacket; } } diff --git a/ProtocolLib/src/com/comphenix/protocol/reflect/FieldAccessException.java b/ProtocolLib/src/com/comphenix/protocol/reflect/FieldAccessException.java new file mode 100644 index 00000000..f83f75bc --- /dev/null +++ b/ProtocolLib/src/com/comphenix/protocol/reflect/FieldAccessException.java @@ -0,0 +1,34 @@ +package com.comphenix.protocol.reflect; + +/** + * Invoked when a field is inaccessible due to security limitations, or when it simply doesn't exist. + * + * @author Kristian + */ +public class FieldAccessException extends Exception { + + /** + * Generated by Eclipse. + */ + private static final long serialVersionUID = 1911011681494034617L; + + public FieldAccessException() { + super(); + } + + public FieldAccessException(String message, Throwable cause) { + super(message, cause); + } + + public FieldAccessException(String message) { + super(message); + } + + public FieldAccessException(Throwable cause) { + super(cause); + } + + public static FieldAccessException fromFormat(String message, Object... params) { + return new FieldAccessException(String.format(message, params)); + } +} diff --git a/ProtocolLib/src/com/comphenix/protocol/reflect/StructureModifier.java b/ProtocolLib/src/com/comphenix/protocol/reflect/StructureModifier.java index 39d1b0ce..fd67a58e 100644 --- a/ProtocolLib/src/com/comphenix/protocol/reflect/StructureModifier.java +++ b/ProtocolLib/src/com/comphenix/protocol/reflect/StructureModifier.java @@ -71,31 +71,37 @@ public class StructureModifier { * Reads the value of a field given its index. * @param fieldIndex - index of the field. * @return Value of the field. - * @throws IllegalAccessException If we're unable to read the field due to a security limitation. + * @throws FieldAccessException The field doesn't exist, or it cannot be accessed under the current security contraints. */ @SuppressWarnings("unchecked") - public TField read(int fieldIndex) throws IllegalAccessException { + public TField read(int fieldIndex) throws FieldAccessException { if (fieldIndex < 0 || fieldIndex >= data.size()) - throw new IllegalArgumentException("Field index must be within 0 - count"); + throw new FieldAccessException("Field index must be within 0 - count", + new IndexOutOfBoundsException("Out of bounds")); if (target == null) throw new IllegalStateException("Cannot read from a NULL target."); - Object result = FieldUtils.readField(data.get(fieldIndex), target, true); - - // Use the converter, if we have it - if (converter != null) - return converter.getSpecific(result); - else - return (TField) result; + try { + Object result = FieldUtils.readField(data.get(fieldIndex), target, true); + + // Use the converter, if we have it + if (converter != null) + return converter.getSpecific(result); + else + return (TField) result; + + } catch (IllegalAccessException e) { + throw new FieldAccessException("Cannot read field due to a security limitation.", e); + } } /** * Reads the value of a field if and ONLY IF it exists. * @param fieldIndex - index of the field. * @return Value of the field, or NULL if it doesn't exist. - * @throws IllegalAccessException If we're unable to read the field due to a security limitation. + * @throws FieldAccessException The field cannot be accessed under the current security contraints. */ - public TField readSafely(int fieldIndex) throws IllegalAccessException { + public TField readSafely(int fieldIndex) throws FieldAccessException { if (fieldIndex >= 0 && fieldIndex < data.size()) { return read(fieldIndex); } else { @@ -108,17 +114,23 @@ public class StructureModifier { * @param fieldIndex - index of the field. * @param value - new value of the field. * @return This structure modifier - for chaining. - * @throws IllegalAccessException If we're unable to write to the field due to a security limitation. + * @throws IllegalAccessException The field doesn't exist, or it cannot be accessed under the current security contraints. */ - public StructureModifier write(int fieldIndex, TField value) throws IllegalAccessException { + public StructureModifier write(int fieldIndex, TField value) throws FieldAccessException { if (fieldIndex < 0 || fieldIndex >= data.size()) - throw new IllegalArgumentException("Field index must be within 0 - count"); + throw new FieldAccessException("Field index must be within 0 - count", + new IndexOutOfBoundsException("Out of bounds")); if (target == null) throw new IllegalStateException("Cannot write to a NULL target."); // Use the converter, if it exists Object obj = converter != null ? converter.getGeneric(value) : value; - FieldUtils.writeField(data.get(fieldIndex), target, obj, true); + + try { + FieldUtils.writeField(data.get(fieldIndex), target, obj, true); + } catch (IllegalAccessException e) { + throw new FieldAccessException("Cannot read field due to a security limitation.", e); + } // Make this method chainable return this; @@ -129,9 +141,9 @@ public class StructureModifier { * @param fieldIndex - index of the potential field. * @param value - new value of the field. * @return This structure modifer - for chaining. - * @throws IllegalAccessException If we're unable to write to the field due to a security limitation. + * @throws IllegalAccessException The field cannot be accessed under the current security contraints. */ - public StructureModifier writeSafely(int fieldIndex, TField value) throws IllegalAccessException { + public StructureModifier writeSafely(int fieldIndex, TField value) throws FieldAccessException { if (fieldIndex >= 0 && fieldIndex < data.size()) { write(fieldIndex, value); } @@ -143,9 +155,9 @@ public class StructureModifier { * @param fieldIndex - index of the field to modify. * @param select - the function that modifies the field value. * @return This structure modifier - for chaining. - * @throws IllegalAccessException + * @throws IllegalAccessException The field cannot be accessed under the current security contraints. */ - public StructureModifier modify(int fieldIndex, Function select) throws IllegalAccessException { + public StructureModifier modify(int fieldIndex, Function select) throws FieldAccessException { TField value = read(fieldIndex); return write(fieldIndex, select.apply(value)); }