Adds a single class to this compilation unit, checking for duplicates and reporting errors. If a duplicate is detected, emits a SyntaxException describing the conflict and its possible resolution. Removes any pending compilation entry for the class if it exists. @param node the {@link Class
(ClassNode node)
| 265 | * @param node the {@link ClassNode} to add |
| 266 | */ |
| 267 | public void addClass(ClassNode node) { |
| 268 | node = node.redirect(); |
| 269 | String name = node.getName(); |
| 270 | ClassNode stored = classes.get(name); |
| 271 | if (stored != null && stored != node) { |
| 272 | // we have a duplicate class! |
| 273 | // One possibility for this is, that we declared a script and a |
| 274 | // class in the same file and named the class like the file |
| 275 | SourceUnit nodeSource = node.getModule().getContext(); |
| 276 | SourceUnit storedSource = stored.getModule().getContext(); |
| 277 | String txt = "Invalid duplicate class definition of class " + node.getName() + " : "; |
| 278 | if (nodeSource == storedSource) { |
| 279 | // same class in same source |
| 280 | txt += "The source " + nodeSource.getName() + " contains at least two definitions of the class " + node.getName() + ".\n"; |
| 281 | if (node.isScriptBody() || stored.isScriptBody()) { |
| 282 | txt += "One of the classes is an explicit generated class using the class statement, the other is a class generated from" + |
| 283 | " the script body based on the file name. Solutions are to change the file name or to change the class name.\n"; |
| 284 | } |
| 285 | } else { |
| 286 | txt += "The sources " + nodeSource.getName() + " and " + storedSource.getName() + " each contain a class with the name " + node.getName() + ".\n"; |
| 287 | } |
| 288 | nodeSource.addErrorAndContinue(new SyntaxException(txt, node)); |
| 289 | } |
| 290 | classes.put(name, node); |
| 291 | |
| 292 | ClassNode cn = classesToCompile.remove(name); |
| 293 | if (cn != null) cn.setRedirect(node); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Marks a class for compilation and associates it with its source location. |
no test coverage detected