From 69011b90d8e9d15dbf45479df2f4f08658970a16 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Wed, 25 Sep 2019 01:19:55 +0200 Subject: [jdk13] Added support for printing text blocks (triple quoted strings) in text block form --- src/utils/lombok/javac/CommentCatcher.java | 15 +++++++++---- .../javac/java8/CommentCollectingParser.java | 11 ++++------ .../javac/java8/CommentCollectingScanner.java | 6 +++++- .../java8/CommentCollectingScannerFactory.java | 8 +++---- .../javac/java8/CommentCollectingTokenizer.java | 25 +++++++++++++++++----- .../javac/java9/CommentCollectingParser.java | 10 ++++----- 6 files changed, 48 insertions(+), 27 deletions(-) (limited to 'src/utils/lombok/javac') diff --git a/src/utils/lombok/javac/CommentCatcher.java b/src/utils/lombok/javac/CommentCatcher.java index f8b73b0a..90266c26 100644 --- a/src/utils/lombok/javac/CommentCatcher.java +++ b/src/utils/lombok/javac/CommentCatcher.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2011-2014 The Project Lombok Authors. + * Copyright (C) 2011-2019 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -35,9 +35,10 @@ import com.sun.tools.javac.util.Context; public class CommentCatcher { private final JavaCompiler compiler; public static final FieldAugment> JCCompilationUnit_comments = FieldAugment.augment(JCCompilationUnit.class, List.class, "lombok$comments"); + public static final FieldAugment> JCCompilationUnit_textBlockStarts = FieldAugment.augment(JCCompilationUnit.class, List.class, "lombok$textBlockStarts"); - public static CommentCatcher create(Context context) { - registerCommentsCollectingScannerFactory(context); + public static CommentCatcher create(Context context, boolean findTextBlocks) { + registerCommentsCollectingScannerFactory(context, findTextBlocks); JavaCompiler compiler = new JavaCompiler(context); setInCompiler(compiler, context); @@ -69,7 +70,12 @@ public class CommentCatcher { return list == null ? Collections.emptyList() : list; } - private static void registerCommentsCollectingScannerFactory(Context context) { + public List getTextBlockStarts(JCCompilationUnit ast) { + List list = JCCompilationUnit_textBlockStarts.get(ast); + return list == null ? Collections.emptyList() : list; + } + + private static void registerCommentsCollectingScannerFactory(Context context, boolean findTextBlocks) { try { Class scannerFactory; int javaCompilerVersion = Javac.getJavaCompilerVersion(); @@ -79,6 +85,7 @@ public class CommentCatcher { scannerFactory = Class.forName("lombok.javac.java7.CommentCollectingScannerFactory"); } else { scannerFactory = Class.forName("lombok.javac.java8.CommentCollectingScannerFactory"); + if (findTextBlocks) Permit.getField(scannerFactory, "findTextBlocks").set(null, true); } Permit.getMethod(scannerFactory, "preRegister", Context.class).invoke(null, context); } catch (InvocationTargetException e) { diff --git a/src/utils/lombok/javac/java8/CommentCollectingParser.java b/src/utils/lombok/javac/java8/CommentCollectingParser.java index b49312cb..c1dc2f7e 100644 --- a/src/utils/lombok/javac/java8/CommentCollectingParser.java +++ b/src/utils/lombok/javac/java8/CommentCollectingParser.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013-2014 The Project Lombok Authors. + * Copyright (C) 2013-2019 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -22,10 +22,7 @@ package lombok.javac.java8; import static lombok.javac.CommentCatcher.JCCompilationUnit_comments; - -import java.util.List; - -import lombok.javac.CommentInfo; +import static lombok.javac.CommentCatcher.JCCompilationUnit_textBlockStarts; import com.sun.tools.javac.parser.JavacParser; import com.sun.tools.javac.parser.Lexer; @@ -44,8 +41,8 @@ class CommentCollectingParser extends JavacParser { public JCCompilationUnit parseCompilationUnit() { JCCompilationUnit result = super.parseCompilationUnit(); if (lexer instanceof CommentCollectingScanner) { - List comments = ((CommentCollectingScanner)lexer).getComments(); - JCCompilationUnit_comments.set(result, comments); + JCCompilationUnit_comments.set(result, ((CommentCollectingScanner) lexer).getComments()); + JCCompilationUnit_textBlockStarts.set(result, ((CommentCollectingScanner) lexer).getTextBlockStarts()); } return result; } diff --git a/src/utils/lombok/javac/java8/CommentCollectingScanner.java b/src/utils/lombok/javac/java8/CommentCollectingScanner.java index b59a9390..5a0647cc 100644 --- a/src/utils/lombok/javac/java8/CommentCollectingScanner.java +++ b/src/utils/lombok/javac/java8/CommentCollectingScanner.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 The Project Lombok Authors. + * Copyright (C) 2013-2019 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -39,4 +39,8 @@ public class CommentCollectingScanner extends Scanner { public List getComments() { return tokenizer.getComments(); } + + public List getTextBlockStarts() { + return tokenizer.getTextBlockStarts(); + } } \ No newline at end of file diff --git a/src/utils/lombok/javac/java8/CommentCollectingScannerFactory.java b/src/utils/lombok/javac/java8/CommentCollectingScannerFactory.java index fa79ff67..cb0d2e12 100644 --- a/src/utils/lombok/javac/java8/CommentCollectingScannerFactory.java +++ b/src/utils/lombok/javac/java8/CommentCollectingScannerFactory.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2011-2013 The Project Lombok Authors. + * Copyright (C) 2011-2019 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -28,7 +28,7 @@ import com.sun.tools.javac.parser.ScannerFactory; import com.sun.tools.javac.util.Context; public class CommentCollectingScannerFactory extends ScannerFactory { - + public static boolean findTextBlocks; @SuppressWarnings("all") public static void preRegister(final Context context) { if (context.get(scannerFactoryKey) == null) { @@ -76,7 +76,7 @@ public class CommentCollectingScannerFactory extends ScannerFactory { public Scanner newScanner(CharSequence input, boolean keepDocComments) { if (input instanceof CharBuffer) { CharBuffer buf = (CharBuffer) input; - return new CommentCollectingScanner(this, new CommentCollectingTokenizer(this, buf)); + return new CommentCollectingScanner(this, new CommentCollectingTokenizer(this, buf, findTextBlocks)); } char[] array = input.toString().toCharArray(); return newScanner(array, array.length, keepDocComments); @@ -84,6 +84,6 @@ public class CommentCollectingScannerFactory extends ScannerFactory { @Override public Scanner newScanner(char[] input, int inputLength, boolean keepDocComments) { - return new CommentCollectingScanner(this, new CommentCollectingTokenizer(this, input, inputLength)); + return new CommentCollectingScanner(this, new CommentCollectingTokenizer(this, input, inputLength, findTextBlocks)); } } diff --git a/src/utils/lombok/javac/java8/CommentCollectingTokenizer.java b/src/utils/lombok/javac/java8/CommentCollectingTokenizer.java index 1834fb00..08477e61 100644 --- a/src/utils/lombok/javac/java8/CommentCollectingTokenizer.java +++ b/src/utils/lombok/javac/java8/CommentCollectingTokenizer.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 The Project Lombok Authors. + * Copyright (C) 2013-2019 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -39,19 +39,30 @@ import com.sun.tools.javac.util.ListBuffer; class CommentCollectingTokenizer extends JavaTokenizer { private int prevEndPosition = 0; private final ListBuffer comments = new ListBuffer(); + private final ListBuffer textBlockStarts; private int endComment = 0; - - CommentCollectingTokenizer(ScannerFactory fac, char[] buf, int inputLength) { + + CommentCollectingTokenizer(ScannerFactory fac, char[] buf, int inputLength, boolean findTextBlocks) { super(fac, new PositionUnicodeReader(fac, buf, inputLength)); + textBlockStarts = findTextBlocks ? new ListBuffer() : null; } - CommentCollectingTokenizer(ScannerFactory fac, CharBuffer buf) { + CommentCollectingTokenizer(ScannerFactory fac, CharBuffer buf, boolean findTextBlocks) { super(fac, new PositionUnicodeReader(fac, buf)); + textBlockStarts = findTextBlocks ? new ListBuffer() : null; + } + + int pos() { + return ((PositionUnicodeReader) reader).pos(); } @Override public Token readToken() { Token token = super.readToken(); - prevEndPosition = ((PositionUnicodeReader)reader).pos(); + prevEndPosition = pos(); + if (textBlockStarts != null && (prevEndPosition - token.pos > 5) && token.getClass().getSimpleName().equals("StringToken")) { + char[] start = reader.getRawCharacters(token.pos, token.pos + 3); + if (start[0] == '"' && start[1] == '"' && start[2] == '"') textBlockStarts.add(token.pos); + } return token; } @@ -113,6 +124,10 @@ class CommentCollectingTokenizer extends JavaTokenizer { return comments.toList(); } + public List getTextBlockStarts() { + return textBlockStarts == null ? List.nil() : textBlockStarts.toList(); + } + static class PositionUnicodeReader extends UnicodeReader { protected PositionUnicodeReader(ScannerFactory sf, char[] input, int inputLength) { super(sf, input, inputLength); diff --git a/src/utils/lombok/javac/java9/CommentCollectingParser.java b/src/utils/lombok/javac/java9/CommentCollectingParser.java index 307be405..034b6705 100644 --- a/src/utils/lombok/javac/java9/CommentCollectingParser.java +++ b/src/utils/lombok/javac/java9/CommentCollectingParser.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013-2017 The Project Lombok Authors. + * Copyright (C) 2013-2019 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -22,10 +22,8 @@ package lombok.javac.java9; import static lombok.javac.CommentCatcher.JCCompilationUnit_comments; +import static lombok.javac.CommentCatcher.JCCompilationUnit_textBlockStarts; -import java.util.List; - -import lombok.javac.CommentInfo; import lombok.javac.java8.CommentCollectingScanner; import com.sun.tools.javac.parser.JavacParser; @@ -45,8 +43,8 @@ class CommentCollectingParser extends JavacParser { public JCCompilationUnit parseCompilationUnit() { JCCompilationUnit result = super.parseCompilationUnit(); if (lexer instanceof CommentCollectingScanner) { - List comments = ((CommentCollectingScanner)lexer).getComments(); - JCCompilationUnit_comments.set(result, comments); + JCCompilationUnit_comments.set(result, ((CommentCollectingScanner) lexer).getComments()); + JCCompilationUnit_textBlockStarts.set(result, ((CommentCollectingScanner) lexer).getTextBlockStarts()); } return result; } -- cgit