本文整理汇总了Java中java.lang.reflect.Constructor.getParameterCount方法的典型用法代码示例。如果您正苦于以下问题:Java Constructor.getParameterCount方法的具体用法?Java Constructor.getParameterCount怎么用?Java Constructor.getParameterCount使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.reflect.Constructor
的用法示例。
在下文中一共展示了Constructor.getParameterCount方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: checkAccess
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
public static void checkAccess(AccessibleObject accessibleObject, Object obj)
throws IllegalAccessException,
InvocationTargetException,
InstantiationException
{
if (accessibleObject instanceof Field) {
Field field = (Field) accessibleObject;
field.set(obj, 42);
field.get(obj);
} else if (accessibleObject instanceof Method) {
Method method = (Method) accessibleObject;
method.invoke(obj);
} else if (accessibleObject instanceof Constructor) {
Constructor<?> constructor = (Constructor<?>) accessibleObject;
Object[] params = new Object[constructor.getParameterCount()];
constructor.newInstance(params);
}
}
示例2: getFieldProcessor
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
private static FieldProcessor getFieldProcessor(Element element) {
Object[] arguments = getFieldProcessorArguments(element);
String clazz = getAttribute(element, "class");
try {
if (arguments == null || arguments.length == 0) {
return (FieldProcessor) Class.forName(clazz).newInstance();
}
Constructor<?>[] constructors = Class.forName(clazz).getConstructors();
for (Constructor<?> constructor : constructors) {
if (arguments.length == constructor.getParameterCount()) {
return (FieldProcessor) constructor.newInstance(arguments);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
示例3: getFieldProcessor
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
private FieldProcessor getFieldProcessor(Element element) {
Object[] arguments = getFieldProcessorArguments(element);
String clazz = element.getAttribute("class");
try {
if (arguments == null || arguments.length == 0) {
return (FieldProcessor) Class.forName(clazz).newInstance();
}
Constructor<?>[] constructors = Class.forName(clazz).getConstructors();
for (Constructor<?> constructor : constructors) {
if (arguments.length == constructor.getParameterCount()) {
return (FieldProcessor) constructor.newInstance(arguments);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
示例4: mapConstructorParameters
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
private static <T> ThrowingSupplier<T> mapConstructorParameters(Constructor<T> constructor, Supplier<?>[] suppliers) {
switch (constructor.getParameterCount()) {
case 0: return constructor::newInstance;
case 1: return () -> constructor.newInstance(suppliers[0].get());
case 2: return () -> constructor.newInstance(suppliers[0].get(), suppliers[1].get());
case 3: return () -> constructor.newInstance(suppliers[0].get(), suppliers[1].get(), suppliers[2].get());
case 4: return () -> constructor.newInstance(suppliers[0].get(), suppliers[1].get(), suppliers[2].get(), suppliers[3].get());
case 5: return () -> constructor.newInstance(suppliers[0].get(), suppliers[1].get(), suppliers[2].get(), suppliers[3].get(), suppliers[4].get());
case 6: return () -> constructor.newInstance(suppliers[0].get(), suppliers[1].get(), suppliers[2].get(), suppliers[3].get(), suppliers[4].get(), suppliers[5].get());
case 7: return () -> constructor.newInstance(suppliers[0].get(), suppliers[1].get(), suppliers[2].get(), suppliers[3].get(), suppliers[4].get(), suppliers[5].get(), suppliers[6].get());
case 8: return () -> constructor.newInstance(suppliers[0].get(), suppliers[1].get(), suppliers[2].get(), suppliers[3].get(), suppliers[4].get(), suppliers[5].get(), suppliers[6].get(), suppliers[7].get());
case 9: return () -> constructor.newInstance(suppliers[0].get(), suppliers[1].get(), suppliers[2].get(), suppliers[3].get(), suppliers[4].get(), suppliers[5].get(), suppliers[6].get(), suppliers[7].get(), suppliers[8].get());
case 10: return () -> constructor.newInstance(suppliers[0].get(), suppliers[1].get(), suppliers[2].get(), suppliers[3].get(), suppliers[4].get(), suppliers[5].get(), suppliers[6].get(), suppliers[7].get(), suppliers[8].get(), suppliers[9].get());
default:
//Should not happen, limits are already checked
return null;
}
}
示例5: createNewDummy
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
/**
* Creates a new dummy entity of the required type. Required non null foreign keys will be taken from the {@link #getDummy(AbstractRedG, Class)} method
* and will be created if necessary as well. If the creation fails for some reason, a {@link DummyCreationException} will be thrown.
*
* @param redG The redG instance
* @param dummyClass The class specifying the wanted entity type
* @param <T> The wanted entity type
* @return A new dummy entity of the required type. It has already been added to the redG object and can be used immediately.
* @throws DummyCreationException If no fitting constructor is found or instantiation fails
*/
private <T extends RedGEntity> T createNewDummy(AbstractRedG redG, Class<T> dummyClass) {
Constructor constructor = Arrays.stream(dummyClass.getDeclaredConstructors())
.filter(this::isDummyRedGEntityConstructor)
.findFirst().orElseThrow(() -> new DummyCreationException("Could not find a fitting constructor"));
Object[] parameter = new Object[constructor.getParameterCount()];
parameter[0] = redG;
for (int i = 1; i < constructor.getParameterCount(); i++) {
parameter[i] = getDummy(redG, constructor.getParameterTypes()[i]);
}
try {
constructor.setAccessible(true);
T obj = dummyClass.cast(constructor.newInstance(parameter));
redG.addEntity(obj);
return obj;
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new DummyCreationException("Instantiation of the dummy failed", e);
}
}
示例6: getConstructor
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
@SuppressWarnings({"unchecked"})
public static <T> Constructor<T> getConstructor(Class<T> clazz, Class<?>... params) {
for (Constructor<?> constructors : clazz.getDeclaredConstructors()) {
if (constructors.getParameterCount() != params.length) {
continue;
}
if (Reflection.matchParams(constructors.getParameterTypes(), params)) {
return (Constructor<T>) constructors;
}
}
return clazz.getSuperclass() == null ? null : getConstructor((Class<T>) clazz.getSuperclass(), params);
}
示例7: getTierStoreConstructor
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
public static Constructor<?> getTierStoreConstructor(Class<?> clazz)
throws DefaultConstructorMissingException {
for (Constructor<?> c : clazz.getConstructors()) {
if (c.getParameterCount() == 1 && c.getParameterTypes()[0] == String.class) {
return c;
}
}
throw new DefaultConstructorMissingException("TierStore default Constructor missing.");
}
示例8: buildProcess
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
/**
* Builds a new process
*
* @return ImageProcess
*/
private Process buildProcess() {
try {
Constructor constructor = processType.getConstructors()[0];
TypeConverter converter = new TypeConverter();
List<Object> list = new ArrayList<>();
for (int i = 0; i < constructor.getParameterCount(); i++) {
Object input = getInput(processInput.get(i).getName());
list.add(converter.convert(input, constructor.getParameterTypes()[i]));
}
return (Process) constructor.newInstance(list.toArray());
} catch (IllegalArgumentException | ReflectiveOperationException ex) {
throw new RuntimeException("Process build fail", ex);
}
}
示例9: getNoArgsConstructor
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
private Constructor<?> getNoArgsConstructor(Constructor<?>[] constructors) {
for (Constructor<?> constructor : constructors) {
if (constructor.getParameterCount() == 0) {
return constructor;
}
}
return null;
}
示例10: buildWithConstructor
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
@SuppressWarnings({"unchecked"})
public static <T> T buildWithConstructor(Class<T> toBuild, Object... args) {
for (Constructor<?> constructor : toBuild.getDeclaredConstructors()) {
if (constructor.getParameterCount() == args.length) {
try {
return (T) constructor.newInstance(args);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new IllegalArgumentException("Couldn't construct a new object of type: " + toBuild + " using it's 0 args constructor!", e);
}
}
}
throw new IllegalArgumentException("No " + args.length + " args constructor found in class: " + toBuild);
}
示例11: hasNoArgsConstructor
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
public static boolean hasNoArgsConstructor(Class clazz) {
boolean noArgsConstructor = false;
for (Constructor<?> constructor : clazz.getConstructors()) {
if (constructor.getParameterCount() == 0) {
noArgsConstructor = true;
}
}
return noArgsConstructor;
}
示例12: isDummyRedGEntityConstructor
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
/**
* Checks whether the constructor matches the standard redG constructor (AbstractRedG as first param and zero or more classes extending RedGEntity).
*
* @param constructor The constructor to check
* @return {@code true} if the constructor matches the standard redG constructor, {@code false} otherwise
*/
private boolean isDummyRedGEntityConstructor(Constructor constructor) {
if (constructor.getParameterCount() < 1) {
return false;
}
if (!AbstractRedG.class.isAssignableFrom(constructor.getParameterTypes()[0])) {
return false;
}
for (int i = 1; i < constructor.getParameterCount(); i++) {
if (!RedGEntity.class.isAssignableFrom(constructor.getParameterTypes()[i])) {
return false;
}
}
return true;
}
示例13: hasParameterlessPublicConstructor
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
public static Constructor<?> hasParameterlessPublicConstructor(Class<?> clazz)
throws DefaultConstructorMissingException {
for (Constructor<?> constructor : clazz.getConstructors()) {
if (constructor.getParameterCount() == 0) {
return constructor;
}
}
throw new DefaultConstructorMissingException("Default constructor missing");
}
示例14: createEntity
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
public static Entity createEntity(String name, FullChunk chunk, CompoundTag nbt, Object... args) {
Entity entity = null;
if (knownEntities.containsKey(name)) {
Class<? extends Entity> clazz = knownEntities.get(name);
if (clazz == null) {
return null;
}
for (Constructor constructor : clazz.getConstructors()) {
if (entity != null) {
break;
}
if (constructor.getParameterCount() != (args == null ? 2 : args.length + 2)) {
continue;
}
try {
if (args == null || args.length == 0) {
entity = (Entity) constructor.newInstance(chunk, nbt);
} else {
Object[] objects = new Object[args.length + 2];
objects[0] = chunk;
objects[1] = nbt;
System.arraycopy(args, 0, objects, 2, args.length);
entity = (Entity) constructor.newInstance(objects);
}
} catch (Exception e) {
MainLogger.getLogger().logException(e);
}
}
}
return entity;
}
示例15: createBlockEntity
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
public static BlockEntity createBlockEntity(String type, FullChunk chunk, CompoundTag nbt, Object... args) {
type = type.replaceFirst("BlockEntity", ""); //TODO: Remove this after the first release
BlockEntity blockEntity = null;
if (knownBlockEntities.containsKey(type)) {
Class<? extends BlockEntity> clazz = knownBlockEntities.get(type);
if (clazz == null) {
return null;
}
for (Constructor constructor : clazz.getConstructors()) {
if (blockEntity != null) {
break;
}
if (constructor.getParameterCount() != (args == null ? 2 : args.length + 2)) {
continue;
}
try {
if (args == null || args.length == 0) {
blockEntity = (BlockEntity) constructor.newInstance(chunk, nbt);
} else {
Object[] objects = new Object[args.length + 2];
objects[0] = chunk;
objects[1] = nbt;
System.arraycopy(args, 0, objects, 2, args.length);
blockEntity = (BlockEntity) constructor.newInstance(objects);
}
} catch (Exception e) {
//ignore
}
}
}
return blockEntity;
}