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

net.sandius.rembulan.lib.luajava.MappedConstructor Maven / Gradle / Ivy

/*
 * Copyright 2016 Miroslav Janíček
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.sandius.rembulan.lib.luajava;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

class MappedConstructor {

	private final Constructor constructor;
	private final ParameterMapping[] parameterMappings;

	MappedConstructor(Constructor constructor, ParameterMapping[] parameterMappings) {
		this.constructor = Objects.requireNonNull(constructor);
		this.parameterMappings = Objects.requireNonNull(parameterMappings);
	}

	static  MappedConstructor of(Constructor constructor) {
		ParameterMapping[] mappings = ParameterMapping.mappingsFor(constructor.getParameterTypes());
		return new MappedConstructor<>(constructor, mappings);
	}

	public Constructor constructor() {
		return constructor;
	}

	public List parameterMappings() {
		return Collections.unmodifiableList(Arrays.asList(parameterMappings));
	}

	public T newInstance(Object[] args)
			throws IllegalAccessException, InvocationTargetException, InstantiationException {

		Object[] actualArgs = ApplyMappingVisitor.applyAll(parameterMappings, args);
		return constructor.newInstance(actualArgs);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy