diff --git a/compiler/Java_CodeGen.jrag b/compiler/Java_CodeGen.jrag
index b03b3e074b1f1dce7e782c2ef4257b7153359508..4b6891d706e88931601823814ccbffcf8f1b19ee 100644
--- a/compiler/Java_CodeGen.jrag
+++ b/compiler/Java_CodeGen.jrag
@@ -320,27 +320,6 @@ aspect Java_Register {
   }
 
 }
-aspect User_Types {
-  syn String Type.getTypeName();
-  eq Type.getTypeName() = getClass().getName();
-  eq PrimType.getTypeName() = getName();
-  eq UserType.getTypeName() = getName();
-
-  syn boolean Type.isUserType();
-  eq Type.isUserType() = false;
-  eq UserType.isUserType() = true;
-
- 
-  coll Set<Decl> Decl.references() [new HashSet<Decl>()] with add;
-
-  Field contributes ((UserType)getType()).decl()   
-  when parentDecl() != null && getType().isUserType()
-  to Decl.references() 
-  for parentDecl();
-
-  syn boolean Decl.hasReferences();
-  eq Decl.hasReferences() = !references().isEmpty();
-}
 
 aspect Java_Class {
 
@@ -356,13 +335,14 @@ aspect Java_Class {
       pp(env.getPrintStream());
 
       Java_emitUserTypeDeps(env, null, false);
+      Java_emitUserTypeRefs(env, null, false);
       env.println("*/");
 
   }
  
   public void Decl.Java_emitUserTypeDeps(Java_env env, String via, boolean outputCode) {
-    if( hasReferences() ) {
-        Iterator<Decl> it = references().iterator();
+    if( hasDependencies() ) {
+        Iterator<Decl> it = type_dependencies().iterator();
         while(it.hasNext()) {
             Decl t = it.next();
 
@@ -376,10 +356,24 @@ aspect Java_Class {
             }
         }
     } 
-//    else {
-//        env.println(" //no more deps ");
-//    }
   }
+  public void Decl.Java_emitUserTypeRefs(Java_env env, String via, boolean outputCode) {
+    if( isReferenced() ) {
+        Iterator<Decl> it = type_references().iterator();
+        while(it.hasNext()) {
+            Decl t = it.next();
+
+            t.Java_emitUserTypeRefs(env, t.getName(), outputCode);
+            if(outputCode) {
+               env.println(t.getName()+".register(e);");
+            } else {  // Just output a comment
+	        String refpath = (via == null) ? "directly" : "indirectly via "+via;
+	       env.println(" //Is referenced by ("+refpath+") on "+t.getName() );
+            }
+        }
+    } 
+ }
+  
 
   public void Decl.Java_emitRegisterEncoder(Java_env env) {
     env.println("public static void register(Encoder e) throws IOException {");
@@ -403,19 +397,19 @@ aspect Java_Class {
           env.println("import java.io.IOException;");
           env.println("import se.lth.control.labcomm"+env.verStr+".Decoder;");
       }
-      if (getType().Java_needInstance() || hasReferences()) {
+      if (getType().Java_needInstance() || hasDependencies()) {
           env.println("import se.lth.control.labcomm"+env.verStr+".Encoder;");
           env.println();
       }
 
-      // For types without references and not needing an instance, 
+      // For types without type_dependencies and not needing an instance, 
       // currently just an empty class is generated
 
       env.println("public class " + getName() + " implements SampleType {");
       env.println();
 
       env.indent();
-      if(hasReferences()) {
+      if(hasDependencies() || isReferenced()) {
         // XXX Java_emitRegisterEncoder(env);
         // XXX Java_emitDispatcher(env, false);
       }
diff --git a/compiler/TypeReferences.jrag b/compiler/TypeReferences.jrag
new file mode 100644
index 0000000000000000000000000000000000000000..81ca242ab0f834c01ff79f3c57e68938b56017e1
--- /dev/null
+++ b/compiler/TypeReferences.jrag
@@ -0,0 +1,35 @@
+aspect User_Types {
+  syn String Type.getTypeName();
+  eq Type.getTypeName() = getClass().getName();
+  eq PrimType.getTypeName() = getName();
+  eq UserType.getTypeName() = getName();
+
+  syn boolean Type.isUserType();
+  eq Type.isUserType() = false;
+  eq UserType.isUserType() = true;
+}
+
+aspect Type_References {
+ 
+  // The dependencies on other type declarations for a Decl.
+  coll Set<Decl> Decl.type_dependencies() [new HashSet<Decl>()] with add;
+
+  Field contributes ((UserType)getType()).decl()   
+  when parentDecl() != null && getType().isUserType()
+  to Decl.type_dependencies() 
+  for parentDecl();
+
+
+  // The references from other type declarations to a Decl.
+  coll Set<Decl> Decl.type_references() [new HashSet<Decl>()] with add;
+
+  Decl contributes this
+  to Decl.type_references()
+  for each type_dependencies();
+
+  syn boolean Decl.hasDependencies();
+  eq Decl.hasDependencies() = !type_dependencies().isEmpty();
+
+  syn boolean Decl.isReferenced();
+  eq Decl.isReferenced() = !type_references().isEmpty();
+}