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

org.walkmod.javalang.compiler.providers.RemoveUnusedSymbolsProvider Maven / Gradle / Ivy

There is a newer version: 2.3.10
Show newest version
/*
 Copyright (C) 2015 Raquel Pau and Albert Coroleu.
 
Walkmod is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Walkmod is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with Walkmod.  If not, see .*/
package org.walkmod.javalang.compiler.providers;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

import org.walkmod.javalang.ast.CompilationUnit;
import org.walkmod.javalang.ast.ImportDeclaration;
import org.walkmod.javalang.ast.Node;
import org.walkmod.javalang.ast.body.AnnotationDeclaration;
import org.walkmod.javalang.ast.body.AnnotationMemberDeclaration;
import org.walkmod.javalang.ast.body.ClassOrInterfaceDeclaration;
import org.walkmod.javalang.ast.body.ConstructorDeclaration;
import org.walkmod.javalang.ast.body.EmptyTypeDeclaration;
import org.walkmod.javalang.ast.body.EnumConstantDeclaration;
import org.walkmod.javalang.ast.body.EnumDeclaration;
import org.walkmod.javalang.ast.body.FieldDeclaration;
import org.walkmod.javalang.ast.body.MethodDeclaration;
import org.walkmod.javalang.ast.body.MultiTypeParameter;
import org.walkmod.javalang.ast.body.Parameter;
import org.walkmod.javalang.ast.body.TypeDeclaration;
import org.walkmod.javalang.ast.body.VariableDeclarator;
import org.walkmod.javalang.ast.expr.ObjectCreationExpr;
import org.walkmod.javalang.ast.expr.VariableDeclarationExpr;
import org.walkmod.javalang.ast.stmt.BlockStmt;
import org.walkmod.javalang.ast.type.ClassOrInterfaceType;
import org.walkmod.javalang.compiler.actions.ReferencesCounterAction;
import org.walkmod.javalang.compiler.actions.RemoveUnusedSymbolsAction;
import org.walkmod.javalang.compiler.symbols.SymbolAction;

public class RemoveUnusedSymbolsProvider implements SymbolActionProvider {

	private Stack> siblings = new Stack>();

	private List actions = Collections.emptyList();

	@Override
	public List getActions(ClassOrInterfaceDeclaration n) {
		buildActionList(n, n.getMembers());
		return actions;
	}

	@Override
	public List getActions(Parameter n) {

		return Collections.emptyList();
	}

	@Override
	public List getActions(ClassOrInterfaceType n) {

		return Collections.emptyList();
	}

	@Override
	public List getActions(MultiTypeParameter n) {

		return Collections.emptyList();
	}

	@Override
	public List getActions(VariableDeclarator n) {
		buildActionList();
		return actions;
	}

	@Override
	public List getActions(ImportDeclaration n) {
		buildActionList();
		return actions;
	}

	@Override
	public List getActions(MethodDeclaration n) {
		buildActionList(n, new LinkedList());
		return actions;
	}

	private void buildActionList(Node n, List children) {

		List lastsiblings = siblings.peek();
		while (!lastsiblings.contains(n) && !siblings.isEmpty()) {
			siblings.pop();
			lastsiblings = siblings.peek();
		}
		
		RemoveUnusedSymbolsAction unusedSymbols = new RemoveUnusedSymbolsAction(
				siblings.peek());
		
		actions = new LinkedList();
		actions.add(unusedSymbols);
		siblings.push(children);

	}

	private void buildActionList() {
		RemoveUnusedSymbolsAction unusedSymbols = new RemoveUnusedSymbolsAction(
				siblings.peek());
		actions = new LinkedList();
		actions.add(unusedSymbols);
	}

	@Override
	public List getActions(FieldDeclaration n) {
		buildActionList(n, n.getVariables());
		return actions;
	}

	@Override
	public List getActions(EnumDeclaration n) {

		buildActionList(n, n.getMembers());

		return actions;
	}

	@Override
	public List getActions(EnumConstantDeclaration n) {
		buildActionList();
		return actions;
	}

	@Override
	public List getActions(AnnotationDeclaration n) {

		buildActionList(n, n.getMembers());

		return actions;

	}

	@Override
	public List getActions(AnnotationMemberDeclaration n) {
		buildActionList();
		return actions;
	}

	@Override
	public List getActions(ConstructorDeclaration n) {
		buildActionList();
		return actions;
	}

	@Override
	public List getActions(CompilationUnit n) {
		siblings.clear();
		ReferencesCounterAction counter = new ReferencesCounterAction();
		List actions = new LinkedList();
		actions.add(counter);
		
		siblings.push(n.getTypes());
		if(n.getImports() != null){
			siblings.push(n.getImports());
		}
		return actions;
	}

	@Override
	public List getActions(EmptyTypeDeclaration n) {
		buildActionList(n, n.getMembers());

		return Collections.emptyList();
	}

	@Override
	public List getActions(TypeDeclaration n) {
		buildActionList(n, n.getMembers());
		return actions;
	}

	@Override
	public List getActions(BlockStmt n) {
		RemoveUnusedSymbolsAction unusedSymbols = new RemoveUnusedSymbolsAction(
				siblings.peek());
		actions = new LinkedList();
		actions.add(unusedSymbols);
		siblings.push(n.getStmts());
		return Collections.emptyList();
	}

	@Override
	public List getActions(VariableDeclarationExpr n) {
		RemoveUnusedSymbolsAction unusedSymbols = new RemoveUnusedSymbolsAction(
				siblings.peek());
		actions = new LinkedList();
		actions.add(unusedSymbols);
		siblings.push(n.getVars());
		return actions;
	}

	@Override
	public List getActions(ObjectCreationExpr n) {
		RemoveUnusedSymbolsAction unusedSymbols = new RemoveUnusedSymbolsAction(
				siblings.peek());
		actions = new LinkedList();
		actions.add(unusedSymbols);
		siblings.push(n.getAnonymousClassBody());
		return actions;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy