/* * Copyright (C) 2008 The Android Open Source Project * * 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. */ package libcore.reflect; import java.lang.reflect.Constructor; import java.lang.reflect.GenericDeclaration; import java.lang.reflect.GenericSignatureFormatError; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import libcore.util.EmptyArray; /** * Implements a parser for the generics signature attribute. * Uses a top-down, recursive descent parsing approach for the following grammar: *
* ClassSignature ::= * OptFormalTypeParams SuperclassSignature {SuperinterfaceSignature}. * SuperclassSignature ::= ClassTypeSignature. * SuperinterfaceSignature ::= ClassTypeSignature. * * OptFormalTypeParams ::= * ["<" FormalTypeParameter {FormalTypeParameter} ">"]. * * FormalTypeParameter ::= Ident ClassBound {InterfaceBound}. * ClassBound ::= ":" [FieldTypeSignature]. * InterfaceBound ::= ":" FieldTypeSignature. * * FieldTypeSignature ::= * ClassTypeSignature | ArrayTypeSignature | TypeVariableSignature. * ArrayTypeSignature ::= "[" TypSignature. * * ClassTypeSignature ::= * "L" {Ident "/"} Ident OptTypeArguments {"." Ident OptTypeArguments} ";". * * OptTypeArguments ::= "<" TypeArgument {TypeArgument} ">". * * TypeArgument ::= ([WildcardIndicator] FieldTypeSignature) | "*". * WildcardIndicator ::= "+" | "-". * * TypeVariableSignature ::= "T" Ident ";". * * TypSignature ::= FieldTypeSignature | BaseType. * BaseType ::= "B" | "C" | "D" | "F" | "I" | "J" | "S" | "Z". * * MethodTypeSignature ::= * OptFormalTypeParams "(" {TypeSignature} ")" ReturnType {ThrowsSignature}. * ThrowsSignature ::= ("^" ClassTypeSignature) | ("^" TypeVariableSignature). * * ReturnType ::= TypSignature | VoidDescriptor. * VoidDescriptor ::= "V". **/ public final class GenericSignatureParser { // TODO: unify this with InternalNames public ListOfTypes exceptionTypes; public ListOfTypes parameterTypes; public TypeVariable[] formalTypeParameters; public Type returnType; public Type fieldType; public ListOfTypes interfaceTypes; public Type superclassType; public ClassLoader loader; GenericDeclaration genericDecl; /* * Parser: */ char symbol; // 0: eof; else valid term symbol or first char of identifier. String identifier; /* * Scanner: * eof is private to the scan methods * and it's set only when a scan is issued at the end of the buffer. */ private boolean eof; char[] buffer; int pos; public GenericSignatureParser(ClassLoader loader) { this.loader = loader; } void setInput(GenericDeclaration genericDecl, String input) { if (input != null) { this.genericDecl = genericDecl; this.buffer = input.toCharArray(); this.eof = false; scanSymbol(); } else { this.eof = true; } } /** * Parses the generic signature of a class and creates the data structure * representing the signature. * * @param genericDecl the GenericDeclaration calling this method * @param signature the generic signature of the class */ public void parseForClass(GenericDeclaration genericDecl, String signature) { setInput(genericDecl, signature); if (!eof) { parseClassSignature(); } else { if(genericDecl instanceof Class) { Class c = (Class) genericDecl; this.formalTypeParameters = EmptyArray.TYPE_VARIABLE; this.superclassType = c.getSuperclass(); Class>[] interfaces = c.getInterfaces(); if (interfaces.length == 0) { this.interfaceTypes = ListOfTypes.EMPTY; } else { this.interfaceTypes = new ListOfTypes(interfaces); } } else { this.formalTypeParameters = EmptyArray.TYPE_VARIABLE; this.superclassType = Object.class; this.interfaceTypes = ListOfTypes.EMPTY; } } } /** * Parses the generic signature of a method and creates the data structure * representing the signature. * * @param genericDecl the GenericDeclaration calling this method * @param signature the generic signature of the class */ public void parseForMethod(GenericDeclaration genericDecl, String signature, Class>[] rawExceptionTypes) { setInput(genericDecl, signature); if (!eof) { parseMethodTypeSignature(rawExceptionTypes); } else { Method m = (Method) genericDecl; this.formalTypeParameters = EmptyArray.TYPE_VARIABLE; Class>[] parameterTypes = m.getParameterTypes(); if (parameterTypes.length == 0) { this.parameterTypes = ListOfTypes.EMPTY; } else { this.parameterTypes = new ListOfTypes(parameterTypes); } Class>[] exceptionTypes = m.getExceptionTypes(); if (exceptionTypes.length == 0) { this.exceptionTypes = ListOfTypes.EMPTY; } else { this.exceptionTypes = new ListOfTypes(exceptionTypes); } this.returnType = m.getReturnType(); } } /** * Parses the generic signature of a constructor and creates the data * structure representing the signature. * * @param genericDecl the GenericDeclaration calling this method * @param signature the generic signature of the class */ public void parseForConstructor(GenericDeclaration genericDecl, String signature, Class>[] rawExceptionTypes) { setInput(genericDecl, signature); if (!eof) { parseMethodTypeSignature(rawExceptionTypes); } else { Constructor c = (Constructor) genericDecl; this.formalTypeParameters = EmptyArray.TYPE_VARIABLE; Class>[] parameterTypes = c.getParameterTypes(); if (parameterTypes.length == 0) { this.parameterTypes = ListOfTypes.EMPTY; } else { this.parameterTypes = new ListOfTypes(parameterTypes); } Class>[] exceptionTypes = c.getExceptionTypes(); if (exceptionTypes.length == 0) { this.exceptionTypes = ListOfTypes.EMPTY; } else { this.exceptionTypes = new ListOfTypes(exceptionTypes); } } } /** * Parses the generic signature of a field and creates the data structure * representing the signature. * * @param genericDecl the GenericDeclaration calling this method * @param signature the generic signature of the class */ public void parseForField(GenericDeclaration genericDecl, String signature) { setInput(genericDecl, signature); if (!eof) { this.fieldType = parseFieldTypeSignature(); } } // // Parser: // void parseClassSignature() { // ClassSignature ::= // OptFormalTypeParameters SuperclassSignature {SuperinterfaceSignature}. parseOptFormalTypeParameters(); // SuperclassSignature ::= ClassTypeSignature. this.superclassType = parseClassTypeSignature(); interfaceTypes = new ListOfTypes(16); while (symbol > 0) { // SuperinterfaceSignature ::= ClassTypeSignature. interfaceTypes.add(parseClassTypeSignature()); } } void parseOptFormalTypeParameters() { // OptFormalTypeParameters ::= // ["<" FormalTypeParameter {FormalTypeParameter} ">"]. ListOfVariables typeParams = new ListOfVariables(); if (symbol == '<') { scanSymbol(); typeParams.add(parseFormalTypeParameter()); while ((symbol != '>') && (symbol > 0)) { typeParams.add(parseFormalTypeParameter()); } expect('>'); } this.formalTypeParameters = typeParams.getArray(); } TypeVariableImpl