/*
 * Copyright 2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.model.internal.type;

import com.google.common.collect.ImmutableList;

import java.lang.ref.WeakReference;

class ClassTypeWrapper implements TypeWrapper {
    private final WeakReference<Class<?>> reference;

    public ClassTypeWrapper(Class<?> clazz) {
        this.reference = new WeakReference<Class<?>>(clazz);
    }

    @Override
    public Class<?> unwrap() {
        return reference.get();
    }

    @Override
    public void collectClasses(ImmutableList.Builder<Class<?>> builder) {
        builder.add(unwrap());
    }

    @Override
    public String getRepresentation(boolean full) {
        if (full) {
            return unwrap().getName();
        } else {
            StringBuffer sb = new StringBuffer();
            Class<?> clazz = unwrap();
            sb.append(clazz.getSimpleName());
            for (Class<?> c = clazz.getEnclosingClass(); c != null; c = c.getEnclosingClass()) {
                sb.insert(0, '.');
                sb.insert(0, c.getSimpleName());
            }
            return sb.toString();
        }
    }
}

@@ -1305,16 +1305,77 @@
  else %7B%0A
+            Class%3C?%3E clazz = unwrap();%0A            try %7B%0A    
         
@@ -1386,18 +1386,19 @@
 StringBu
-ff
+ild
 er sb = 
@@ -1413,10 +1413,11 @@
 ngBu
-ff
+ild
 er()
@@ -1426,43 +1426,8 @@
     
-        Class%3C?%3E clazz = unwrap();%0A
     
@@ -1460,32 +1460,36 @@
 tSimpleName());%0A
+    
             for 
@@ -1577,32 +1577,36 @@
                 
+    
 sb.insert(0, '.'
@@ -1608,16 +1608,20 @@
 , '.');%0A
+    
         
@@ -1669,26 +1669,34 @@
             
-%7D%0A
+    %7D%0A    
             
@@ -1713,24 +1713,526 @@
 toString();%0A
+            %7D catch (NoClassDefFoundError ignore) %7B%0A                // This happens for IBM JDK 6 for nested interfaces -- see https://issues.apache.org/jira/browse/GROOVY-7010%0A                // Let's try to return something as close as possible to the intended value%0A                Package pkg = clazz.getPackage();%0A                int pkgPrefixLength = pkg == null ? 0 : pkg.getName().length() + 1;%0A                return clazz.getName().substring(pkgPrefixLength).replace('$', '.');%0A            %7D%0A
         %7D%0A  

Work around IBM JDK 6 error caused by ModelType.getDisplayName()