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

com.cflint.StackHandler Maven / Gradle / Ivy

Go to download

A static code analysis tool for ColdFusion (in the spirit of FindBugs and Lint). With CFLint, you are able to analyze your ColdFusion code base for code violations.

There is a newer version: 1.5.0
Show newest version
package com.cflint;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class StackHandler {

	Deque varStack = new ArrayDeque();

	public StackHandler() {
		super();
		excludes.add("VARIABLES");
		excludes.add("ARGUMENTS");
		excludes.add("SUPER");
		excludes.add("SESSION");
		excludes.add("APPLICATION");
		excludes.add("THIS");
		excludes.add("LOCAL");
		varStack.push(new Stack(""));
	}

	// Set variables = new HashSet();
	Set excludes = new HashSet();

	public void addVariable(final String name) {
		varStack.peek().getVariables().add(name.toUpperCase());
	}

	public void addArgument(final String name) {
		varStack.peek().getArguments().add(name.toUpperCase());
	}

	public boolean hasArgument(final String name) {
		return varStack.peek().getArguments().contains(name.toUpperCase());
	}

	public void push(final String pathName) {
		varStack.push(new Stack(pathName));
	}

	public void pop() {
		varStack.pop();
	}

	public Object getPluginVar(final Class clazz, final String var) {
		final String key = clazz.getName() + "_" + var;
		final Iterator iter = varStack.iterator();
		while (iter.hasNext()) {
			final Stack vars = iter.next();
			if (vars.getPluginvariables().containsKey(key)) {
				return vars.getPluginvariables().get(key);
			}
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public Collection getPluginAllVars(final Class clazz, final String var) {
		final List retval = new ArrayList();
		final String key = clazz.getName() + "_" + var;
		final Iterator iter = varStack.iterator();
		while (iter.hasNext()) {
			final Stack vars = iter.next();
			if (vars.getPluginvariables().containsKey(key)) {
				final Object value = vars.getPluginvariables().get(key);
				if (value instanceof Collection) {
					retval.addAll((Collection) value);
				} else {
					retval.add(value);
				}
			}
		}
		return retval;
	}

	public void setPluginVar(final Class clazz, final String var, final Object value) {
		final String key = clazz.getName() + "_" + var;
		varStack.peek().getPluginvariables().put(key, value);
	}

	public boolean isVariable(final String name) {
		final Iterator iter = varStack.iterator();
		while (iter.hasNext()) {
			final Stack vars = iter.next();
			if (vars.getVariables().contains(name.toUpperCase())) {
				return true;
			}
		}
		return false;
	}

	public boolean isArgument(final String name) {
		final Iterator iter = varStack.iterator();
		while (iter.hasNext()) {
			final Stack vars = iter.next();
			if (vars.getArguments().contains(name.toUpperCase())) {
				return true;
			}
		}
		return false;
	}

	public boolean checkVariable(final String name) {
		if (excludes.contains(name.toUpperCase())) {
			return true;
		}
		final Iterator iter = varStack.iterator();
		while (iter.hasNext()) {
			final Stack vars = iter.next();
			// Report only once per scope level
			if (vars.getReported().contains(name.toUpperCase())) {
				return true;
			}
			if (vars.getVariables().contains(name.toUpperCase())) {
				return true;
			}
			if (vars.getArguments().contains(name.toUpperCase())) {
				// Dereference of an argument without the arguments scope copies
				// to local scope
				vars.getVariables().add(name.toUpperCase());
				return true;
			}
		}
		// reported once gets pushed to avoid duplicates
		varStack.peek().getReported().add(name.toUpperCase());
		return false;
	}

	public static class Stack {
		public Stack(final String pathName) {
			super();
			this.pathName = pathName;
		}

		Set variables = new HashSet();
		Set reported = new HashSet();
		Map pluginvariables = new HashMap();
		Set arguments = new HashSet();
		String pathName;

		public String getPathName() {
			return pathName;
		}

		public void setPathName(final String pathName) {
			this.pathName = pathName;
		}

		public Set getVariables() {
			return variables;
		}

		public Set getArguments() {
			return arguments;
		}

		public Map getPluginvariables() {
			return pluginvariables;
		}

		public Set getReported() {
			return reported;
		}

	}
}