1 /* 2 * Copyright (C) 2015 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 package com.google.currysrc.processors; 17 18 import com.google.common.collect.ImmutableList; 19 import com.google.common.collect.Lists; 20 import com.google.currysrc.api.process.Reporter; 21 import com.google.currysrc.api.process.ast.BodyDeclarationLocator; 22 import com.google.currysrc.api.process.ast.BodyDeclarationLocators; 23 24 import org.eclipse.jdt.core.dom.ASTNode; 25 import org.eclipse.jdt.core.dom.BodyDeclaration; 26 import org.eclipse.jdt.core.dom.Comment; 27 import org.eclipse.jdt.core.dom.Javadoc; 28 29 import java.io.FileNotFoundException; 30 import java.io.IOException; 31 import java.io.InputStream; 32 import java.io.InputStreamReader; 33 import java.io.LineNumberReader; 34 import java.io.Reader; 35 import java.nio.charset.StandardCharsets; 36 import java.util.List; 37 38 /** 39 * Replaces selected Javadoc comments with canned text. 40 */ 41 public class ReplaceSelectedJavadoc extends BaseModifyCommentScanner { 42 43 private static final String MARKER = "--"; 44 45 private List<Replacement> replacements; 46 47 public ReplaceSelectedJavadoc(List<Replacement> replacements) { 48 this.replacements = ImmutableList.copyOf(replacements); 49 } 50 51 @Override protected String processComment(Reporter reporter, Comment commentNode, 52 String commentText) { 53 54 if (!(commentNode instanceof Javadoc)) { 55 return null; 56 } 57 Javadoc javadoc = (Javadoc) commentNode; 58 ASTNode declarationNode = javadoc.getParent(); 59 if (declarationNode == null || !(declarationNode instanceof BodyDeclaration)) { 60 return null; 61 } 62 63 BodyDeclaration bodyDeclaration = (BodyDeclaration) declarationNode; 64 for (Replacement replacement : replacements) { 65 if (replacement.locator.matches(bodyDeclaration)) { 66 reporter.info(bodyDeclaration, "Replaced comment text"); 67 return replacement.replacementText; 68 } 69 } 70 return null; 71 } 72 73 @Override 74 public String toString() { 75 return "ReplaceSelectedJavadoc{" + 76 "replacements=" + replacements + 77 '}'; 78 } 79 80 /** 81 * Reads a file containing replacement javadoc. 82 * 83 * <p>The format is: 84 * <pre> 85 * --method:foo.Bar#Bar(String) 86 * Replacement text. 87 * -- 88 * 89 * # This is a comment. 90 * --field:foo.Bar#baz 91 * Replacement text. 92 * -- 93 * </pre> 94 * 95 * <p>Empty lines are ignored. Lines beginning with # are ignored. The first line of a block must 96 * start with "--" and be immediately followed by a {@link BodyDeclarationLocator} string. The 97 * following lines are read verbatim until a line containing closing marker, also "--". 98 */ 99 public static ReplaceSelectedJavadoc createFromResource(String resourceName) throws IOException { 100 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 101 InputStream is = classLoader.getResourceAsStream(resourceName); 102 if (is == null) { 103 throw new FileNotFoundException("Unknown resource: " + resourceName); 104 } 105 Reader fileReader = new InputStreamReader(is, StandardCharsets.UTF_8); 106 try (LineNumberReader reader = new LineNumberReader(fileReader)) { 107 List<Replacement> replacements = Lists.newArrayList(); 108 String line; 109 while ((line = reader.readLine()) != null) { 110 if (line.trim().isEmpty() || line.startsWith("#")) { 111 // Forgive empty lines and comments. 112 continue; 113 } 114 if (!line.startsWith(MARKER)) { 115 throw new IOException( 116 "Unexpected locator line: " + line + " at line " + reader.getLineNumber()); 117 } 118 String locatorString = line.substring(MARKER.length()); 119 BodyDeclarationLocator locator = BodyDeclarationLocators.fromStringForm(locatorString); 120 121 // Read the replacement comment. 122 String replacementText = readUntilMarker(reader); 123 replacements.add(new Replacement(locator, replacementText)); 124 } 125 return new ReplaceSelectedJavadoc(replacements); 126 } 127 } 128 129 private static String readUntilMarker(LineNumberReader reader) throws IOException { 130 StringBuilder builder = new StringBuilder(); 131 String line; 132 while ((line = reader.readLine()) != null) { 133 if (line.startsWith(MARKER)) { 134 break; 135 } 136 builder.append(line); 137 builder.append("\n"); 138 } 139 if (line == null) { 140 throw new IOException("Did not find closing marker at end of file " + reader.getLineNumber()); 141 } 142 // Remove the trailing \n 143 builder.setLength(builder.length() - 1); 144 return builder.toString(); 145 } 146 147 public static class Replacement { 148 public final BodyDeclarationLocator locator; 149 public final String replacementText; 150 151 public Replacement(BodyDeclarationLocator locator, String replacementText) { 152 this.locator = locator; 153 this.replacementText = replacementText; 154 } 155 } 156 } 157