diff options
author | Rawi01 <Rawi01@users.noreply.github.com> | 2020-12-21 17:07:22 +0100 |
---|---|---|
committer | Rawi01 <Rawi01@users.noreply.github.com> | 2020-12-21 17:16:33 +0100 |
commit | 624c8a5a52d14ba1a6b690809f9ada15f71e15f5 (patch) | |
tree | 4ecef644d47fd32ef8cdbd66207f30f997362cc9 /src/core | |
parent | 626c33255bbac12ddab72dda7de2447132f29ae4 (diff) | |
download | lombok-624c8a5a52d14ba1a6b690809f9ada15f71e15f5.tar.gz lombok-624c8a5a52d14ba1a6b690809f9ada15f71e15f5.tar.bz2 lombok-624c8a5a52d14ba1a6b690809f9ada15f71e15f5.zip |
[fixes #2682] Fix NPE, always add Javadoc return statement
Diffstat (limited to 'src/core')
5 files changed, 29 insertions, 30 deletions
diff --git a/src/core/lombok/core/handlers/HandlerUtil.java b/src/core/lombok/core/handlers/HandlerUtil.java index 92a9a142..9fd38720 100644 --- a/src/core/lombok/core/handlers/HandlerUtil.java +++ b/src/core/lombok/core/handlers/HandlerUtil.java @@ -762,12 +762,14 @@ public class HandlerUtil { private static final Pattern LINE_BREAK_FINDER = Pattern.compile("(\\r?\\n)?"); public static String stripLinesWithTagFromJavadoc(String javadoc, String regexpFragment) { + if (javadoc == null || javadoc.isEmpty()) return javadoc; Pattern p = Pattern.compile("^\\s*\\**\\s*" + regexpFragment + "\\s*\\**\\s*$", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(javadoc); return m.replaceAll(""); } public static String stripSectionsFromJavadoc(String javadoc) { + if (javadoc == null || javadoc.isEmpty()) return javadoc; Matcher sectionMatcher = SECTION_FINDER.matcher(javadoc); if (!sectionMatcher.find()) return javadoc; @@ -775,6 +777,7 @@ public class HandlerUtil { } public static String getJavadocSection(String javadoc, String sectionNameSpec) { + if (javadoc == null || javadoc.isEmpty()) return null; String[] sectionNames = sectionNameSpec.split("\\|"); Matcher sectionMatcher = SECTION_FINDER.matcher(javadoc); Matcher lineBreakMatcher = LINE_BREAK_FINDER.matcher(javadoc); @@ -805,19 +808,30 @@ public class HandlerUtil { private static final Pattern FIND_RETURN = Pattern.compile("^\\s*\\**\\s*@returns?\\s+.*$", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); public static String addReturnsThisIfNeeded(String in) { - if (FIND_RETURN.matcher(in).find()) return in; + if (in != null && FIND_RETURN.matcher(in).find()) return in; return addJavadocLine(in, "@return {@code this}."); } public static String addReturnsUpdatedSelfIfNeeded(String in) { - if (FIND_RETURN.matcher(in).find()) return in; + if (in != null && FIND_RETURN.matcher(in).find()) return in; return addJavadocLine(in, "@return a clone of this object, except with this updated property (returns {@code this} if an identical value is passed)."); } public static String addJavadocLine(String in, String line) { + if (in == null) return line; if (in.endsWith("\n")) return in + line + "\n"; return in + "\n" + line; } + + public static String getParamJavadoc(String methodComment, String param) { + if (methodComment == null || methodComment.isEmpty()) return methodComment; + Pattern pattern = Pattern.compile("@param " + param + " (\\S|\\s)+?(?=^ ?@)", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(methodComment); + if (matcher.find()) { + return matcher.group(); + } + return null; + } } diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index a7afd996..053b102e 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -2645,8 +2645,9 @@ public class EclipseHandlerUtil { } public static void setDocComment(CompilationUnitDeclaration cud, TypeDeclaration type, ASTNode node, String doc) { - Map<String, String> docs = EcjAugments.CompilationUnit_javadoc.setIfAbsent(cud.compilationResult.compilationUnit, new HashMap<String, String>()); + if (doc == null) return; + Map<String, String> docs = EcjAugments.CompilationUnit_javadoc.setIfAbsent(cud.compilationResult.compilationUnit, new HashMap<String, String>()); if (node instanceof AbstractMethodDeclaration) { AbstractMethodDeclaration methodDeclaration = (AbstractMethodDeclaration) node; String signature = getSignature(type, methodDeclaration); @@ -2759,10 +2760,10 @@ public class EclipseHandlerUtil { try { CompilationUnitDeclaration cud = ((CompilationUnitDeclaration) from.top().get()); String newJavadoc = copyMode.apply(cud, from); - if (newJavadoc != null) { - if (forceAddReturn) newJavadoc = addReturnsThisIfNeeded(newJavadoc); - setDocComment(cud, type, to, newJavadoc); + if (forceAddReturn) { + newJavadoc = addReturnsThisIfNeeded(newJavadoc); } + setDocComment(cud, type, to, newJavadoc); } catch (Exception ignore) {} } } diff --git a/src/core/lombok/eclipse/handlers/HandleBuilder.java b/src/core/lombok/eclipse/handlers/HandleBuilder.java index f8eb9ed0..189c5a69 100755 --- a/src/core/lombok/eclipse/handlers/HandleBuilder.java +++ b/src/core/lombok/eclipse/handlers/HandleBuilder.java @@ -29,8 +29,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import org.eclipse.jdt.internal.compiler.ast.ASTNode; import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration; @@ -1045,14 +1043,8 @@ public class HandleBuilder extends EclipseAnnotationHandler<Builder> { try { CompilationUnitDeclaration cud = (CompilationUnitDeclaration) from.top().get(); String methodComment = getDocComment(cud, from.get()); - if (methodComment == null) return; - - Pattern pattern = Pattern.compile("@param " + param + " (\\S|\\s)+?(?=^ ?@)", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); - Matcher matcher = pattern.matcher(methodComment); - if (matcher.find()) { - String newJavadoc = addReturnsThisIfNeeded(matcher.group()); - setDocComment(cud, type, to, newJavadoc); - } + String newJavadoc = addReturnsThisIfNeeded(getParamJavadoc(methodComment, param)); + setDocComment(cud, type, to, newJavadoc); } catch (Exception ignore) {} } diff --git a/src/core/lombok/javac/handlers/HandleBuilder.java b/src/core/lombok/javac/handlers/HandleBuilder.java index 40b59183..867d6889 100644 --- a/src/core/lombok/javac/handlers/HandleBuilder.java +++ b/src/core/lombok/javac/handlers/HandleBuilder.java @@ -27,8 +27,6 @@ import static lombok.javac.JavacTreeMaker.TypeTag.typeTag; import static lombok.javac.handlers.JavacHandlerUtil.*; import java.util.ArrayList; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import javax.lang.model.element.Modifier; @@ -927,16 +925,10 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> { try { JCCompilationUnit cu = ((JCCompilationUnit) from.top().get()); String methodComment = Javac.getDocComment(cu, from.get()); - if (methodComment == null) return; - - Pattern pattern = Pattern.compile("@param " + param + " (\\S|\\s)+?(?=^ ?@)", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); - Matcher matcher = pattern.matcher(methodComment); - if (matcher.find()) { - String newJavadoc = addReturnsThisIfNeeded(matcher.group()); - Javac.setDocComment(cu, to, newJavadoc); - } + String newJavadoc = addReturnsThisIfNeeded(getParamJavadoc(methodComment, param)); + Javac.setDocComment(cu, to, newJavadoc); } catch (Exception ignore) {} - } + } public JavacNode makeBuilderClass(BuilderJob job) { //boolean isStatic, JavacNode source, JavacNode tdParent, String builderClassName, List<JCTypeParameter> typeParams, JCAnnotation ast, AccessLevel access) { diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index df1eb438..fbde9568 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -2120,10 +2120,10 @@ public class JavacHandlerUtil { try { JCCompilationUnit cu = ((JCCompilationUnit) from.top().get()); String newJavadoc = copyMode.apply(cu, from); - if (newJavadoc != null) { - if (forceAddReturn) newJavadoc = addReturnsThisIfNeeded(newJavadoc); - Javac.setDocComment(cu, to, newJavadoc); + if (forceAddReturn) { + newJavadoc = addReturnsThisIfNeeded(newJavadoc); } + Javac.setDocComment(cu, to, newJavadoc); } catch (Exception ignore) {} } |