/*
 * Copyright (C) 2004-2008  exedio GmbH (www.exedio.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.exedio.cope.instrument;

import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import com.exedio.cope.Feature;
import com.exedio.cope.MandatoryViolationException;
import com.exedio.cope.Settable;

class CopeFeature
{
	static final String TAG_PREFIX = "cope.";
	static final String TAG_GETTER  = TAG_PREFIX + "get";
	static final String TAG_SETTER  = TAG_PREFIX + "set";
	static final String TAG_INITIAL = TAG_PREFIX + "initial";

	final CopeType parent;
	final JavaAttribute javaAttribute;
	final String name;
	final int modifier;
	final Visibility visibility;
	final String docComment;
	final Option setterOption;
	final boolean initial;
	private Feature value;
	
	CopeFeature(final CopeType parent, final JavaAttribute javaAttribute)
	{
		this.parent = parent;
		this.javaAttribute = javaAttribute;
		this.name = javaAttribute.name;
		this.modifier = javaAttribute.modifier;
		this.visibility = javaAttribute.getVisibility();

		this.docComment = javaAttribute.getDocComment();
		this.setterOption = new Option(Injector.findDocTagLine(docComment, TAG_SETTER), true);
		this.initial = Injector.hasTag(docComment, TAG_INITIAL);
		
		parent.register(this);
	}
	
	final JavaClass getParent()
	{
		return javaAttribute.parent;
	}
	
	final Feature getInstance()
	{
		if(value==null)
			value = (Feature)javaAttribute.evaluate();
		
		return value;
	}
	
	final boolean isInitial()
	{
		if(initial)
			return true;
		
		final Feature instance = getInstance();
		return instance instanceof Settable && ((Settable)instance).isInitial();
	}

	private final boolean isWriteable()
	{
		final Feature instance = getInstance();
		return instance instanceof Settable && !((Settable)instance).isFinal();
	}
	
	final boolean hasGeneratedSetter()
	{
		return isWriteable() && setterOption.exists;
	}
	
	final int getGeneratedSetterModifier()
	{
		return setterOption.getModifier(modifier);
	}
	
	final SortedSet<Class<? extends Throwable>> getInitialExceptions()
	{
		final Feature instance = getInstance();
		final Set<Class<? extends Throwable>> resultList = ((Settable<?>)instance).getInitialExceptions();
		final SortedSet<Class<? extends Throwable>> result = new TreeSet<Class<? extends Throwable>>(CopeType.CLASS_COMPARATOR);
		result.addAll(resultList);
		if(((Settable<?>)instance).getInitialType().isPrimitive())
			result.remove(MandatoryViolationException.class);
		return result;
	}
}

@@ -1096,118 +1096,8 @@
 .%22;%0A
-%09static final String TAG_GETTER  = TAG_PREFIX + %22get%22;%0A%09static final String TAG_SETTER  = TAG_PREFIX + %22set%22;%0A
 %09sta
@@ -1151,16 +1151,16 @@
 itial%22;%0A
+
 %0A%09final 
@@ -1313,36 +1313,8 @@
 nt;%0A
-%09final Option setterOption;%0A
 %09fin
@@ -1678,97 +1678,8 @@
 ();%0A
-%09%09this.setterOption = new Option(Injector.findDocTagLine(docComment, TAG_SETTER), true);%0A
 %09%09th
@@ -2142,353 +2142,8 @@
 %09%7D%0A%0A
-%09private final boolean isWriteable()%0A%09%7B%0A%09%09final Feature instance = getInstance();%0A%09%09return instance instanceof Settable && !((Settable)instance).isFinal();%0A%09%7D%0A%09%0A%09final boolean hasGeneratedSetter()%0A%09%7B%0A%09%09return isWriteable() && setterOption.exists;%0A%09%7D%0A%09%0A%09final int getGeneratedSetterModifier()%0A%09%7B%0A%09%09return setterOption.getModifier(modifier);%0A%09%7D%0A%09%0A
 %09fin

remove unused code