make/jdk/src/classes/build/tools/taglet/Preview.java
changeset 58713 ad69fd32778e
equal deleted inserted replaced
58712:14e098407bb0 58713:ad69fd32778e
       
     1 /*
       
     2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package build.tools.taglet;
       
    27 
       
    28 import java.util.Arrays;
       
    29 import java.util.EnumSet;
       
    30 import java.util.List;
       
    31 import java.util.Set;
       
    32 import java.util.function.Predicate;
       
    33 import javax.lang.model.element.Element;
       
    34 import com.sun.source.doctree.DocTree;
       
    35 import com.sun.source.doctree.TextTree;
       
    36 import com.sun.source.doctree.UnknownInlineTagTree;
       
    37 import jdk.javadoc.doclet.Taglet;
       
    38 import static jdk.javadoc.doclet.Taglet.Location.*;
       
    39 
       
    40 /**
       
    41  * An block tag to insert a standard warning about a preview API.
       
    42  */
       
    43 public class Preview implements Taglet {
       
    44 
       
    45     /** Returns the set of locations in which a taglet may be used. */
       
    46     @Override
       
    47     public Set<Location> getAllowedLocations() {
       
    48         return EnumSet.of(MODULE, PACKAGE, TYPE, CONSTRUCTOR, METHOD, FIELD);
       
    49     }
       
    50 
       
    51     @Override
       
    52     public boolean isInlineTag() {
       
    53         return true;
       
    54     }
       
    55 
       
    56     @Override
       
    57     public String getName() {
       
    58         return "preview";
       
    59     }
       
    60 
       
    61     @Override
       
    62     public String toString(List<? extends DocTree> tags, Element elem) {
       
    63         UnknownInlineTagTree previewTag = (UnknownInlineTagTree) tags.get(0);
       
    64         List<? extends DocTree> previewContent = previewTag.getContent();
       
    65         String previewText = ((TextTree) previewContent.get(0)).getBody();
       
    66         String[] summaryAndDetails = previewText.split("\n\r?\n\r?");
       
    67         String summary = summaryAndDetails[0];
       
    68         String details = summaryAndDetails.length > 1 ? summaryAndDetails[1] : summaryAndDetails[0];
       
    69         StackTraceElement[] stackTrace = new Exception().getStackTrace();
       
    70         Predicate<StackTraceElement> isSummary =
       
    71                 el -> el.getClassName().endsWith("HtmlDocletWriter") &&
       
    72                       el.getMethodName().equals("addSummaryComment");
       
    73         if (Arrays.stream(stackTrace).anyMatch(isSummary)) {
       
    74             return "<div style=\"display:inline-block; font-weight:bold\">" + summary + "</div><br>";
       
    75         }
       
    76         return "<div style=\"border: 1px solid red; border-radius: 5px; padding: 5px; display:inline-block; font-size: larger\">" + details + "</div><br>";
       
    77     }
       
    78 }
       
    79