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

org.walkmod.javalang.compiler.reflection.GenericsBuilderFromArgs 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.reflection;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.walkmod.javalang.ast.expr.ClassExpr;
import org.walkmod.javalang.ast.expr.Expression;
import org.walkmod.javalang.compiler.Builder;
import org.walkmod.javalang.compiler.symbols.SymbolType;
import org.walkmod.javalang.compiler.types.TypesLoaderVisitor;
import org.walkmod.javalang.exceptions.NoSuchExpressionTypeException;

/**
 * For a given set of expressions, which some of them could reference an
 * specific class (e.g A.class), when the parameter is generic (e.g. T), then
 * the map that T corresponds to A.class
 * 
 * @author rpau
 *
 */
public class GenericsBuilderFromArgs implements Builder> {

    private Method method;

    private List argumentValues;

    public GenericsBuilderFromArgs() {}

    public GenericsBuilderFromArgs(Method method, List argumentValues) {
        this.method = method;
        this.argumentValues = argumentValues;

    }

    public void setMethod(Method method) {
        this.method = method;
    }

    public void setArgumentValues(List argumentValues) {
        this.argumentValues = argumentValues;
    }

    @Override
    public Map build(Map obj) {
        if (obj == null) {
            obj = new HashMap();
        }
        TypeVariable[] typeVariables = method.getTypeParameters();

        if (typeVariables != null) {

            for (int i = 0; i < typeVariables.length; i++) {

                Type[] parameterTypes = method.getGenericParameterTypes();

                if (parameterTypes != null && argumentValues != null) {

                    for (int j = 0; j < parameterTypes.length && j < argumentValues.size(); j++) {

                        if (parameterTypes[j] instanceof ParameterizedType) {

                            String variableName =
                                    ((ParameterizedType) parameterTypes[j]).getActualTypeArguments()[0].toString();

                            if (variableName.length() == 1) {
                                if (argumentValues.get(j) instanceof ClassExpr) {
                                    Class paramClass;
                                    try {
                                        paramClass = TypesLoaderVisitor.getClassLoader()
                                                .loadClass(((ClassExpr) argumentValues.get(j)).getType());
                                    } catch (ClassNotFoundException e) {
                                        throw new NoSuchExpressionTypeException(
                                                "Invalid class into the generics resolution", e);
                                    }

                                    SymbolType auxType = new SymbolType(paramClass.getName());
                                    obj.put(variableName, auxType);
                                }
                            }
                        }
                    }
                }
            }
        }

        return obj;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy