001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.harmony.lang.reflect.support;
018:
019: import java.lang.reflect.MalformedParameterizedTypeException;
020:
021: import org.apache.harmony.lang.reflect.parser.InterimParameterizedType;
022: import org.apache.harmony.lang.reflect.parser.InterimClassType;
023: import org.apache.harmony.lang.reflect.parser.*;
024:
025: import org.apache.harmony.vm.VMGenericsAndAnnotations;
026:
027: /**
028: * @author Serguei S. Zapreyev
029: * @version $Revision: 1.1.2.1 $
030: */
031:
032: /**
033: * AuxiliaryChecker provides kinds of check.
034: */
035: public final class AuxiliaryChecker {
036:
037: /**
038: * This method checks the correspondence of the formal parameter number and the actual argument number.
039: *
040: * @param ppType a parsered information produced from a parameterized type signature.
041: * @param startPoint an instance of the Class, Method, Constructor or Field type to start the search
042: * of a type variable declaration place.
043: * @return an array of Type objects representing the actual type arguments to this type.
044: */
045: public static void checkArgsNumber(InterimParameterizedType ppType,
046: Object startPoint)
047: throws MalformedParameterizedTypeException {
048: // XXX: reprogram method (for example, to improve the preloop and the loop)
049: InterimParameterizedType currentBit = ppType;
050: InterimType currentBitArgs[] = currentBit.parameters;
051:
052: InterimClassType currentClass = currentBit.rawType;
053: Class klazz = null;
054: try {
055: klazz = AuxiliaryLoader.findClass(AuxiliaryFinder
056: .transform(currentClass.classTypeName.substring(1)
057: .replace('/', '.')), startPoint);
058: } catch (Throwable e) {
059:
060: }
061:
062: String ccSignature = AuxiliaryUtil
063: .toUTF8(VMGenericsAndAnnotations.getSignature(klazz));
064:
065: InterimClassGenericDecl decl;
066: if (ccSignature != null) {
067: decl = (InterimClassGenericDecl) Parser.parseSignature(
068: ccSignature, Parser.SignatureKind.CLASS_SIGNATURE,
069: (java.lang.reflect.GenericDeclaration) startPoint);
070:
071: if ((decl.typeParameters != null && currentBitArgs != null && decl.typeParameters.length != currentBitArgs.length)
072: || (decl.typeParameters == null && currentBitArgs != null)
073: || (decl.typeParameters != null && currentBitArgs == null)) {
074: throw new MalformedParameterizedTypeException();
075: }
076: } else {
077: if (currentBitArgs != null && currentBitArgs.length > 0) {
078: throw new MalformedParameterizedTypeException();
079: }
080: }
081:
082: while (currentBit.ownerType != null) {
083: InterimType pt = currentBit.ownerType;
084: if (pt instanceof InterimParameterizedType) {
085: currentBit = (InterimParameterizedType) currentBit.ownerType;
086: } else {
087: break;
088: }
089: currentBitArgs = currentBit.parameters;
090:
091: currentClass = currentBit.rawType;
092: klazz = null;
093: try {
094: //klazz = ClassLoader.findClass(currentClass.classTypeName);
095: klazz = AuxiliaryLoader.findClass(AuxiliaryFinder
096: .transform(currentClass.classTypeName
097: .substring(1).replace('/', '.')),
098: startPoint);
099: } catch (Throwable e) {
100:
101: }
102:
103: ccSignature = AuxiliaryUtil.toUTF8(VMGenericsAndAnnotations
104: .getSignature(klazz));
105: if (ccSignature != null) {
106: decl = (InterimClassGenericDecl) Parser
107: .parseSignature(
108: ccSignature,
109: Parser.SignatureKind.CLASS_SIGNATURE,
110: (java.lang.reflect.GenericDeclaration) startPoint);
111:
112: if ((decl.typeParameters != null
113: && currentBitArgs != null && decl.typeParameters.length != currentBitArgs.length)
114: || (decl.typeParameters == null && currentBitArgs != null)
115: || (decl.typeParameters != null && currentBitArgs == null)) {
116: throw new MalformedParameterizedTypeException();
117: }
118: } else {
119: if (currentBitArgs != null && currentBitArgs.length > 0) {
120: throw new MalformedParameterizedTypeException();
121: }
122: }
123: }
124: }
125: }
|