This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 45108

Summary: New Project wizard, whenever asking for an existing Java package root, should try to verify package structure
Product: java Reporter: Jesse Glick <jglick>
Component: ProjectAssignee: Tomas Zezula <tzezula>
Status: NEW ---    
Severity: blocker CC: anebuzelsky, johnjullion, jrojcek
Priority: P3 Keywords: API, UI
Version: 4.x   
Hardware: All   
OS: All   
Issue Type: ENHANCEMENT Exception Reporter:
Bug Depends on: 49685, 51151    
Bug Blocks: 41535, 54165    

Description Jesse Glick 2004-06-17 15:48:58 UTC
It would be nice if whenever in a New Project
wizard you were asked to select an existing Java
package root, the wizard would do a quick check
for representative Java sources and verify that
the package names inside each really correspond to
their location within the package root. If not,
the wizard should show a warning message (and
possibly disable the Next/Finish button, though
there may be exceptional cases where this would be
very annoying).

Could be a static utility method somewhere in
java/project, perhaps? Or even in java/api since
it is not exactly project-specific. Could possibly
have one method to find the Java package name for
a given FileObject - returning a String package
name (possibly "") if it could be parsed
correctly, or null if the file was malformed
somehow - used also by DefaultClassPathProvider;
and a utility method to verify a package root, by
looking for some **/*.java and checking the
package names against the relative paths.
Comment 1 Jesse Glick 2004-06-17 15:49:27 UTC
May be too late for D. Should be considered a priority for E, however.
Comment 2 Jesse Glick 2004-06-17 15:51:07 UTC
An additional possible utility method would be to take a folder and
scan for likely package roots inside of it. However I am not sure any
of our existing wizards would use such a method.

Package root verification would apply to j2seproject import; freeform
project creation and customization; possibly to web app import, TBD.
Comment 3 Jesse Glick 2005-07-21 16:57:14 UTC
*** Issue 61234 has been marked as a duplicate of this issue. ***
Comment 4 Antonin Nebuzelsky 2005-07-22 09:36:06 UTC
Please, reevaluate. Will this be done for F?
Comment 5 Tomas Zezula 2005-07-22 10:52:57 UTC
Easy to do, it can be done to 4.2 as a fix
Comment 6 Jesse Glick 2006-06-28 22:49:13 UTC
Something like this:

String WHITESPACE = "(?:\\s|/\\*(?:[^*]|\\*[^/])*\\*/)";
String IDENTIFIER = "(?:\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*)";
String QUALIFIED_IDENTIFIER = "(?:" + IDENTIFIER + "(?:\\." + IDENTIFIER + ")*)";
Pattern PACKAGE = Pattern.compile(
    "^" + WHITESPACE + "*(?:package" + WHITESPACE + "+(" + QUALIFIED_IDENTIFIER +
    ")|(?:import|public|final|abstract|strictfp|class|interface|enum))",
Pattern.DOTALL);
FileObject findRoot(FileObject file) {
  if (file.isFolder()) {
    // could also scan for contained files here
    return null;
  }
  // look for package statement
  try {
    InputStream is = file.getInputStream();
    try {
      ByteArrayOutputStream baos = new ByteArrayOutputStream((int) file.getSize());
      FileUtil.copy(is, baos);
      String content = baos.toString(); // XXX default encoding OK?
      Matcher m = PACKAGE.matcher(content);
      if (m.find()) {
        String pkg = m.group(1);
        FileObject root = file.getParent();
        List<String> components = new ArrayList<String>();
        if (pkg != null) {
          StringTokenizer tok = new StringTokenizer(pkg, ".");
          while (tok.hasMoreTokens()) {
            components.add(tok.nextToken());
          }
          Collections.reverse(components);
        }
        for (String component : components) {
          if (root == null || !root.getNameExt().equals(component)) {
            return null;
          }
          root = root.getParent();
        }
        return root;
      }
    } finally {
      is.close();
    }
  } catch (IOException x) {
    Exceptions.printStackTrace(x);
  }
  return null;
}