package ceylon.language;

import com.redhat.ceylon.compiler.java.metadata.Ceylon;
import com.redhat.ceylon.compiler.java.metadata.Method;
import com.redhat.ceylon.compiler.java.metadata.Name;
import com.redhat.ceylon.compiler.java.metadata.TypeInfo;

@Ceylon(major = 3)
@Method
public final class className_ {
    
    private className_() {}
    
    public static java.lang.String className(@Name("obj")
    @TypeInfo("ceylon.language::Object")
    final java.lang.Object object) {
    	//TODO: type args?
        Name annot = object.getClass().getAnnotation(Name.class);
        if (annot != null) {
            if (object.getClass().getPackage() != null) {
                return object.getClass().getPackage().getName() + "::" + annot.value();
            } else {
                return annot.value();
            }
        } else {
            Ceylon ann = object.getClass().getAnnotation(Ceylon.class);
            java.lang.String pkg = object.getClass().getPackage() == null ? ""
                    : mangle(object.getClass().getPackage().getName(), ann);
            java.lang.String name = mangle(object.getClass().getSimpleName(), ann);
            // FIXME The next part really shouldn't be necessary as long as we add @Name
            // annotations to all classes with mangled names
            return pkg + "::" + name;
        }
    }

    private final static java.lang.String mangle(java.lang.String s, Ceylon ann) {
        int i = s.indexOf("$");
        if (i>0) {
            s = s.substring(0, i) + s.substring(i+1);
        }
        if (s.endsWith("_") && ann != null) {
            s = s.substring(0, s.length()-1);
        }
        return s;
    }

}

@@ -986,38 +986,11 @@
 ? %22%22
-%0A                    : mangle(
+ : 
 obje
@@ -1021,30 +1021,24 @@
 ().getName()
-, ann)
 ;%0A          
@@ -1067,55 +1067,313 @@
 e = 
-mangle(object.getClass().getSimpleName(), ann);
+object.getClass().getSimpleName();%0A            if (name.isEmpty()) %7B%0A                name = object.getClass().getName();%0A                //Remove the package from the classname%0A                if (!pkg.isEmpty()) %7B%0A                    name = name.substring(pkg.length()+1);%0A                %7D%0A            %7D
 %0A   
@@ -1398,12 +1398,14 @@
 The 
-next
+mangle
  par
@@ -1544,25 +1544,51 @@
 urn 
-pkg + %22::%22 + name
+mangle(pkg, ann) + %22::%22 + mangle(name, ann)
 ;%0A  

Fix #152 for Callables