Home | History | Annotate | Download | only in doclava
      1 /*
      2  * Copyright (C) 2010 Google Inc.
      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 com.google.doclava;
     18 
     19 import java.util.ArrayList;
     20 import java.util.Arrays;
     21 
     22 public class AnnotationInstanceInfo implements Resolvable {
     23   private ClassInfo mType;
     24   private String mAnnotationName; // for debugging purposes TODO - remove
     25   private ArrayList<AnnotationValueInfo> mElementValues;
     26   private ArrayList<Resolution> mResolutions;
     27 
     28   public AnnotationInstanceInfo() {
     29       mType = null;
     30       mElementValues = new ArrayList<AnnotationValueInfo>();
     31     }
     32 
     33   public AnnotationInstanceInfo(ClassInfo type, AnnotationValueInfo[] elementValues) {
     34     mType = type;
     35     mElementValues = new ArrayList<AnnotationValueInfo>(Arrays.asList(elementValues));
     36   }
     37 
     38   ClassInfo type() {
     39     return mType;
     40   }
     41 
     42   public void setClass(ClassInfo cl) {
     43       mType = cl;
     44   }
     45 
     46   public void setSimpleAnnotationName(String name) {
     47       mAnnotationName = name;
     48   }
     49 
     50   ArrayList<AnnotationValueInfo> elementValues() {
     51     return mElementValues;
     52   }
     53 
     54   public void addElementValue(AnnotationValueInfo info) {
     55       mElementValues.add(info);
     56   }
     57 
     58   @Override
     59   public String toString() {
     60     StringBuilder str = new StringBuilder();
     61     str.append("@");
     62     if (mType == null) {
     63         str.append(mAnnotationName);
     64     } else {
     65         str.append(mType.qualifiedName());
     66     }
     67     str.append("(");
     68 
     69     for (AnnotationValueInfo value : mElementValues) {
     70       if (value.element() != null) {
     71           str.append(value.element().name());
     72           str.append("=");
     73       }
     74 
     75       str.append(value.valueString());
     76       if (value != mElementValues.get(mElementValues.size()-1)) {
     77         str.append(",");
     78       }
     79     }
     80     str.append(")");
     81     return str.toString();
     82   }
     83 
     84   public void addResolution(Resolution resolution) {
     85       if (mResolutions == null) {
     86           mResolutions = new ArrayList<Resolution>();
     87       }
     88 
     89       mResolutions.add(resolution);
     90   }
     91 
     92   public void printResolutions() {
     93       System.out.println("Resolutions for Annotation:");
     94       for (Resolution r : mResolutions) {
     95           System.out.println(r);
     96       }
     97   }
     98 
     99   public boolean resolveResolutions() {
    100       ArrayList<Resolution> resolutions = mResolutions;
    101       mResolutions = new ArrayList<Resolution>();
    102 
    103       boolean allResolved = true;
    104       for (Resolution resolution : resolutions) {
    105           StringBuilder qualifiedClassName = new StringBuilder();
    106           InfoBuilder.resolveQualifiedName(resolution.getValue(), qualifiedClassName,
    107                   resolution.getInfoBuilder());
    108 
    109           // if we still couldn't resolve it, save it for the next pass
    110           if ("".equals(qualifiedClassName.toString())) {
    111               mResolutions.add(resolution);
    112               allResolved = false;
    113           } else if ("annotationTypeName".equals(resolution.getVariable())) {
    114               setClass(InfoBuilder.Caches.obtainClass(qualifiedClassName.toString()));
    115           }
    116       }
    117 
    118       return allResolved;
    119   }
    120 }
    121