Example usage for java.lang Class getConstructors

List of usage examples for java.lang Class getConstructors

Introduction

In this page you can find the example usage for java.lang Class getConstructors.

Prototype

@CallerSensitive
public Constructor<?>[] getConstructors() throws SecurityException 

Source Link

Document

Returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object.

Usage

From source file:br.upe.ecomp.dosa.view.wizard.WizardAction.java

private void addProblems() {
    problemComboBox.setModel(new DefaultComboBoxModel());
    problemComboBox.addItem("");
    for (Class<? extends Problem> problem : applicationContext.getProblemList()) {
        try {//from w w  w  . j a  v  a  2 s  . co  m
            problemComboBox.addItem(problem.getConstructors()[0].newInstance());
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:br.upe.ecomp.dosa.view.wizard.WizardAction.java

private void addAlgorithms() {
    algorithmComboBox.setModel(new DefaultComboBoxModel());
    algorithmComboBox.addItem("");
    for (Class<? extends Algorithm> algorithm : applicationContext.getAlgorithmList()) {
        try {//from   www .j  a v a2s.c om
            algorithmComboBox.addItem(algorithm.getConstructors()[0].newInstance());
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils.java

/**
 * @return the {@link Constructor} which can be invoked with given arguments, may be
 *         <code>null</code>.
 *//*from   w  ww  .ja v  a2s.  c o  m*/
@SuppressWarnings("unchecked")
public static <T> Constructor<T> getConstructorForArguments(Class<T> clazz, Object... arguments) {
    Assert.isNotNull(clazz);
    Assert.isNotNull(arguments);
    for (Constructor<?> constructor : clazz.getConstructors()) {
        Class<?>[] parameterTypes = constructor.getParameterTypes();
        // check compatibility of each parameter and argument
        boolean canUse = true;
        if (parameterTypes.length == arguments.length) {
            for (int i = 0; i < parameterTypes.length; i++) {
                Class<?> parameterType = parameterTypes[i];
                Object argument = arguments[i];
                if (!isAssignableFrom(parameterType, argument)) {
                    canUse = false;
                    break;
                }
            }
            // use if possible
            if (canUse) {
                return (Constructor<T>) constructor;
            }
        }
    }
    // no compatible constructor
    return null;
}

From source file:com.mewmew.fairy.v1.cli.AnnotatedCLI.java

public AnnotatedCLI(Class... clazz) {
    final List<AnnotatedOption> list = new ArrayList<AnnotatedOption>();
    for (Class aClass : clazz) {
        for (Field field : aClass.getDeclaredFields()) {
            Param param = field.getAnnotation(Param.class);
            if (param != null) {
                list.add(new AnnotatedOption(aClass, field, param));
            }//from w  w w. j  av a 2 s.c om
            Args arg = field.getAnnotation(Args.class);
            if (arg != null) {
                args.put(aClass, field);
            }
        }
        for (Constructor ctor : aClass.getConstructors()) {
            AnnotatedConstructor actor = new AnnotatedConstructor(aClass, ctor);
            Class[] types = ctor.getParameterTypes();
            Annotation[][] annotations = ctor.getParameterAnnotations();
            for (int i = 0; i < types.length; i++) {
                Class type = types[i];
                Annotation[] ann = annotations[i];
                for (Annotation annotation : ann) {
                    if (annotation instanceof Param) {
                        actor.addParam((Param) annotation, type);
                    }
                }
            }
            if (actor.isValid()) {
                ctors.put(aClass, actor);
            }
        }
    }
    options = new Options();
    mappings = Multimaps.newMultimap(Maps.<Class, Collection<AnnotatedOption>>newHashMap(),
            new Supplier<Collection<AnnotatedOption>>() {
                public Collection<AnnotatedOption> get() {
                    return new ArrayList<AnnotatedOption>();
                }
            });
    for (AnnotatedConstructor constructor : ctors.values()) {
        for (AnnotatedConstructor.AnnotatedParam param : constructor.getParams()) {
            boolean hasArgs = !(param.getType().equals(boolean.class) || param.getType().equals(Boolean.class));
            String option = param.getParam().option();
            while (options.hasOption(option)) {
                option = option + option;
            }
            options.addOption(option, param.getParam().name(), hasArgs, param.getParam().desc());
        }
    }
    for (AnnotatedOption opt : list) {
        boolean hasArgs = !(opt.field.getType().equals(boolean.class)
                || opt.field.getType().equals(Boolean.class));
        while (options.hasOption(opt.getOpt())) {
            opt.setOpt(opt.getOpt() + opt.getOpt());
        }
        options.addOption(opt.getOpt(), opt.getName(), hasArgs, opt.getParam().desc());
        mappings.put(opt.clazz, opt);
    }
}

From source file:br.upe.ecomp.dosa.view.wizard.WizardAction.java

private void addMeasurements() {
    measurementAvailableList.setModel(new DefaultListModel());
    for (Class<? extends Measurement> measurement : applicationContext.getMeasurementList()) {
        try {/*www  . j a  v  a  2s  .c  o m*/
            ((DefaultListModel) measurementAvailableList.getModel())
                    .addElement(measurement.getConstructors()[0].newInstance());
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:org.drools.compiler.PackageBuilder.java

private void mergePackage(final Package pkg, final PackageDescr packageDescr) {
    final List imports = packageDescr.getImports();
    for (final Iterator it = imports.iterator(); it.hasNext();) {
        pkg.addImport((String) it.next());
    }/*from ww  w  .  j  av  a2s.c  o m*/
    TypeResolver tResolver = null;
    if (DroolsEnv.isJava()) {
        tResolver = new ClassTypeResolver(imports, pkg.getPackageCompilationData().getClassLoader());

    } else {
        Class cl;
        try {
            cl = Class.forName("cli.org.drools.dotnet.semantics.DotnetClassTypeResolver");
            tResolver = (ClassTypeResolver) cl.getConstructors()[0]
                    .newInstance(new Object[] { imports, pkg.getPackageCompilationData().getClassLoader() });

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    final TypeResolver typeResolver = tResolver;

    final Map globals = packageDescr.getGlobals();
    for (final Iterator it = globals.keySet().iterator(); it.hasNext();) {
        final String identifier = (String) it.next();
        final String className = (String) globals.get(identifier);

        Class clazz;
        try {
            clazz = typeResolver.resolveType(className);
            pkg.addGlobal(identifier, clazz);
        } catch (final ClassNotFoundException e) {
            new GlobalError(identifier);
        }
    }
}

From source file:org.drools.compiler.PackageBuilder.java

/**
 * @return a Type resolver, lazily. //from   w ww.j  a v a  2  s . c  o  m
 * If one does not exist yet, it will be initialised.
 */
private TypeResolver getTypeResolver() {

    if (this.typeResolver == null) {
        if (DroolsEnv.isJava()) {
            typeResolver = new ClassTypeResolver(pkg.getImports(),
                    pkg.getPackageCompilationData().getClassLoader());
            // make an automatic import for the current package
            typeResolver.addImport(pkg.getName() + ".*");
            typeResolver.addImport("java.lang.*");
        } else {
            Class cl;
            try {
                cl = Class.forName("cli.org.drools.dotnet.semantics.DotnetClassTypeResolver");
                typeResolver = (ClassTypeResolver) cl.getConstructors()[0].newInstance(
                        new Object[] { pkg.getImports(), pkg.getPackageCompilationData().getClassLoader() });
                typeResolver.addImport(pkg.getName());
                typeResolver.addImport("System");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
    return this.typeResolver;
}

From source file:br.upe.ecomp.dosa.view.wizard.WizardAction.java

private void addStopConditions() {
    stopConditionAvailableList.setModel(new DefaultListModel());
    for (Class<? extends StopCondition> stopCondition : applicationContext.getStopConditionList()) {
        try {//w w  w .  j  a v  a2  s.  c o m
            ((DefaultListModel) stopConditionAvailableList.getModel())
                    .addElement(stopCondition.getConstructors()[0].newInstance());
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:org.obsidian.test.TestAbstract.java

public TestAbstract(Class classToTest) {
    //class object to test
    TestAbstract.classTested = classToTest;

    //class tested simple name
    classTestedSimpleName = classToTest.getSimpleName();

    //name of test class
    testClassName = classTestedSimpleName + "_O_Test";

    //populate method lists
    Method[] methods = classToTest.getDeclaredMethods();
    constructors = classToTest.getConstructors();
    getters = MethodSorter.getGetters(methods);
    setters = MethodSorter.getSetters(methods);
    remainingMethods = MethodSorter.getRemainingMethods(methods);

    //init body/*from   w  ww . j a  va  2s .  co  m*/
    body = new StringBuilder();
}