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

com.fujitsu.vdmj.tc.annotations.TCAnnotation Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 *
 *	Copyright (c) 2018 Nick Battle.
 *
 *	Author: Nick Battle
 *
 *	This file is part of VDMJ.
 *
 *	VDMJ is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	VDMJ 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 General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with VDMJ.  If not, see .
 *	SPDX-License-Identifier: GPL-3.0-or-later
 *
 ******************************************************************************/

package com.fujitsu.vdmj.tc.annotations;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import com.fujitsu.vdmj.mapper.MappingOptional;
import com.fujitsu.vdmj.tc.TCNode;
import com.fujitsu.vdmj.tc.definitions.TCClassDefinition;
import com.fujitsu.vdmj.tc.definitions.TCDefinition;
import com.fujitsu.vdmj.tc.expressions.TCExpression;
import com.fujitsu.vdmj.tc.expressions.TCExpressionList;
import com.fujitsu.vdmj.tc.lex.TCIdentifierToken;
import com.fujitsu.vdmj.tc.modules.TCModule;
import com.fujitsu.vdmj.tc.statements.TCStatement;
import com.fujitsu.vdmj.tc.types.TCType;
import com.fujitsu.vdmj.typechecker.Environment;
import com.fujitsu.vdmj.typechecker.ModuleEnvironment;
import com.fujitsu.vdmj.typechecker.NameScope;
import com.fujitsu.vdmj.typechecker.PrivateClassEnvironment;

public abstract class TCAnnotation extends TCNode implements MappingOptional
{
	private static final long serialVersionUID = 1L;

	public final TCIdentifierToken name;
	public final TCExpressionList args;
	
	private static final Set> declared = new HashSet>(); 
	private static final List instances = new Vector(); 

	public TCAnnotation(TCIdentifierToken name, TCExpressionList args)
	{
		this.name = name;
		this.args = args;
		
		declared.add(this.getClass());
		instances.add(this);
	}
	
	public static void reset()
	{
		declared.clear();
		instances.clear();
	}

	public static void init(Environment globals)
	{
		for (Class clazz: declared)
		{
			try
			{
				Method doInit = clazz.getMethod("doInit", (Class[])null);
				doInit.invoke(null, (Object[])null);
			}
			catch (InvocationTargetException e)
			{
				throw new RuntimeException(clazz.getSimpleName() + ":" + e.getCause());
			}
			catch (Throwable e)
			{
				throw new RuntimeException(clazz.getSimpleName() + ":" + e);
			}
		}
		
		for (TCAnnotation annotation: instances)
		{
			annotation.doInit(globals);
		}
	}
	
	public static void doInit()
	{
		// Nothing by default
	}

	protected void doInit(Environment globals)
	{
		// Nothing by default
	}

	public static List getInstances(Class type)
	{
		List found = new Vector();
		
		for (TCAnnotation instance: instances)
		{
			if (type.isAssignableFrom(instance.getClass()))
			{
				found.add(instance);
			}
		}
		
		return found;
	}

	@Override
	public String toString()
	{
		return "@" + name + (args.isEmpty() ? "" : "(" + args + ")");
	}

	public void tcBefore(TCDefinition def, Environment env, NameScope scope)
	{
		// Do nothing
	}
	
	public void tcBefore(TCStatement stmt, Environment env, NameScope scope)
	{
		// Do nothing
	}
	
	public void tcBefore(TCExpression exp, Environment env, NameScope scope)
	{
		// Do nothing
	}

	public void tcBefore(TCModule m)
	{
		// Do nothing
	}

	public void tcBefore(TCModule m, ModuleEnvironment e)
	{
		// Do nothing
	}

	public void tcBefore(TCClassDefinition clazz)
	{
		// Do nothing
	}

	public void tcBefore(TCClassDefinition clazz, PrivateClassEnvironment self)
	{
		// Do nothing
	}

	public void tcAfter(TCDefinition def, TCType type, Environment env, NameScope scope)
	{
		// Do nothing
	}
	
	public void tcAfter(TCStatement stmt, TCType type, Environment env, NameScope scope)
	{
		// Do nothing
	}
	
	public void tcAfter(TCExpression exp, TCType type, Environment env, NameScope scope)
	{
		// Do nothing
	}

	public void tcAfter(TCModule m)
	{
		// Do nothing
	}

	public void tcAfter(TCModule m, ModuleEnvironment env)
	{
		// Do nothing
	}

	public void tcAfter(TCClassDefinition m)
	{
		// Do nothing
	}
	
	public void tcAfter(TCClassDefinition clazz, PrivateClassEnvironment self)
	{
		// Do nothing
	}

	public static void close()
	{
		for (TCAnnotation annotation: instances)
		{
			annotation.doClose();
		}
	}
	
	public void doClose()
	{
		// Nothing by default
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy