All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.uscexp.grappa.extension.nodes.AstTreeNode Maven / Gradle / Ivy

There is a newer version: 0.4.0-Beta
Show newest version
/*
 * Copyright (C) 2014 by haui - all rights reserved
 */
package com.github.uscexp.grappa.extension.nodes;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.parboiled.Node;
import org.parboiled.support.ParsingResult;
import org.parboiled.trees.MutableTreeNodeImpl;

import com.github.uscexp.grappa.extension.interpreter.AstInterpreter;
import com.github.uscexp.grappa.extension.interpreter.ProcessStore;

/**
 * base class of all {@link AstInterpreter} tree nodes. contains the {@link ParsingResult}
 * node and the value of the parse node (from input).
 * 
 * @author haui
 *
 */
public abstract class AstTreeNode extends MutableTreeNodeImpl> {

	protected Node parseNode;
	protected String value;

	public AstTreeNode(Node node, String value) {
		super();
		this.parseNode = node;
		this.value = value;
	}
	
	public void interpretIt(Long id) throws ReflectiveOperationException {
	    if( !ProcessStore.getInstance(id).checkPrecondition())
	        return;
		for (AstTreeNode astTreeNode : getChildren()) {
			astTreeNode.interpretIt(id);
		}
		interpret(id);
	}

	/**
	 * Interpret method
	 *
	 * @param id instance id
	 */
	protected abstract void interpret(Long id) throws ReflectiveOperationException;

	@SuppressWarnings("unchecked")
	protected  F convert(String value, Class valueType, Class factoryClass, String factoryMethod)
		throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException,
			InvocationTargetException {
		F result = null;
		if (!valueType.equals(Object.class)) {
			if (!factoryClass.equals(Object.class) && !factoryMethod.equals("")) {
				Method method = factoryClass.getDeclaredMethod(factoryMethod, String.class);
				result = (F) method.invoke(null, value);
			} else {
				Constructor constructor = valueType.getDeclaredConstructor(String.class);
				result = constructor.newInstance(value);
			}
		} else {
			result = (F) value;
		}
		return result;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy