<commit_before>/**
 * 
 */
package org.minnal.instrument.resource;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.CtNewMethod;

import org.javalite.common.Inflector;
import org.minnal.core.Request;
import org.minnal.core.Response;
import org.minnal.core.server.exception.NotFoundException;
import org.minnal.instrument.entity.EntityNode;
import org.minnal.instrument.entity.EntityNode.EntityNodePath;

/**
 * @author ganeshs
 *
 */
public abstract class MethodCreator {

	private CtClass ctClass;
	
	private EntityNodePath path;
	
	protected static final String FIND_ENTITY_TEMPLATE = ":model_class :field_name = :model_class.first(new Object[]{\":entity_key\", " +
			"request.getHeader(\":param_name\")}); if (:field_name == null) {throw new " + NotFoundException.class.getName() + 
			"(\":field_name with :entity_key \" + request.getHeader(\":param_name\") + \" not found\");}";
	
	protected static final String FIND_COLLECTION_ITEM_TEMPLATE = ":model_class :field_name = :parent.collection(\":resource_name\").first(new Object[]{\":collection_entity_key\", " +
			"request.getHeader(\":param_name\")}); if (:field_name == null) {throw new " + NotFoundException.class.getName() + 
			"(\":field_name with :entity_key \" + request.getHeader(\":param_name\") + \" not found\");}";
	
	public MethodCreator(CtClass ctClass, EntityNodePath path) {
		this.ctClass = ctClass;
		this.path = path;
	}
	
	/**
	 * @return the path
	 */
	public EntityNodePath getPath() {
		return path;
	}

	public void create() throws Exception {
		createMethod(createBody());
	}
	
	protected abstract String createBody();
	
	protected boolean returnVoid() {
		return false;
	}
	
	protected void createMethod(String body) throws Exception {
		String methodName = getMethodName();
		if (methodExists(methodName)) {
			return;
		}
		if (ctClass.isFrozen()) {
			ctClass.defrost();
		}
		StringWriter writer = new StringWriter();
		writer.append("public ").append(returnVoid() ? "void " : "Object ").append(methodName).append("(").append(Request.class.getName()).append(" request, ");
		writer.append(Response.class.getName()).append(" response) {").append(body).append("}");
		CtMethod method = CtNewMethod.make(writer.toString(), ctClass);
		ctClass.addMethod(method);
	}
	
	public abstract String getMethodName();
	
	protected boolean methodExists(String methodName) {
		try {
			ctClass.getDeclaredMethod(methodName, new CtClass[]{ClassPool.getDefault().get(Request.class.getName()), 
					ClassPool.getDefault().get(Request.class.getName())});
			return true;
		} catch (javassist.NotFoundException e) {
			return false;
		}
	}
	
	protected void resolveTemplate(StringWriter writer, String template, EntityNode node, String paramName, String parent) {
		Map<String, String> placeholders = new HashMap<String, String>();<commit_msg>Fixed a bug in javaassit manipulation of methods<commit_after>/**
 * 
 */
package org.minnal.instrument.resource;

import java.io.StringWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.CtNewMethod;

import org.javalite.common.Inflector;
import org.minnal.core.Request;
import org.minnal.core.Response;
import org.minnal.core.server.exception.NotFoundException;
import org.minnal.instrument.entity.EntityNode;
import org.minnal.instrument.entity.EntityNode.EntityNodePath;

/**
 * @author ganeshs
 *
 */
public abstract class MethodCreator {

	private CtClass ctClass;
	
	private EntityNodePath path;
	
	protected static final String FIND_ENTITY_TEMPLATE = ":model_class :field_name = :model_class.first(new Object[]{\":entity_key\", " +
			"request.getHeader(\":param_name\")}); if (:field_name == null) {throw new " + NotFoundException.class.getName() + 
			"(\":field_name with :entity_key \" + request.getHeader(\":param_name\") + \" not found\");}";
	
	protected static final String FIND_COLLECTION_ITEM_TEMPLATE = ":model_class :field_name = :parent.collection(\":resource_name\").first(new Object[]{\":collection_entity_key\", " +
			"request.getHeader(\":param_name\")}); if (:field_name == null) {throw new " + NotFoundException.class.getName() + 
			"(\":field_name with :entity_key \" + request.getHeader(\":param_name\") + \" not found\");}";
	
	public MethodCreator(CtClass ctClass, EntityNodePath path) {
		this.ctClass = ctClass;
		this.path = path;
	}
	
	/**
	 * @return the path
	 */
	public EntityNodePath getPath() {
		return path;
	}

	public void create() throws Exception {
		createMethod(createBody());
	}
	
	protected abstract String createBody();
	
	protected boolean returnVoid() {
		return false;
	}
	
	protected void createMethod(String body) throws Exception {
		String methodName = getMethodName();
		if (methodExists(methodName)) {
			return;
		}
		if (ctClass.isFrozen()) {
			ctClass.defrost();
		}
		StringWriter writer = new StringWriter();
		writer.append("public ").append(returnVoid() ? "void " : "Object ").append(methodName).append("(").append(Request.class.getName()).append(" request, ");
		writer.append(Response.class.getName()).append(" response) {").append(body).append("}");
		CtMethod method = CtNewMethod.make(writer.toString(), ctClass);
		ctClass.addMethod(method);
	}
	
	public abstract String getMethodName();
	
	protected boolean methodExists(String methodName) {
		try {
			CtMethod[] methods = ctClass.getMethods();
			CtClass[] params = new CtClass[]{ClassPool.getDefault().get(Request.class.getName()), 
					ClassPool.getDefault().get(Response.class.getName())};
			for (CtMethod method : methods) {
				if (method.getName().equals(methodName) && Arrays.equals(method.getParameterTypes(), params)) {
					return true;
				}
			}
			return false;
		} catch (javassist.NotFoundException e) {
			return false;
		}
	}
	
	protected void resolveTemplate(StringWriter writer, String template, EntityNode node, String paramName, String parent) {
		Map<String, String> placeholders = new HashMap<String, String>();
{'fraction_non_alphanumeric': 0.14528543938422064, 'fraction_numerical': 0.0, 'mean_word_length': 4.90625, 'pattern_counts': {'":': 22, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4674364', 'n_tokens_mistral': 2084, 'n_tokens_neox': 1905, 'n_words': 406}