Home | History | Annotate | Download | only in subst
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package signature.compare.model.subst;
     18 
     19 import signature.model.IArrayType;
     20 import signature.model.IClassDefinition;
     21 import signature.model.IClassReference;
     22 import signature.model.IParameterizedType;
     23 import signature.model.IPrimitiveType;
     24 import signature.model.ITypeReference;
     25 import signature.model.ITypeVariableDefinition;
     26 import signature.model.ITypeVariableReference;
     27 import signature.model.IWildcardType;
     28 import signature.model.impl.SigClassReference;
     29 
     30 import java.util.ArrayList;
     31 import java.util.Collections;
     32 import java.util.HashMap;
     33 import java.util.HashSet;
     34 import java.util.Iterator;
     35 import java.util.List;
     36 import java.util.Map;
     37 import java.util.Set;
     38 
     39 public class ViewpointAdapter {
     40 
     41     static Map<ITypeVariableDefinition, ITypeReference> createTypeMapping(
     42             IParameterizedType paramameterizedType,
     43             IClassDefinition parameterizedTypeDefinition) {
     44         List<ITypeVariableDefinition> typeParameters =
     45                 parameterizedTypeDefinition.getTypeParameters();
     46         List<ITypeReference> actualTypeArguments = paramameterizedType
     47                 .getTypeArguments();
     48         if (actualTypeArguments == null || typeParameters == null) {
     49             return Collections.emptyMap();
     50         }
     51         Map<ITypeVariableDefinition, ITypeReference> substitution =
     52                 new HashMap<ITypeVariableDefinition, ITypeReference>();
     53         Iterator<ITypeVariableDefinition> paramsIterator = typeParameters
     54                 .iterator();
     55         Iterator<ITypeReference> argumentsIterator = actualTypeArguments
     56                 .iterator();
     57         while (paramsIterator.hasNext() && argumentsIterator.hasNext()) {
     58             substitution.put(paramsIterator.next(), argumentsIterator.next());
     59         }
     60         return substitution;
     61     }
     62 
     63     public static Set<ITypeReference> substitutedTypeReferences(
     64             Set<ITypeReference> original,
     65             Map<ITypeVariableDefinition, ITypeReference> mappings) {
     66         List<ITypeReference> result = new ArrayList<ITypeReference>(original);
     67         return new HashSet<ITypeReference>(substitutedTypeReferences(result,
     68                 mappings));
     69     }
     70 
     71     public static List<ITypeReference> substitutedTypeReferences(
     72             List<ITypeReference> original,
     73             Map<ITypeVariableDefinition, ITypeReference> mappings) {
     74         List<ITypeReference> result = new ArrayList<ITypeReference>(original
     75                 .size());
     76         for (ITypeReference typeReference : original) {
     77             result.add(substitutedTypeReference(typeReference, mappings));
     78         }
     79         return result;
     80     }
     81 
     82     public static ITypeReference substitutedTypeReference(
     83             ITypeReference original,
     84             Map<ITypeVariableDefinition, ITypeReference> mappings) {
     85         ITypeReference type = original;
     86         if (type instanceof IClassReference) {
     87             return new ClassReferenceProjection((IClassReference) original,
     88                     mappings);
     89         } else if (type instanceof IPrimitiveType) {
     90             return type;
     91         } else if (type instanceof IArrayType) {
     92             return new ArrayTypeProjection((IArrayType) type, mappings);
     93         } else if (type instanceof IParameterizedType) {
     94             return new ParameterizedTypeProjection((IParameterizedType) type,
     95                     mappings);
     96         } else if (type instanceof IWildcardType) {
     97             return new WildcardTypeProjection((IWildcardType) type, mappings);
     98         } else if (type instanceof ITypeVariableReference) {
     99             // here happens the substitution
    100             ITypeReference subst = mappings.get(((ITypeVariableReference) type)
    101                     .getTypeVariableDefinition());
    102             return subst != null ? subst : type;
    103         }
    104         throw new IllegalStateException();
    105     }
    106 
    107     public static IClassReference getReferenceTo(IClassDefinition definition) {
    108         return new SigClassReference(new ClassProjection(definition,
    109                 new HashMap<ITypeVariableDefinition, ITypeReference>()));
    110     }
    111 }
    112