]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/environment/AmbiguousNameException.java
Merge "Adding more detailed message to thrown exceptions in SyncElementFactory"
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / environment / AmbiguousNameException.java
1 package org.simantics.scl.compiler.environment;
2
3 import java.util.Arrays;
4 import java.util.Collection;
5
6 /**
7  * An exception for a name conflict between imported SCL modules. This exception is thrown by the
8  * methods of the {@link Namespace} class.
9  */
10 public class AmbiguousNameException extends Exception {
11     private static final long serialVersionUID = 1448746846203589730L;
12     
13     public final String[] conflictingModules;
14     public final String name;
15     
16     /**
17      * Construct the exception with a collection of modules and a name.
18      * @param conflictingModules  a collection of modules that are in name conflict
19      * @param name  the conflicting name
20      */
21     public AmbiguousNameException(Collection<String> conflictingModules, String name) {
22         this.conflictingModules = conflictingModules.toArray(new String[conflictingModules.size()]);
23         Arrays.sort(this.conflictingModules);
24         this.name = name;
25     }
26     
27     @Override
28     public synchronized Throwable fillInStackTrace() {
29         return this;
30     }
31     
32     @Override
33     public String getMessage() {
34         StringBuilder b = new StringBuilder();
35         b.append("Ambiguous reference to " + name + ". It can be found from modules ");
36         for(int i=0;i<conflictingModules.length;++i) {
37             if(i > 0) {
38                 if(i == conflictingModules.length-1)
39                     b.append(" and ");
40                 else
41                     b.append(", ");
42             }
43             b.append(conflictingModules[i]);
44         }
45         b.append('.');
46         return b.toString();
47     }
48 }