package jp.naist.sd.kenja.factextractor.ast;

import jp.naist.sd.kenja.factextractor.Blob;
import jp.naist.sd.kenja.factextractor.Blobable;

import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;

public class ASTField implements Blobable{

	private Multimap<String, Blob> fieldMap;

	public ASTField() {
		fieldMap = HashMultimap.create();
	}

	public void parseFieldDeclaration(FieldDeclaration node) {
		for(Object obj: node.fragments()){
			VariableDeclarationFragment fragment = (VariableDeclarationFragment)obj;
			String fieldName = fragment.getName().toString();
			Blob fieldBlob = new Blob(fieldName);

			if (fieldMap.containsKey(fieldName)) {
				int i = 0;
				for (Blob field : fieldMap.get(fieldName)) {
					conflict(field, fieldName, i++);
				}
				conflict(fieldBlob, fieldName, i);
			}
			fieldMap.put(fieldName, fieldBlob);
		}
	}
	
	private void conflict(Blob blob, String fieldName, int numConflicted) {
		String newName = fieldName + ".conflicted" + numConflicted;
		blob.setName(newName);
	}

	@Override
	public Iterable<Blob> getBlobs() {
		return fieldMap.values();
	}
}

@@ -335,16 +335,51 @@
 ultimap;
+%0Aimport java.lang.reflect.Modifier;
 %0A%0Apublic
@@ -745,16 +745,119 @@
 tring();
+%0A%0A%09%09%09int fieldModifier = node.getModifiers();%0A%09%09%09String fieldVisibility = getVisibility(fieldModifier);
 %0A%09%09%09Blob
@@ -878,16 +878,40 @@
 ew Blob(
+fieldVisibility + %22%5Cn%22, 
 fieldNam
@@ -1292,16 +1292,16 @@
 licted;%0A
-
 %09%09blob.s
@@ -1321,16 +1321,304 @@
 e);%0A%09%7D%0A%0A
+%09private String getVisibility(int modifier) %7B%0A%09%09if (Modifier.isPrivate(modifier)) %7B%0A%09%09%09return %22%5Bprivate%5D%22;%0A%09%09%7D else if (Modifier.isProtected(modifier)) %7B%0A%09%09%09return %22%5Bprotected%5D%22;%0A%09%09%7D else if (Modifier.isPublic(modifier)) %7B%0A%09%09%09return %22%5Bpublic%5D%22;%0A%09%09%7D else %7B%0A%09%09%09return %22%5Binternal%5D%22;%0A%09%09%7D%0A%09%7D%0A%0A
 %09@Overri

add field visibility