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

com.evento.parser.java.JavaProjectorParser Maven / Gradle / Ivy

There is a newer version: ev1.10.2
Show newest version
package com.evento.parser.java;

import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration;
import com.evento.parser.model.component.Projector;
import com.evento.parser.model.handler.EventHandler;
import com.evento.parser.model.payload.Event;
import org.jaxen.JaxenException;

import java.util.List;
import java.util.stream.Collectors;

/**
 * A parser for a Java Projector component, extending the JavaComponentParser class.
 */
public class JavaProjectorParser extends JavaComponentParser {


	/**
	 * Creates a JavaProjectorParser object.
	 *
	 * @param node the Node object representing the Java projector component
	 */
	public JavaProjectorParser(Node node) {
		super(node);
	}

	@SuppressWarnings("deprecation")
	private List findEventHandlers() throws JaxenException {
		var query = getQueryForAnnotatedMethod("EventHandler");
		return node.getFirstChildOfType(ASTTypeDeclaration.class).findChildNodesWithXPath(query).stream().map(
				n -> {
					var md = (ASTMethodDeclaration) n;
					var eventName = md.getFormalParameters().getFirstDescendantOfType(ASTClassOrInterfaceType.class).getImage();
					return new EventHandler(new Event(eventName), n.getBeginLine());
				}
		).collect(Collectors.toList());
	}


	public Projector parse() throws Exception {
		Projector projector = new Projector();
		projector.setComponentName(getDeclaredClassName());
		projector.setEventHandlers(findEventHandlers());
		findCommandInvocations(projector.getEventHandlers());
		return projector;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy