spoon.pattern.internal.ValueConvertorImpl 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.pattern.internal;
import java.util.List;
import spoon.SpoonException;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.code.CtLiteral;
import spoon.reflect.code.CtNewArray;
import spoon.reflect.code.CtStatement;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtExecutable;
import spoon.reflect.declaration.CtNamedElement;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.reference.CtReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.support.visitor.SignaturePrinter;
/**
* Converts the individual parameter values to required type
*/
public class ValueConvertorImpl implements ValueConvertor {
public ValueConvertorImpl() {
}
@Override
public T getValueAs(Factory factory, String parameterName, Object value, Class valueClass) {
if (valueClass.isInstance(value)) {
return cloneIfNeeded(valueClass.cast(value));
}
if (CtExpression.class.isAssignableFrom(valueClass)) {
if (value instanceof Class) {
return (T) factory.Code().createClassAccess(factory.Type().createReference((Class) value));
}
if (value instanceof CtTypeReference) {
//convert type reference into code element as class access
CtTypeReference> tr = (CtTypeReference>) value;
return (T) factory.Code().createClassAccess(tr);
}
if (value == null || value instanceof String || value instanceof Number || value instanceof Boolean || value instanceof Character) {
//convert String to code element as Literal
return (T) factory.Code().createLiteral(value);
}
if (value.getClass().isArray()) {
Class> itemClass = value.getClass().getComponentType();
if (CtExpression.class.isAssignableFrom(itemClass)) {
CtNewArray