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 com.google.clearsilver.jsilver.data.Data;
     20 import com.google.doclava.apicheck.ApiCheck;
     21 import com.google.doclava.apicheck.ApiInfo;
     22 import com.google.doclava.apicheck.ApiParseException;
     23 import java.io.PrintWriter;
     24 import java.io.StringWriter;
     25 import java.util.ArrayList;
     26 import java.util.Collections;
     27 import java.util.LinkedHashMap;
     28 import java.util.List;
     29 import java.util.Map;
     30 
     31 
     32 /**
     33  * Applies version information to the DroidDoc class model from apicheck XML files. Sample usage:
     34  *
     35  * <pre>
     36  *   ClassInfo[] classInfos = ...
     37  *
     38  *   SinceTagger sinceTagger = new SinceTagger()
     39  *   sinceTagger.addVersion("frameworks/base/api/1.xml", "product 1.0")
     40  *   sinceTagger.addVersion("frameworks/base/api/2.xml", "product 1.5")
     41  *   sinceTagger.tagAll(...);
     42  * </pre>
     43  */
     44 public class SinceTagger {
     45 
     46   private final Map<String, String> xmlToName = new LinkedHashMap<String, String>();
     47 
     48   /**
     49    * Specifies the apicheck XML file and the API version it holds. Calls to this method should be
     50    * called in order from oldest version to newest.
     51    */
     52   public void addVersion(String file, String name) {
     53     xmlToName.put(file, name);
     54   }
     55 
     56   public void tagAll(ClassInfo[] classDocs) {
     57     // read through the XML files in order, applying their since information
     58     // to the Javadoc models
     59     for (Map.Entry<String, String> versionSpec : xmlToName.entrySet()) {
     60       String xmlFile = versionSpec.getKey();
     61       String versionName = versionSpec.getValue();
     62 
     63       ApiInfo specApi;
     64       try {
     65         specApi = new ApiCheck().parseApi(xmlFile);
     66       } catch (ApiParseException e) {
     67         StringWriter stackTraceWriter = new StringWriter();
     68         e.printStackTrace(new PrintWriter(stackTraceWriter));
     69         Errors.error(Errors.BROKEN_SINCE_FILE, null, "Failed to parse " + xmlFile
     70                 + " for " + versionName + " since data.\n" + stackTraceWriter.toString());
     71         continue;
     72       }
     73 
     74       applyVersionsFromSpec(versionName, specApi, classDocs);
     75     }
     76 
     77     if (!xmlToName.isEmpty()) {
     78       warnForMissingVersions(classDocs);
     79     }
     80   }
     81 
     82   public boolean hasVersions() {
     83     return !xmlToName.isEmpty();
     84   }
     85 
     86   /**
     87    * Writes an index of the version names to {@code data}.
     88    */
     89   public void writeVersionNames(Data data) {
     90     int index = 1;
     91     for (String version : xmlToName.values()) {
     92       data.setValue("since." + index + ".name", version);
     93       index++;
     94     }
     95   }
     96 
     97   /**
     98    * Applies the version information to {@code classDocs} where not already present.
     99    *
    100    * @param versionName the version name
    101    * @param specApi the spec for this version. If a symbol is in this spec, it was present in the
    102    *        named version
    103    * @param classDocs the doc model to update
    104    */
    105   private void applyVersionsFromSpec(String versionName, ApiInfo specApi, ClassInfo[] classDocs) {
    106     for (ClassInfo classDoc : classDocs) {
    107       PackageInfo packageSpec
    108           = specApi.getPackages().get(classDoc.containingPackage().name());
    109 
    110       if (packageSpec == null) {
    111         continue;
    112       }
    113 
    114       ClassInfo classSpec = packageSpec.allClasses().get(classDoc.name());
    115 
    116       if (classSpec == null) {
    117         continue;
    118       }
    119 
    120       versionPackage(versionName, classDoc.containingPackage());
    121       versionClass(versionName, classDoc);
    122       versionConstructors(versionName, classSpec, classDoc);
    123       versionFields(versionName, classSpec, classDoc);
    124       versionMethods(versionName, classSpec, classDoc);
    125     }
    126   }
    127 
    128   /**
    129    * Applies version information to {@code doc} where not already present.
    130    */
    131   private void versionPackage(String versionName, PackageInfo doc) {
    132     if (doc.getSince() == null) {
    133       doc.setSince(versionName);
    134     }
    135   }
    136 
    137   /**
    138    * Applies version information to {@code doc} where not already present.
    139    */
    140   private void versionClass(String versionName, ClassInfo doc) {
    141     if (doc.getSince() == null) {
    142       doc.setSince(versionName);
    143     }
    144   }
    145 
    146   /**
    147    * Applies version information from {@code spec} to {@code doc} where not already present.
    148    */
    149   private void versionConstructors(String versionName, ClassInfo spec, ClassInfo doc) {
    150     for (MethodInfo constructor : doc.constructors()) {
    151       if (constructor.getSince() == null
    152           && spec.hasConstructor(constructor)) {
    153         constructor.setSince(versionName);
    154       }
    155     }
    156   }
    157 
    158   /**
    159    * Applies version information from {@code spec} to {@code doc} where not already present.
    160    */
    161   private void versionFields(String versionName, ClassInfo spec, ClassInfo doc) {
    162     for (FieldInfo field : doc.fields()) {
    163       if (field.getSince() == null && spec.allFields().containsKey(field.name())) {
    164         field.setSince(versionName);
    165       }
    166     }
    167   }
    168 
    169   /**
    170    * Applies version information from {@code spec} to {@code doc} where not already present.
    171    */
    172   private void versionMethods(String versionName, ClassInfo spec, ClassInfo doc) {
    173     for (MethodInfo method : doc.methods()) {
    174       if (method.getSince() != null) {
    175         continue;
    176       }
    177 
    178       for (ClassInfo superclass : spec.hierarchy()) {
    179         if (superclass.allMethods().containsKey(method.getHashableName())) {
    180           method.setSince(versionName);
    181           break;
    182         }
    183       }
    184     }
    185   }
    186 
    187   /**
    188    * Warns if any symbols are missing version information. When configured properly, this will yield
    189    * zero warnings because {@code apicheck} guarantees that all symbols are present in the most
    190    * recent API.
    191    */
    192   private void warnForMissingVersions(ClassInfo[] classDocs) {
    193     for (ClassInfo claz : classDocs) {
    194       if (!checkLevelRecursive(claz)) {
    195         continue;
    196       }
    197 
    198       if (claz.getSince() == null) {
    199         Errors.error(Errors.NO_SINCE_DATA, claz.position(), "XML missing class "
    200             + claz.qualifiedName());
    201       }
    202 
    203       for (FieldInfo field : missingVersions(claz.fields())) {
    204         Errors.error(Errors.NO_SINCE_DATA, field.position(), "XML missing field "
    205             + claz.qualifiedName() + "#" + field.name());
    206       }
    207 
    208       for (MethodInfo constructor : missingVersions(claz.constructors())) {
    209         Errors.error(Errors.NO_SINCE_DATA, constructor.position(), "XML missing constructor "
    210             + claz.qualifiedName() + "#" + constructor.getHashableName());
    211       }
    212 
    213       for (MethodInfo method : missingVersions(claz.methods())) {
    214         Errors.error(Errors.NO_SINCE_DATA, method.position(), "XML missing method "
    215             + claz.qualifiedName() + "#" + method.getHashableName());
    216       }
    217     }
    218   }
    219 
    220   /**
    221    * Returns the DocInfos in {@code all} that are documented but do not have since tags.
    222    */
    223   private <T extends MemberInfo> Iterable<T> missingVersions(ArrayList<T> all) {
    224     List<T> result = Collections.emptyList();
    225     for (T t : all) {
    226       // if this member has version info or isn't documented, skip it
    227       if (t.getSince() != null || t.isHidden() || !checkLevelRecursive(t.realContainingClass())) {
    228         continue;
    229       }
    230 
    231       if (result.isEmpty()) {
    232         result = new ArrayList<T>(); // lazily construct a mutable list
    233       }
    234       result.add(t);
    235     }
    236     return result;
    237   }
    238 
    239   /**
    240    * Returns true if {@code claz} and all containing classes are documented. The result may be used
    241    * to filter out members that exist in the API data structure but aren't a part of the API.
    242    */
    243   private boolean checkLevelRecursive(ClassInfo claz) {
    244     for (ClassInfo c = claz; c != null; c = c.containingClass()) {
    245       if (!c.checkLevel()) {
    246         return false;
    247       }
    248     }
    249     return true;
    250   }
    251 }
    252