spoon.reflect.meta.impl.AbstractRoleHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spoon-core Show documentation
Show all versions of spoon-core Show documentation
Spoon is a tool for meta-programming, analysis and transformation of Java programs.
/*
* 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.List;
import java.util.Map;
import java.util.Set;
import spoon.SpoonException;
import spoon.reflect.meta.RoleHandler;
import spoon.reflect.path.CtRole;
/**
* common implementation of {@link RoleHandler}
* @param the type of node whose attribute has to be manipulated
* @param the type of container value of the attribute
* @param the type of item value of the attribute
*/
abstract class AbstractRoleHandler implements RoleHandler {
private final CtRole role;
private final Class targetClass;
private final Class valueClass;
@SuppressWarnings({ "unchecked", "rawtypes" })
protected AbstractRoleHandler(CtRole role, Class targetType, Class> valueType) {
this.role = role;
this.targetClass = targetType;
this.valueClass = (Class) valueType;
}
@Override
public CtRole getRole() {
return role;
}
@Override
public Class> getTargetType() {
return targetClass;
}
@SuppressWarnings("unchecked")
protected T castTarget(Object element) {
return (T) element;
}
@SuppressWarnings("unchecked")
protected U castValue(Object value) {
return (U) value;
}
protected void checkItemsClass(Iterable> iterable) {
//check that each item has expected class
for (Object value : iterable) {
castItemValue(value);
}
}
@SuppressWarnings("unchecked")
protected V castItemValue(Object value) {
//check that item has expected class
if (value != null && !valueClass.isInstance(value)) {
throw new ClassCastException(value.getClass().getName() + " cannot be cast to " + valueClass.getName());
}
return (V) value;
}
@Override
public void setValue(W element, X value) {
throw new SpoonException("Setting of CtRole." + role.name() + " is not supported for " + element.getClass().getSimpleName());
}
@Override
public Class> getValueClass() {
return valueClass;
}
@Override
public List asList(W element) {
throw new SpoonException("The value of CtRole." + getRole().name() + " cannot be adapted to List for " + element.getClass().getSimpleName());
}
@Override
public Set asSet(W element) {
throw new SpoonException("The value of CtRole." + getRole().name() + " cannot be adapted to Set for " + element.getClass().getSimpleName());
}
@Override
public Map asMap(W element) {
throw new SpoonException("The value of CtRole." + getRole().name() + " cannot be adapted to Map for " + element.getClass().getSimpleName());
}
@Override
public String toString() {
return getTargetType().getName() + "#" + getRole().getCamelCaseName();
}
}