< prev index next >

test/langtools/tools/lib/toolbox/ToolBox.java

Print this page




 633         public void write(Path dir) throws IOException {
 634             Path file = dir.resolve(getJavaFileNameFromSource(source));
 635             Files.createDirectories(file.getParent());
 636             try (BufferedWriter out = Files.newBufferedWriter(file)) {
 637                 out.write(source.replace("\n", lineSeparator));
 638             }
 639         }
 640 
 641         @Override
 642         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
 643             return source;
 644         }
 645 
 646         private static Pattern commentPattern =
 647                 Pattern.compile("(?s)(\\s+//.*?\n|/\\*.*?\\*/)");
 648         private static Pattern modulePattern =
 649                 Pattern.compile("module\\s+((?:\\w+\\.)*)");
 650         private static Pattern packagePattern =
 651                 Pattern.compile("package\\s+(((?:\\w+\\.)*)(?:\\w+))");
 652         private static Pattern classPattern =
 653                 Pattern.compile("(?:public\\s+)?(?:class|enum|interface)\\s+(\\w+)");
 654 
 655         /**
 656          * Extracts the Java file name from the class declaration.
 657          * This method is intended for simple files and uses regular expressions.
 658          * Comments in the source are stripped before looking for the
 659          * declarations from which the name is derived.
 660          */
 661         static String getJavaFileNameFromSource(String source) {
 662             StringBuilder sb = new StringBuilder();
 663             Matcher matcher = commentPattern.matcher(source);
 664             int start = 0;
 665             while (matcher.find()) {
 666                 sb.append(source.substring(start, matcher.start()));
 667                 start = matcher.end();
 668             }
 669             sb.append(source.substring(start));
 670             source = sb.toString();
 671 
 672             String packageName = null;
 673 




 633         public void write(Path dir) throws IOException {
 634             Path file = dir.resolve(getJavaFileNameFromSource(source));
 635             Files.createDirectories(file.getParent());
 636             try (BufferedWriter out = Files.newBufferedWriter(file)) {
 637                 out.write(source.replace("\n", lineSeparator));
 638             }
 639         }
 640 
 641         @Override
 642         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
 643             return source;
 644         }
 645 
 646         private static Pattern commentPattern =
 647                 Pattern.compile("(?s)(\\s+//.*?\n|/\\*.*?\\*/)");
 648         private static Pattern modulePattern =
 649                 Pattern.compile("module\\s+((?:\\w+\\.)*)");
 650         private static Pattern packagePattern =
 651                 Pattern.compile("package\\s+(((?:\\w+\\.)*)(?:\\w+))");
 652         private static Pattern classPattern =
 653                 Pattern.compile("(?:public\\s+)?(?:class|enum|interface|record)\\s+(\\w+)");
 654 
 655         /**
 656          * Extracts the Java file name from the class declaration.
 657          * This method is intended for simple files and uses regular expressions.
 658          * Comments in the source are stripped before looking for the
 659          * declarations from which the name is derived.
 660          */
 661         static String getJavaFileNameFromSource(String source) {
 662             StringBuilder sb = new StringBuilder();
 663             Matcher matcher = commentPattern.matcher(source);
 664             int start = 0;
 665             while (matcher.find()) {
 666                 sb.append(source.substring(start, matcher.start()));
 667                 start = matcher.end();
 668             }
 669             sb.append(source.substring(start));
 670             source = sb.toString();
 671 
 672             String packageName = null;
 673 


< prev index next >