test/langtools/tools/lib/toolbox/ToolBox.java
changeset 48325 e5cdedd37b78
parent 47216 71c04702a3d5
child 50240 b9c483223a91
equal deleted inserted replaced
48323:23d427d8a1ff 48325:e5cdedd37b78
     1 /*
     1 /*
     2  * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
   522         @Override
   522         @Override
   523         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   523         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   524             return source;
   524             return source;
   525         }
   525         }
   526 
   526 
       
   527         private static Pattern commentPattern =
       
   528                 Pattern.compile("(?s)(\\s+//.*?\n|/\\*.*?\\*/)");
   527         private static Pattern modulePattern =
   529         private static Pattern modulePattern =
   528                 Pattern.compile("module\\s+((?:\\w+\\.)*)");
   530                 Pattern.compile("module\\s+((?:\\w+\\.)*)");
   529         private static Pattern packagePattern =
   531         private static Pattern packagePattern =
   530                 Pattern.compile("package\\s+(((?:\\w+\\.)*)(?:\\w+))");
   532                 Pattern.compile("package\\s+(((?:\\w+\\.)*)(?:\\w+))");
   531         private static Pattern classPattern =
   533         private static Pattern classPattern =
   532                 Pattern.compile("(?:public\\s+)?(?:class|enum|interface)\\s+(\\w+)");
   534                 Pattern.compile("(?:public\\s+)?(?:class|enum|interface)\\s+(\\w+)");
   533 
   535 
   534         /**
   536         /**
   535          * Extracts the Java file name from the class declaration.
   537          * Extracts the Java file name from the class declaration.
   536          * This method is intended for simple files and uses regular expressions,
   538          * This method is intended for simple files and uses regular expressions.
   537          * so comments matching the pattern can make the method fail.
   539          * Comments in the source are stripped before looking for the
       
   540          * declarations from which the name is derived.
   538          */
   541          */
   539         static String getJavaFileNameFromSource(String source) {
   542         static String getJavaFileNameFromSource(String source) {
       
   543             StringBuilder sb = new StringBuilder();
       
   544             Matcher matcher = commentPattern.matcher(source);
       
   545             int start = 0;
       
   546             while (matcher.find()) {
       
   547                 sb.append(source.substring(start, matcher.start()));
       
   548                 start = matcher.end();
       
   549             }
       
   550             sb.append(source.substring(start));
       
   551             source = sb.toString();
       
   552 
   540             String packageName = null;
   553             String packageName = null;
   541 
   554 
   542             Matcher matcher = modulePattern.matcher(source);
   555             matcher = modulePattern.matcher(source);
   543             if (matcher.find())
   556             if (matcher.find())
   544                 return "module-info.java";
   557                 return "module-info.java";
   545 
   558 
   546             matcher = packagePattern.matcher(source);
   559             matcher = packagePattern.matcher(source);
   547             if (matcher.find())
   560             if (matcher.find())