use of java.lang.reflect.Constructor in project android_frameworks_base by DirtyUnicorns.
the class FilterFactory method createFilterByClass.
public Filter createFilterByClass(Class filterClass, String filterName) {
// Make sure this is a Filter subclass
try {
filterClass.asSubclass(Filter.class);
} catch (ClassCastException e) {
throw new IllegalArgumentException("Attempting to allocate class '" + filterClass + "' which is not a subclass of Filter!");
}
// Look for the correct constructor
Constructor filterConstructor = null;
try {
filterConstructor = filterClass.getConstructor(String.class);
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException("The filter class '" + filterClass + "' does not have a constructor of the form <init>(String name)!");
}
// Construct the filter
Filter filter = null;
try {
filter = (Filter) filterConstructor.newInstance(filterName);
} catch (Throwable t) {
// Condition checked below
}
if (filter == null) {
throw new IllegalArgumentException("Could not construct the filter '" + filterName + "'!");
}
return filter;
}
use of java.lang.reflect.Constructor in project dubbo by alibaba.
the class Builder method newObjectBuilder.
private static Builder<?> newObjectBuilder(final Class<?> c) {
if (c.isEnum())
return newEnumBuilder(c);
if (c.isAnonymousClass())
throw new RuntimeException("Can not instantiation anonymous class: " + c);
if (c.getEnclosingClass() != null && !Modifier.isStatic(c.getModifiers()))
throw new RuntimeException("Can not instantiation inner and non-static class: " + c);
if (Throwable.class.isAssignableFrom(c))
return SerializableBuilder;
ClassLoader cl = ClassHelper.getCallerClassLoader(Builder.class);
// is same package.
boolean isp;
String cn = c.getName(), bcn;
if (// is system class. if( cn.startsWith("java.") || cn.startsWith("javax.") || cn.startsWith("sun.") )
c.getClassLoader() == null) {
isp = false;
bcn = BUILDER_CLASS_NAME + "$bc" + BUILDER_CLASS_COUNTER.getAndIncrement();
} else {
isp = true;
bcn = cn + "$bc" + BUILDER_CLASS_COUNTER.getAndIncrement();
}
// is Collection, is Map, is Serializable.
boolean isc = Collection.class.isAssignableFrom(c);
boolean ism = !isc && Map.class.isAssignableFrom(c);
boolean iss = !(isc || ism) && Serializable.class.isAssignableFrom(c);
// deal with fields.
// fix-order fields names
String[] fns = null;
// load field-config file.
InputStream is = c.getResourceAsStream(c.getSimpleName() + FIELD_CONFIG_SUFFIX);
if (is != null) {
try {
int len = is.available();
if (len > 0) {
if (len > MAX_FIELD_CONFIG_FILE_SIZE)
throw new RuntimeException("Load [" + c.getName() + "] field-config file error: File-size too larger");
String[] lines = IOUtils.readLines(is);
if (lines != null && lines.length > 0) {
List<String> list = new ArrayList<String>();
for (int i = 0; i < lines.length; i++) {
fns = lines[i].split(",");
Arrays.sort(fns, FNC);
for (int j = 0; j < fns.length; j++) list.add(fns[j]);
}
fns = list.toArray(new String[0]);
}
}
} catch (IOException e) {
throw new RuntimeException("Load [" + c.getName() + "] field-config file error: " + e.getMessage());
} finally {
try {
is.close();
} catch (IOException e) {
}
}
}
Field f, fs[];
if (fns != null) {
fs = new Field[fns.length];
for (int i = 0; i < fns.length; i++) {
String fn = fns[i];
try {
f = c.getDeclaredField(fn);
int mod = f.getModifiers();
if (Modifier.isStatic(mod) || (serializeIgnoreFinalModifier(c) && Modifier.isFinal(mod)))
throw new RuntimeException("Field [" + c.getName() + "." + fn + "] is static/final field.");
if (Modifier.isTransient(mod)) {
if (iss)
return SerializableBuilder;
throw new RuntimeException("Field [" + c.getName() + "." + fn + "] is transient field.");
}
f.setAccessible(true);
fs[i] = f;
} catch (SecurityException e) {
throw new RuntimeException(e.getMessage());
} catch (NoSuchFieldException e) {
throw new RuntimeException("Field [" + c.getName() + "." + fn + "] not found.");
}
}
} else {
Class<?> t = c;
List<Field> fl = new ArrayList<Field>();
do {
fs = t.getDeclaredFields();
for (Field tf : fs) {
int mod = tf.getModifiers();
if (Modifier.isStatic(mod) || (serializeIgnoreFinalModifier(c) && Modifier.isFinal(mod)) || // skip static or inner-class's 'this$0' field.
tf.getName().equals("this$0") || //skip private inner-class field
!Modifier.isPublic(tf.getType().getModifiers()))
continue;
if (Modifier.isTransient(mod)) {
if (iss)
return SerializableBuilder;
continue;
}
tf.setAccessible(true);
fl.add(tf);
}
t = t.getSuperclass();
} while (t != Object.class);
fs = fl.toArray(new Field[0]);
if (fs.length > 1)
Arrays.sort(fs, FC);
}
// deal with constructors.
Constructor<?>[] cs = c.getDeclaredConstructors();
if (cs.length == 0) {
Class<?> t = c;
do {
t = t.getSuperclass();
if (t == null)
throw new RuntimeException("Can not found Constructor?");
cs = c.getDeclaredConstructors();
} while (cs.length == 0);
}
if (cs.length > 1)
Arrays.sort(cs, CC);
// writeObject code.
StringBuilder cwf = new StringBuilder("protected void writeObject(Object obj, ").append(GenericObjectOutput.class.getName()).append(" out) throws java.io.IOException{");
cwf.append(cn).append(" v = (").append(cn).append(")$1; ");
cwf.append("$2.writeInt(fields.length);");
// readObject code.
StringBuilder crf = new StringBuilder("protected void readObject(Object ret, ").append(GenericObjectInput.class.getName()).append(" in) throws java.io.IOException{");
crf.append("int fc = $2.readInt();");
crf.append("if( fc != ").append(fs.length).append(" ) throw new IllegalStateException(\"Deserialize Class [").append(cn).append("], field count not matched. Expect ").append(fs.length).append(" but get \" + fc +\".\");");
crf.append(cn).append(" ret = (").append(cn).append(")$1;");
// newInstance code.
StringBuilder cni = new StringBuilder("protected Object newInstance(").append(GenericObjectInput.class.getName()).append(" in){ return ");
Constructor<?> con = cs[0];
int mod = con.getModifiers();
boolean dn = Modifier.isPublic(mod) || (isp && !Modifier.isPrivate(mod));
if (dn) {
cni.append("new ").append(cn).append("(");
} else {
con.setAccessible(true);
cni.append("constructor.newInstance(new Object[]{");
}
Class<?>[] pts = con.getParameterTypes();
for (int i = 0; i < pts.length; i++) {
if (i > 0)
cni.append(',');
cni.append(defaultArg(pts[i]));
}
if (!dn)
// close object array.
cni.append("}");
cni.append("); }");
// get bean-style property metadata.
Map<String, PropertyMetadata> pms = propertyMetadatas(c);
List<Builder<?>> builders = new ArrayList<Builder<?>>(fs.length);
// field name, field type name.
String fn, ftn;
// field type.
Class<?> ft;
// direct access.
boolean da;
PropertyMetadata pm;
for (int i = 0; i < fs.length; i++) {
f = fs[i];
fn = f.getName();
ft = f.getType();
ftn = ReflectUtils.getName(ft);
da = isp && (f.getDeclaringClass() == c) && (Modifier.isPrivate(f.getModifiers()) == false);
if (da) {
pm = null;
} else {
pm = pms.get(fn);
if (pm != null && (pm.type != ft || pm.setter == null || pm.getter == null))
pm = null;
}
crf.append("if( fc == ").append(i).append(" ) return;");
if (ft.isPrimitive()) {
if (ft == boolean.class) {
if (da) {
cwf.append("$2.writeBool(v.").append(fn).append(");");
crf.append("ret.").append(fn).append(" = $2.readBool();");
} else if (pm != null) {
cwf.append("$2.writeBool(v.").append(pm.getter).append("());");
crf.append("ret.").append(pm.setter).append("($2.readBool());");
} else {
cwf.append("$2.writeBool(((Boolean)fields[").append(i).append("].get($1)).booleanValue());");
crf.append("fields[").append(i).append("].set(ret, ($w)$2.readBool());");
}
} else if (ft == byte.class) {
if (da) {
cwf.append("$2.writeByte(v.").append(fn).append(");");
crf.append("ret.").append(fn).append(" = $2.readByte();");
} else if (pm != null) {
cwf.append("$2.writeByte(v.").append(pm.getter).append("());");
crf.append("ret.").append(pm.setter).append("($2.readByte());");
} else {
cwf.append("$2.writeByte(((Byte)fields[").append(i).append("].get($1)).byteValue());");
crf.append("fields[").append(i).append("].set(ret, ($w)$2.readByte());");
}
} else if (ft == char.class) {
if (da) {
cwf.append("$2.writeShort((short)v.").append(fn).append(");");
crf.append("ret.").append(fn).append(" = (char)$2.readShort();");
} else if (pm != null) {
cwf.append("$2.writeShort((short)v.").append(pm.getter).append("());");
crf.append("ret.").append(pm.setter).append("((char)$2.readShort());");
} else {
cwf.append("$2.writeShort((short)((Character)fields[").append(i).append("].get($1)).charValue());");
crf.append("fields[").append(i).append("].set(ret, ($w)((char)$2.readShort()));");
}
} else if (ft == short.class) {
if (da) {
cwf.append("$2.writeShort(v.").append(fn).append(");");
crf.append("ret.").append(fn).append(" = $2.readShort();");
} else if (pm != null) {
cwf.append("$2.writeShort(v.").append(pm.getter).append("());");
crf.append("ret.").append(pm.setter).append("($2.readShort());");
} else {
cwf.append("$2.writeShort(((Short)fields[").append(i).append("].get($1)).shortValue());");
crf.append("fields[").append(i).append("].set(ret, ($w)$2.readShort());");
}
} else if (ft == int.class) {
if (da) {
cwf.append("$2.writeInt(v.").append(fn).append(");");
crf.append("ret.").append(fn).append(" = $2.readInt();");
} else if (pm != null) {
cwf.append("$2.writeInt(v.").append(pm.getter).append("());");
crf.append("ret.").append(pm.setter).append("($2.readInt());");
} else {
cwf.append("$2.writeInt(((Integer)fields[").append(i).append("].get($1)).intValue());");
crf.append("fields[").append(i).append("].set(ret, ($w)$2.readInt());");
}
} else if (ft == long.class) {
if (da) {
cwf.append("$2.writeLong(v.").append(fn).append(");");
crf.append("ret.").append(fn).append(" = $2.readLong();");
} else if (pm != null) {
cwf.append("$2.writeLong(v.").append(pm.getter).append("());");
crf.append("ret.").append(pm.setter).append("($2.readLong());");
} else {
cwf.append("$2.writeLong(((Long)fields[").append(i).append("].get($1)).longValue());");
crf.append("fields[").append(i).append("].set(ret, ($w)$2.readLong());");
}
} else if (ft == float.class) {
if (da) {
cwf.append("$2.writeFloat(v.").append(fn).append(");");
crf.append("ret.").append(fn).append(" = $2.readFloat();");
} else if (pm != null) {
cwf.append("$2.writeFloat(v.").append(pm.getter).append("());");
crf.append("ret.").append(pm.setter).append("($2.readFloat());");
} else {
cwf.append("$2.writeFloat(((Float)fields[").append(i).append("].get($1)).floatValue());");
crf.append("fields[").append(i).append("].set(ret, ($w)$2.readFloat());");
}
} else if (ft == double.class) {
if (da) {
cwf.append("$2.writeDouble(v.").append(fn).append(");");
crf.append("ret.").append(fn).append(" = $2.readDouble();");
} else if (pm != null) {
cwf.append("$2.writeDouble(v.").append(pm.getter).append("());");
crf.append("ret.").append(pm.setter).append("($2.readDouble());");
} else {
cwf.append("$2.writeDouble(((Double)fields[").append(i).append("].get($1)).doubleValue());");
crf.append("fields[").append(i).append("].set(ret, ($w)$2.readDouble());");
}
}
} else if (ft == c) {
if (da) {
cwf.append("this.writeTo(v.").append(fn).append(", $2);");
crf.append("ret.").append(fn).append(" = (").append(ftn).append(")this.parseFrom($2);");
} else if (pm != null) {
cwf.append("this.writeTo(v.").append(pm.getter).append("(), $2);");
crf.append("ret.").append(pm.setter).append("((").append(ftn).append(")this.parseFrom($2));");
} else {
cwf.append("this.writeTo((").append(ftn).append(")fields[").append(i).append("].get($1), $2);");
crf.append("fields[").append(i).append("].set(ret, this.parseFrom($2));");
}
} else {
int bc = builders.size();
builders.add(register(ft));
if (da) {
cwf.append("builders[").append(bc).append("].writeTo(v.").append(fn).append(", $2);");
crf.append("ret.").append(fn).append(" = (").append(ftn).append(")builders[").append(bc).append("].parseFrom($2);");
} else if (pm != null) {
cwf.append("builders[").append(bc).append("].writeTo(v.").append(pm.getter).append("(), $2);");
crf.append("ret.").append(pm.setter).append("((").append(ftn).append(")builders[").append(bc).append("].parseFrom($2));");
} else {
cwf.append("builders[").append(bc).append("].writeTo((").append(ftn).append(")fields[").append(i).append("].get($1), $2);");
crf.append("fields[").append(i).append("].set(ret, builders[").append(bc).append("].parseFrom($2));");
}
}
}
// skip any fields.
crf.append("for(int i=").append(fs.length).append(";i<fc;i++) $2.skipAny();");
// collection or map
if (isc) {
cwf.append("$2.writeInt(v.size()); for(java.util.Iterator it=v.iterator();it.hasNext();){ $2.writeObject(it.next()); }");
crf.append("int len = $2.readInt(); for(int i=0;i<len;i++) ret.add($2.readObject());");
} else if (ism) {
cwf.append("$2.writeInt(v.size()); for(java.util.Iterator it=v.entrySet().iterator();it.hasNext();){ java.util.Map.Entry entry = (java.util.Map.Entry)it.next(); $2.writeObject(entry.getKey()); $2.writeObject(entry.getValue()); }");
crf.append("int len = $2.readInt(); for(int i=0;i<len;i++) ret.put($2.readObject(), $2.readObject());");
}
cwf.append(" }");
crf.append(" }");
ClassGenerator cg = ClassGenerator.newInstance(cl);
cg.setClassName(bcn);
cg.setSuperClass(AbstractObjectBuilder.class);
cg.addDefaultConstructor();
cg.addField("public static java.lang.reflect.Field[] fields;");
cg.addField("public static " + BUILDER_CLASS_NAME + "[] builders;");
if (!dn)
cg.addField("public static java.lang.reflect.Constructor constructor;");
cg.addMethod("public Class getType(){ return " + cn + ".class; }");
cg.addMethod(cwf.toString());
cg.addMethod(crf.toString());
cg.addMethod(cni.toString());
try {
Class<?> wc = cg.toClass();
// set static field
wc.getField("fields").set(null, fs);
wc.getField("builders").set(null, builders.toArray(new Builder<?>[0]));
if (!dn)
wc.getField("constructor").set(null, con);
return (Builder<?>) wc.newInstance();
} catch (RuntimeException e) {
throw e;
} catch (Throwable e) {
throw new RuntimeException(e.getMessage(), e);
} finally {
cg.release();
}
}
use of java.lang.reflect.Constructor in project android_frameworks_base by AOSPA.
the class EffectFactory method instantiateEffect.
private Effect instantiateEffect(Class effectClass, String name) {
// Make sure this is an Effect subclass
try {
effectClass.asSubclass(Effect.class);
} catch (ClassCastException e) {
throw new IllegalArgumentException("Attempting to allocate effect '" + effectClass + "' which is not a subclass of Effect!", e);
}
// Look for the correct constructor
Constructor effectConstructor = null;
try {
effectConstructor = effectClass.getConstructor(EffectContext.class, String.class);
} catch (NoSuchMethodException e) {
throw new RuntimeException("The effect class '" + effectClass + "' does not have " + "the required constructor.", e);
}
// Construct the effect
Effect effect = null;
try {
effect = (Effect) effectConstructor.newInstance(mEffectContext, name);
} catch (Throwable t) {
throw new RuntimeException("There was an error constructing the effect '" + effectClass + "'!", t);
}
return effect;
}
use of java.lang.reflect.Constructor in project android_frameworks_base by AOSPA.
the class TransitionInflater method createCustom.
private Object createCustom(AttributeSet attrs, Class expectedType, String tag) {
String className = attrs.getAttributeValue(null, "class");
if (className == null) {
throw new InflateException(tag + " tag must have a 'class' attribute");
}
try {
synchronized (sConstructors) {
Constructor constructor = sConstructors.get(className);
if (constructor == null) {
Class c = mContext.getClassLoader().loadClass(className).asSubclass(expectedType);
if (c != null) {
constructor = c.getConstructor(sConstructorSignature);
constructor.setAccessible(true);
sConstructors.put(className, constructor);
}
}
return constructor.newInstance(mContext, attrs);
}
} catch (InstantiationException e) {
throw new InflateException("Could not instantiate " + expectedType + " class " + className, e);
} catch (ClassNotFoundException e) {
throw new InflateException("Could not instantiate " + expectedType + " class " + className, e);
} catch (InvocationTargetException e) {
throw new InflateException("Could not instantiate " + expectedType + " class " + className, e);
} catch (NoSuchMethodException e) {
throw new InflateException("Could not instantiate " + expectedType + " class " + className, e);
} catch (IllegalAccessException e) {
throw new InflateException("Could not instantiate " + expectedType + " class " + className, e);
}
}
use of java.lang.reflect.Constructor in project intellij-community by JetBrains.
the class NotNullVerifyingInstrumenterTest method testConstructorParam.
public void testConstructorParam() throws Exception {
Class<?> testClass = prepareTest();
Constructor method = testClass.getConstructor(Object.class);
verifyCallThrowsException("Argument 0 for @NotNull parameter of ConstructorParam.<init> must not be null", null, method, (Object) null);
}
Aggregations