From ae08abe821f6c6ce0a5c5d8f741925ce35ec4ca3 Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Mon, 29 Oct 2012 22:15:25 +0100 Subject: [PATCH] Remove PrimitiveUtils - it's already present in Bukkit's Guava library. --- ProtocolLib/dependency-reduced-pom.xml | 2 +- .../protocol/CleanupStaticMembers.java | 4 +- .../protocol/injector/PacketConstructor.java | 6 +- .../protocol/reflect/PrimitiveUtils.java | 124 ------------------ .../protocol/reflect/StructureModifier.java | 2 +- .../reflect/compiler/StructureCompiler.java | 6 +- .../reflect/instances/PrimitiveGenerator.java | 8 +- 7 files changed, 13 insertions(+), 139 deletions(-) delete mode 100644 ProtocolLib/src/main/java/com/comphenix/protocol/reflect/PrimitiveUtils.java diff --git a/ProtocolLib/dependency-reduced-pom.xml b/ProtocolLib/dependency-reduced-pom.xml index 5fb86ef9..6b5086a3 100644 --- a/ProtocolLib/dependency-reduced-pom.xml +++ b/ProtocolLib/dependency-reduced-pom.xml @@ -4,7 +4,7 @@ com.comphenix.protocol ProtocolLib ProtocolLib - 1.5.0 + 1.5.1-SNAPSHOT Provides read/write access to the Minecraft protocol. http://dev.bukkit.org/server-mods/protocollib/ diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/CleanupStaticMembers.java b/ProtocolLib/src/main/java/com/comphenix/protocol/CleanupStaticMembers.java index 60bbbcfa..dbc2e819 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/CleanupStaticMembers.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/CleanupStaticMembers.java @@ -14,7 +14,6 @@ import com.comphenix.protocol.reflect.FieldUtils; import com.comphenix.protocol.reflect.FuzzyReflection; import com.comphenix.protocol.reflect.MethodUtils; import com.comphenix.protocol.reflect.ObjectCloner; -import com.comphenix.protocol.reflect.PrimitiveUtils; import com.comphenix.protocol.reflect.compiler.BackgroundCompiler; import com.comphenix.protocol.reflect.compiler.StructureCompiler; import com.comphenix.protocol.reflect.instances.CollectionGenerator; @@ -46,8 +45,7 @@ class CleanupStaticMembers { BukkitUnwrapper.class, DefaultInstances.class, CollectionGenerator.class, PrimitiveGenerator.class, FuzzyReflection.class, MethodUtils.class, BackgroundCompiler.class, StructureCompiler.class, - ObjectCloner.class, PrimitiveUtils.class, Packets.Server.class, - Packets.Client.class + ObjectCloner.class, Packets.Server.class, Packets.Client.class }; String[] internalClasses = { diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketConstructor.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketConstructor.java index f8e0e654..7c2917f7 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketConstructor.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketConstructor.java @@ -25,9 +25,9 @@ import net.minecraft.server.Packet; import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.reflect.FieldAccessException; -import com.comphenix.protocol.reflect.PrimitiveUtils; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; +import com.google.common.primitives.Primitives; /** * A packet constructor that uses an internal Minecraft. @@ -181,9 +181,9 @@ public class PacketConstructor { Class paramType = params[i]; // The input type is always wrapped - if (PrimitiveUtils.isPrimitive(paramType)) { + if (paramType.isPrimitive()) { // Wrap it - paramType = PrimitiveUtils.wrap(paramType); + paramType = Primitives.wrap(paramType); } // Compare assignability diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/PrimitiveUtils.java b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/PrimitiveUtils.java deleted file mode 100644 index 3597920f..00000000 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/PrimitiveUtils.java +++ /dev/null @@ -1,124 +0,0 @@ -package com.comphenix.protocol.reflect; - -/* - * Copyright (C) 2008 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import java.lang.reflect.Type; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -/** - * Contains static utility methods pertaining to primitive types and their - * corresponding wrapper types. - * - * @author Kevin Bourrillion - */ -public final class PrimitiveUtils { - private PrimitiveUtils() { - } - - /** A map from primitive types to their corresponding wrapper types. */ - private static final Map, Class> PRIMITIVE_TO_WRAPPER_TYPE; - - /** A map from wrapper types to their corresponding primitive types. */ - private static final Map, Class> WRAPPER_TO_PRIMITIVE_TYPE; - - // Sad that we can't use a BiMap. :( - static { - Map, Class> primToWrap = new HashMap, Class>(16); - Map, Class> wrapToPrim = new HashMap, Class>(16); - - add(primToWrap, wrapToPrim, boolean.class, Boolean.class); - add(primToWrap, wrapToPrim, byte.class, Byte.class); - add(primToWrap, wrapToPrim, char.class, Character.class); - add(primToWrap, wrapToPrim, double.class, Double.class); - add(primToWrap, wrapToPrim, float.class, Float.class); - add(primToWrap, wrapToPrim, int.class, Integer.class); - add(primToWrap, wrapToPrim, long.class, Long.class); - add(primToWrap, wrapToPrim, short.class, Short.class); - add(primToWrap, wrapToPrim, void.class, Void.class); - - PRIMITIVE_TO_WRAPPER_TYPE = Collections.unmodifiableMap(primToWrap); - WRAPPER_TO_PRIMITIVE_TYPE = Collections.unmodifiableMap(wrapToPrim); - } - - private static void add(Map, Class> forward, - Map, Class> backward, Class key, Class value) { - forward.put(key, value); - backward.put(value, key); - } - - /** - * Returns true if this type is a primitive. - */ - public static boolean isPrimitive(Type type) { - return PRIMITIVE_TO_WRAPPER_TYPE.containsKey(type); - } - - /** - * Returns {@code true} if {@code type} is one of the nine primitive-wrapper - * types, such as {@link Integer}. - * - * @see Class#isPrimitive - */ - public static boolean isWrapperType(Type type) { - return WRAPPER_TO_PRIMITIVE_TYPE.containsKey(checkNotNull(type)); - } - - /** - * Returns the corresponding wrapper type of {@code type} if it is a - * primitive type; otherwise returns {@code type} itself. Idempotent. - * - *
-	 *     wrap(int.class) == Integer.class
-	 *     wrap(Integer.class) == Integer.class
-	 *     wrap(String.class) == String.class
-	 * 
- */ - public static Class wrap(Class type) { - // cast is safe: long.class and Long.class are both of type Class - @SuppressWarnings("unchecked") - Class wrapped = (Class) PRIMITIVE_TO_WRAPPER_TYPE - .get(checkNotNull(type)); - return (wrapped == null) ? type : wrapped; - } - - /** - * Returns the corresponding primitive type of {@code type} if it is a - * wrapper type; otherwise returns {@code type} itself. Idempotent. - * - *
-	 *     unwrap(Integer.class) == int.class
-	 *     unwrap(int.class) == int.class
-	 *     unwrap(String.class) == String.class
-	 * 
- */ - public static Class unwrap(Class type) { - // cast is safe: long.class and Long.class are both of type Class - @SuppressWarnings("unchecked") - Class unwrapped = (Class) WRAPPER_TO_PRIMITIVE_TYPE - .get(checkNotNull(type)); - return (unwrapped == null) ? type : unwrapped; - } - - public static T checkNotNull(T obj) { - if (obj == null) { - throw new NullPointerException(); - } - return obj; - } -} diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java index f4bb07f3..2665dc21 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java @@ -426,7 +426,7 @@ public class StructureModifier { Class type = field.getType(); // First, ignore primitive fields - if (!PrimitiveUtils.isPrimitive(type)) { + if (!type.isPrimitive()) { // Next, see if we actually can generate a default value if (generator.getDefault(type) != null) { // If so, require it diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/compiler/StructureCompiler.java b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/compiler/StructureCompiler.java index fde9de08..10ceaa72 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/compiler/StructureCompiler.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/compiler/StructureCompiler.java @@ -25,9 +25,9 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import com.comphenix.protocol.reflect.PrimitiveUtils; import com.comphenix.protocol.reflect.StructureModifier; import com.google.common.base.Objects; +import com.google.common.primitives.Primitives; import net.sf.cglib.asm.*; @@ -306,7 +306,7 @@ public final class StructureCompiler { for (int i = 0; i < fields.size(); i++) { Class outputType = fields.get(i).getType(); - Class inputType = PrimitiveUtils.wrap(outputType); + Class inputType = Primitives.wrap(outputType); String typeDescriptor = Type.getDescriptor(outputType); String inputPath = inputType.getName().replace('.', '/'); @@ -323,7 +323,7 @@ public final class StructureCompiler { mv.visitVarInsn(Opcodes.ALOAD, 3); mv.visitVarInsn(Opcodes.ALOAD, 2); - if (!PrimitiveUtils.isPrimitive(outputType)) + if (!outputType.isPrimitive()) mv.visitTypeInsn(Opcodes.CHECKCAST, inputPath); else boxingHelper.unbox(Type.getType(outputType)); diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/instances/PrimitiveGenerator.java b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/instances/PrimitiveGenerator.java index 526846a0..5563acc6 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/instances/PrimitiveGenerator.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/instances/PrimitiveGenerator.java @@ -21,8 +21,8 @@ import java.lang.reflect.Array; import javax.annotation.Nullable; -import com.comphenix.protocol.reflect.PrimitiveUtils; import com.google.common.base.Defaults; +import com.google.common.primitives.Primitives; /** * Provides constructors for primtive types, wrappers, arrays and strings. @@ -58,10 +58,10 @@ public class PrimitiveGenerator implements InstanceProvider { @Override public Object create(@Nullable Class type) { - if (PrimitiveUtils.isPrimitive(type)) { + if (type.isPrimitive()) { return Defaults.defaultValue(type); - } else if (PrimitiveUtils.isWrapperType(type)) { - return Defaults.defaultValue(PrimitiveUtils.unwrap(type)); + } else if (Primitives.isWrapperType(type)) { + return Defaults.defaultValue(Primitives.unwrap(type)); } else if (type.isArray()) { Class arrayType = type.getComponentType(); return Array.newInstance(arrayType, 0);