Search in sources :

Example 71 with Constructor

use of java.lang.reflect.Constructor in project intellij-community by JetBrains.

the class NotNullVerifyingInstrumenterTest method testConstructorParamWithMessage.

public void testConstructorParamWithMessage() throws Exception {
    Class<?> testClass = prepareTest();
    Constructor method = testClass.getConstructor(Object.class);
    verifyCallThrowsException("ConstructorParam.ConstructorParam.o cant be null", null, method, (Object) null);
}
Also used : Constructor(java.lang.reflect.Constructor)

Example 72 with Constructor

use of java.lang.reflect.Constructor in project intellij-community by JetBrains.

the class ChainingFilterTransformer method doTransform.

private Reader doTransform(ResourceRootFilter filter, Reader original) {
    if ("RenamingCopyFilter".equals(filter.filterType)) {
        final Matcher matcher = (Matcher) filter.getProperties().get("matcher");
        final String replacement = (String) filter.getProperties().get("replacement");
        if (matcher == null || replacement == null)
            return original;
        matcher.reset(myOutputFileRef.get().getName());
        if (matcher.find()) {
            final String newFileName = matcher.replaceFirst(replacement);
            myOutputFileRef.set(new File(myOutputFileRef.get().getParentFile(), newFileName));
        }
        return original;
    }
    try {
        Class<?> clazz = Class.forName(filter.filterType);
        if (!FilterReader.class.isAssignableFrom(clazz)) {
            myContext.processMessage(new CompilerMessage(GradleResourcesBuilder.BUILDER_NAME, BuildMessage.Kind.WARNING, String.format("Error - Invalid filter specification for %s. It should extend java.io.FilterReader.", filter.filterType), null));
        }
        Constructor constructor = clazz.getConstructor(Reader.class);
        FilterReader result = (FilterReader) constructor.newInstance(original);
        final Map<Object, Object> properties = filter.getProperties();
        if (!properties.isEmpty()) {
            if (ExpandProperties.class.getName().equals(filter.filterType)) {
                final Map<Object, Object> antProps = new HashMap<>(properties);
                final Project project = new Project();
                for (Map.Entry<Object, Object> entry : antProps.entrySet()) {
                    project.setProperty(entry.getKey().toString(), entry.getValue().toString());
                }
                properties.clear();
                properties.put("project", project);
            }
            ConfigureUtil.configureByMap(properties, result);
        }
        return result;
    } catch (Throwable th) {
        myContext.processMessage(new CompilerMessage(GradleResourcesBuilder.BUILDER_NAME, BuildMessage.Kind.WARNING, String.format("Error - Failed to apply filter(%s): %s", filter.filterType, th.getMessage()), null));
    }
    return original;
}
Also used : CompilerMessage(org.jetbrains.jps.incremental.messages.CompilerMessage) Matcher(java.util.regex.Matcher) Constructor(java.lang.reflect.Constructor) ExpandProperties(org.apache.tools.ant.filters.ExpandProperties) Project(org.apache.tools.ant.Project) File(java.io.File) FilterReader(java.io.FilterReader)

Example 73 with Constructor

use of java.lang.reflect.Constructor in project lucene-solr by apache.

the class SolrCore method createInstance.

/**
   * Creates an instance by trying a constructor that accepts a SolrCore before
   * trying the default (no arg) constructor.
   *
   * @param className the instance class to create
   * @param cast      the class or interface that the instance should extend or implement
   * @param msg       a message helping compose the exception error if any occurs.
   * @param core      The SolrCore instance for which this object needs to be loaded
   * @return the desired instance
   * @throws SolrException if the object could not be instantiated
   */
public static <T> T createInstance(String className, Class<T> cast, String msg, SolrCore core, ResourceLoader resourceLoader) {
    Class<? extends T> clazz = null;
    if (msg == null)
        msg = "SolrCore Object";
    try {
        clazz = resourceLoader.findClass(className, cast);
        //most of the classes do not have constructors which takes SolrCore argument. It is recommended to obtain SolrCore by implementing SolrCoreAware.
        // So invariably always it will cause a  NoSuchMethodException. So iterate though the list of available constructors
        Constructor<?>[] cons = clazz.getConstructors();
        for (Constructor<?> con : cons) {
            Class<?>[] types = con.getParameterTypes();
            if (types.length == 1 && types[0] == SolrCore.class) {
                return cast.cast(con.newInstance(core));
            }
        }
        //use the empty constructor
        return resourceLoader.newInstance(className, cast);
    } catch (SolrException e) {
        throw e;
    } catch (Exception e) {
        // "InvocationTargetException" that have no useful getMessage
        if (null != e.getCause() && e.getCause() instanceof SolrException) {
            SolrException inner = (SolrException) e.getCause();
            throw inner;
        }
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error Instantiating " + msg + ", " + className + " failed to instantiate " + cast.getName(), e);
    }
}
Also used : Constructor(java.lang.reflect.Constructor) SolrException(org.apache.solr.common.SolrException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) IOException(java.io.IOException) NoSuchFileException(java.nio.file.NoSuchFileException) SolrException(org.apache.solr.common.SolrException) FileNotFoundException(java.io.FileNotFoundException) KeeperException(org.apache.zookeeper.KeeperException)

Example 74 with Constructor

use of java.lang.reflect.Constructor in project SpaciousLib by anhcraft.

the class ItemsNBT method setByteArray.

public ItemStack setByteArray(String name, byte[] value) {
    try {
        Class<?> e = Class.forName("org.anhcraft.spaciouslib.Inventory.ItemNBT.NBTCompound_" + GameVersion.getVersion().toString().replace("v", ""));
        Constructor c = e.getConstructor();
        NBTCompoundWarpper i = (NBTCompoundWarpper) c.newInstance();
        i.importFromItem(item);
        i.set(name, value);
        return i.exportToItem(item);
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException x) {
        x.printStackTrace();
        return null;
    }
}
Also used : Constructor(java.lang.reflect.Constructor) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 75 with Constructor

use of java.lang.reflect.Constructor in project SpaciousLib by anhcraft.

the class ItemsNBT method setList.

public ItemStack setList(String name, List<NBTCompoundWarpper> value) {
    try {
        Class<?> e = Class.forName("org.anhcraft.spaciouslib.Inventory.ItemNBT.NBTCompound_" + GameVersion.getVersion().toString().replace("v", ""));
        Constructor c = e.getConstructor();
        NBTCompoundWarpper i = (NBTCompoundWarpper) c.newInstance();
        i.importFromItem(item);
        i.set(name, value);
        return i.exportToItem(item);
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException x) {
        x.printStackTrace();
        return null;
    }
}
Also used : Constructor(java.lang.reflect.Constructor) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

Constructor (java.lang.reflect.Constructor)1314 InvocationTargetException (java.lang.reflect.InvocationTargetException)283 Method (java.lang.reflect.Method)253 IOException (java.io.IOException)128 Field (java.lang.reflect.Field)112 ArrayList (java.util.ArrayList)106 Test (org.junit.Test)92 DOMTestDocumentBuilderFactory (org.w3c.domts.DOMTestDocumentBuilderFactory)74 JUnitTestSuiteAdapter (org.w3c.domts.JUnitTestSuiteAdapter)73 List (java.util.List)61 JAXPDOMTestDocumentBuilderFactory (org.w3c.domts.JAXPDOMTestDocumentBuilderFactory)58 Map (java.util.Map)50 Type (java.lang.reflect.Type)39 Annotation (java.lang.annotation.Annotation)38 HashMap (java.util.HashMap)38 HashSet (java.util.HashSet)31 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)31 ParameterizedType (java.lang.reflect.ParameterizedType)30 File (java.io.File)20 URL (java.net.URL)20