package sds.decompile;

import sds.assemble.BaseContent;
import sds.assemble.ClassContent;

/**
 * This class is for decompiling contents of class.
 * @author inagaki
 */
public class ClassDecompiler extends AbstractDecompiler {
	/**
	 * constructor.
	 * @param result decompiled source
	 */
	public ClassDecompiler(DecompiledResult result) {
		super(result);
	}

	@Override
	public void decompile(BaseContent content) {
		ClassContent cc = (ClassContent)content;
		addAnnotation(cc.getAnnotation());
		addDeclaration(cc);
		result.changeIndent(DecompiledResult.INCREMENT);

		FieldDecompiler fieldDecom = new FieldDecompiler(result);
		fieldDecom.decompile(cc.getFields());

		MethodDecompiler methodDecom = new MethodDecompiler(fieldDecom.getResult());
		methodDecom.decompile(cc.getMethods());

		// end
		result.writeEndScope();
	}

	@Override
	void addDeclaration(BaseContent content) {
		ClassContent cc = (ClassContent)content;
		StringBuilder classDeclaration = new StringBuilder();
		classDeclaration.append(cc.getAccessFlag()).append(cc.getThisClass())
						.append(" extends ").append(cc.getSuperClass());
		if(cc.getInterfaces().length > 0) {
			classDeclaration.append(" implements ");
			String[] interfaces = cc.getInterfaces();
			for(int i = 0; i < interfaces.length - 1; i++) {
				classDeclaration.append(interfaces[i]).append(", ");
			}
			classDeclaration.append(interfaces[interfaces.length - 1]);
		}
		classDeclaration.append(" {");
		result.write(classDeclaration.toString());
	}
}

@@ -17,16 +17,38 @@
 mpile;%0A%0A
+import java.util.Map;%0A
 import s
@@ -72,16 +72,16 @@
 ontent;%0A
-
 import s
@@ -767,16 +767,35 @@
 Result()
+, cc.getThisClass()
 );%0A%09%09met
@@ -1100,15 +1100,469 @@
 s())
+;%0A
 %0A%09%09
-%09%09%09%09
+// generics%0A%09%09if(cc.getGenericsMap().size() %3E 0) %7B%0A%09%09%09classDeclaration.append(%22%3C%22);%0A%09%09%09StringBuilder generics = new StringBuilder();%0A%09%09%09for(Map.Entry%3CString, String%3E e : cc.getGenericsMap().entrySet()) %7B%0A%09%09%09%09generics.append(e.getKey()).append(%22 extends %22).append(e.getValue()).append(%22,%22);%0A%09%09%09%7D%0A%09%09%09classDeclaration.append(generics.toString().substring(0, generics.length()-1 ));%0A%09%09%09classDeclaration.append(%22%3E%22);%0A%09%09%7D%0A%0A%09%09// extends classes%0A%09%09classDeclaration
 .app
@@ -1605,16 +1605,44 @@
 lass());
+%0A%0A%09%09// implements interfaces
 %0A%09%09if(cc
@@ -1942,16 +1942,17 @@
 %5D);%0A%09%09%7D%0A
+%0A
 %09%09classD
@@ -2024,10 +2024,9 @@
 ());%0A%09%7D%0A
+
 %7D
-%0A

add generics types.