Package com.tonic.parser.util
Class DescriptorParser
- java.lang.Object
-
- com.tonic.parser.util.DescriptorParser
-
public final class DescriptorParser extends Object
Static helpers for reading and building JVM field and method descriptors.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDescriptorParser.ParseResultOne parsed type together with the number of descriptor characters it spans.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static intgetArgumentsAndReturnSize(String methodDescriptor)Sums the argument slots and the return type slots.static intgetArgumentsSize(String methodDescriptor)Sums the local variable slots the arguments occupy, counting long and double as two.static List<TypeInfo>getArgumentTypes(String methodDescriptor)Parses the parameter types between the parentheses.static StringgetMethodDescriptor(TypeInfo returnType, TypeInfo... paramTypes)Builds a method descriptor from a return type and parameter types.static StringgetMethodDescriptor(TypeInfo returnType, List<TypeInfo> paramTypes)Builds a method descriptor from a return type and a parameter list.static TypeInfogetReturnType(String methodDescriptor)Extracts the return type following the closing parenthesis.static intgetSize(String typeDescriptor)Returns the slot width of a single type.static booleanisFieldDescriptor(String descriptor)Tests whether a descriptor starts with a field type character.static booleanisMethodDescriptor(String descriptor)Tests whether a descriptor has the shape of a method descriptor.static DescriptorParser.ParseResultparseTypeAt(String descriptor, int offset)Parses the single type starting at an offset, recursing through array dimensions.
-
-
-
Method Detail
-
getArgumentTypes
public static List<TypeInfo> getArgumentTypes(String methodDescriptor)
Parses the parameter types between the parentheses.- Parameters:
methodDescriptor- the method descriptor- Returns:
- the parameter types in order
- Throws:
IllegalArgumentException- if the descriptor is null, does not open with '(', has no ')', or holds an unrecognized type
-
getReturnType
public static TypeInfo getReturnType(String methodDescriptor)
Extracts the return type following the closing parenthesis.- Parameters:
methodDescriptor- the method descriptor- Returns:
- the return type
- Throws:
IllegalArgumentException- if the descriptor is null, has no ')', or nothing follows it
-
getArgumentsSize
public static int getArgumentsSize(String methodDescriptor)
Sums the local variable slots the arguments occupy, counting long and double as two.- Parameters:
methodDescriptor- the method descriptor- Returns:
- the argument slot count
- Throws:
IllegalArgumentException- if the descriptor is malformed
-
getArgumentsAndReturnSize
public static int getArgumentsAndReturnSize(String methodDescriptor)
Sums the argument slots and the return type slots.- Parameters:
methodDescriptor- the method descriptor- Returns:
- the combined slot count
- Throws:
IllegalArgumentException- if the descriptor is malformed
-
getSize
public static int getSize(String typeDescriptor)
Returns the slot width of a single type.- Parameters:
typeDescriptor- a field type descriptor- Returns:
- 2 for long and double, 0 for void, otherwise 1
-
parseTypeAt
public static DescriptorParser.ParseResult parseTypeAt(String descriptor, int offset)
Parses the single type starting at an offset, recursing through array dimensions.- Parameters:
descriptor- the descriptor to read fromoffset- the character index the type starts at- Returns:
- the parsed type and the characters it consumed
- Throws:
IllegalArgumentException- if the offset is past the end, an object type is unterminated, or the type character is not recognized
-
getMethodDescriptor
public static String getMethodDescriptor(TypeInfo returnType, TypeInfo... paramTypes)
Builds a method descriptor from a return type and parameter types.- Parameters:
returnType- the return typeparamTypes- the parameter types in order- Returns:
- the assembled descriptor
-
getMethodDescriptor
public static String getMethodDescriptor(TypeInfo returnType, List<TypeInfo> paramTypes)
Builds a method descriptor from a return type and a parameter list.- Parameters:
returnType- the return typeparamTypes- the parameter types in order- Returns:
- the assembled descriptor
-
isMethodDescriptor
public static boolean isMethodDescriptor(String descriptor)
Tests whether a descriptor has the shape of a method descriptor.- Parameters:
descriptor- the descriptor to test, may be null- Returns:
- true if it opens with '(' and contains ')'
-
isFieldDescriptor
public static boolean isFieldDescriptor(String descriptor)
Tests whether a descriptor starts with a field type character.- Parameters:
descriptor- the descriptor to test, may be null- Returns:
- true if it begins with an object, array or primitive type character
-
-