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);
}
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;
}
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);
}
}
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;
}
}
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;
}
}
Aggregations