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

spoon.reflect.meta.impl.MapHandler Maven / Gradle / Ivy

Go to download

Spoon is a tool for meta-programming, analysis and transformation of Java programs.

There is a newer version: 11.1.1-beta-14
Show newest version
/*
 * SPDX-License-Identifier: (MIT OR CECILL-C)
 *
 * Copyright (C) 2006-2023 INRIA and contributors
 *
 * Spoon is available either under the terms of the MIT License (see LICENSE-MIT.txt) or the Cecill-C License (see LICENSE-CECILL-C.txt). You as the user are entitled to choose the terms under which to adopt Spoon.
 */
package spoon.reflect.meta.impl;

import java.util.AbstractMap;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import spoon.reflect.meta.ContainerKind;
import spoon.reflect.meta.RoleHandler;
import spoon.reflect.path.CtRole;

/**
 * implementation of {@link RoleHandler}, which handles attributes of type Map<String, V>
 * @param  the type of node whose attribute has to be manipulated
 * @param  the type of item value of the attribute
 */
abstract class MapHandler extends AbstractRoleHandler, V> {

	protected MapHandler(CtRole role, Class targetType, Class valueClass) {
		super(role, targetType, valueClass);
	}

	@Override
	public ContainerKind getContainerKind() {
		return ContainerKind.MAP;
	}

	@Override
	protected Map castValue(Object value) {
		Map map = super.castValue(value);
		//check that each item has expected class
		checkItemsClass(map.values());
		return map;
	}

	@Override
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public  java.util.Collection asCollection(W element) {
		return (Collection) asMap(element).values();
	}

	@Override
	public  Map asMap(W e) {
		// TODO Auto-generated method stub
		return new AbstractMap() {
			T element = castTarget(e);

			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public Set> entrySet() {
				return (Set) MapHandler.this.entrySet(element);
			}

			@SuppressWarnings("unchecked")
			@Override
			public X get(Object key) {
				return (X) MapHandler.this.get(element, key);
			}

			@SuppressWarnings("unchecked")
			@Override
			public X put(String key, X value) {
				return (X) MapHandler.this.put(element, key, castItemValue(value));
			}
		};
	}

	protected V get(T element, Object key) {
		return this.>getValue(element).get(key);
	}

	protected V put(T element, String key, V value) {
		Map values = new LinkedHashMap<>(this.>getValue(element));
		V ret = values.put(key, value);
		setValue(element, values);
		return ret;
	}

	protected Set> entrySet(T element) {
		return this.>getValue(element).entrySet();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy