/** * Gets the {@link PrimitiveType} for the given type key. * * @param key The type key * @return The primitive type * @throws IllegalStateException If the descriptor is invalid */ static PrimitiveType getFromKey(char key) { if (key == 'V') { return VoidType.INSTANCE; } if (BaseType.isValidBase(key)) { return BaseType.getFromKey(key); } throw new IllegalStateException("Invalid primitive type: " + key); }
/** * Establishes whether the given key, is a valid base * key. * * @param key The key * @return {@code true} if the key represents a base type; * {@code false} otherwise */ public static boolean isValidBase(final char key) { return Arrays.stream(values()) .anyMatch(type -> type.key == key); }
/** * Reads the next {@link PrimitiveType} from source. * * @return The type * @throws IllegalStateException If the descriptor is invalid */ public PrimitiveType readPrimitiveType() { // Void Type if (this.peek() == 'V') { this.advance(); return VoidType.INSTANCE; } // Base Type if (BaseType.isValidBase(this.peek())) { return this.readBaseType(); } throw new IllegalStateException("Invalid descriptor provided!"); }
/** * Reads the next {@link BaseType} from source. * * @return The type * @throws IllegalStateException If the descriptor is invalid */ public BaseType readBaseType() { return BaseType.getFromKey(this.advance()); }
/** * Reads the next {@link FieldType} from source. * * @return The type * @throws IllegalStateException If the descriptor is invalid */ public FieldType readFieldType() { // Array Type if (this.peek() == '[') { return this.readArrayType(); } // Base Type if (BaseType.isValidBase(this.peek())) { return this.readBaseType(); } // Object Type if (this.peek() == 'L') { return this.readObjectType(); } throw new IllegalStateException("Invalid descriptor provided!"); }
/** * Gets the {@link BaseType} from the given key. * * @param key The key * @return The base type */ public static BaseType getFromKey(final char key) { return Arrays.stream(values()) .filter(type -> type.key == key) .findFirst().orElse(null); }