]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/environment/AmbiguousNameException.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / environment / AmbiguousNameException.java
diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/environment/AmbiguousNameException.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/environment/AmbiguousNameException.java
new file mode 100644 (file)
index 0000000..c9b9de0
--- /dev/null
@@ -0,0 +1,48 @@
+package org.simantics.scl.compiler.environment;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+/**
+ * An exception for a name conflict between imported SCL modules. This exception is thrown by the
+ * methods of the {@link Namespace} class.
+ */
+public class AmbiguousNameException extends Exception {
+    private static final long serialVersionUID = 1448746846203589730L;
+    
+    public final String[] conflictingModules;
+    public final String name;
+    
+    /**
+     * Construct the exception with a collection of modules and a name.
+     * @param conflictingModules  a collection of modules that are in name conflict
+     * @param name  the conflicting name
+     */
+    public AmbiguousNameException(Collection<String> conflictingModules, String name) {
+        this.conflictingModules = conflictingModules.toArray(new String[conflictingModules.size()]);
+        Arrays.sort(this.conflictingModules);
+        this.name = name;
+    }
+    
+    @Override
+    public synchronized Throwable fillInStackTrace() {
+        return this;
+    }
+    
+    @Override
+    public String getMessage() {
+        StringBuilder b = new StringBuilder();
+        b.append("Ambiguous reference to " + name + ". It can be found from modules ");
+        for(int i=0;i<conflictingModules.length;++i) {
+            if(i > 0) {
+                if(i == conflictingModules.length-1)
+                    b.append(" and ");
+                else
+                    b.append(", ");
+            }
+            b.append(conflictingModules[i]);
+        }
+        b.append('.');
+        return b.toString();
+    }
+}