/**
 * The MIT License
 * Copyright (c) 2003 David G Jones
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package info.dgjones.st2x.transform.method.intra;

import java.util.List;

import info.dgjones.st2x.ClassParser;
import info.dgjones.st2x.JavaMethod;
import info.dgjones.st2x.javatoken.JavaCallStart;
import info.dgjones.st2x.javatoken.JavaIdentifier;
import info.dgjones.st2x.javatoken.JavaKeyword;
import info.dgjones.st2x.javatoken.JavaToken;
import info.dgjones.st2x.transform.method.AbstractMethodBodyTransformation;
import info.dgjones.st2x.transform.tokenmatcher.TokenMatcher;
import info.dgjones.st2x.transform.tokenmatcher.TokenMatcherFactory;



public class TransformCreateCall extends AbstractMethodBodyTransformation {

	public TransformCreateCall() {
		super();
	}
	public TransformCreateCall(TokenMatcherFactory factory) {
		super(factory);
	}

	protected TokenMatcher matchers(TokenMatcherFactory factory) {
		return factory.token(JavaCallStart.class, "create.*");
	}

	protected int transform(JavaMethod javaMethod, List tokens, int i) {
		JavaCallStart call = (JavaCallStart)tokens.get(i);
		if (ClassParser.NON_CONSTRUCTORS.contains(call.value)) {
			return i;
		}
		if (i > 0 && (tokens.get(i - 1) instanceof JavaIdentifier)) {
			JavaToken token = (JavaToken) tokens.get(i - 1);
			if (token.value.equals("super")) {
				return i;
			} else if (token.value.equals(javaMethod.javaClass.className) && javaMethod.isConstructor()) {
				call.value = "this";
				tokens.remove(i-1);
				return i-1;
			}
			call.value = token.value;
			tokens.remove(i - 1);
			tokens.add(i - 1, new JavaKeyword("new"));
		} else if (javaMethod.isConstructor()) {
			call.value = "this";
		} else {
			call.value = javaMethod.javaClass.className;
			tokens.add(i, new JavaKeyword("new"));
		}
		return i;
	}
}
