зеркало из https://github.com/mozilla/pjs.git
Added ScriptRuntime.newInstanceOrNull to call class.newInstance() and return null if it fails. It allows to replace in few places try/3 different catch by a simple method call.
This commit is contained in:
Родитель
b7852204ac
Коммит
9a1096723f
|
@ -49,11 +49,8 @@ public abstract class ClassNameHelper {
|
|||
"org.mozilla.javascript.optimizer.OptClassNameHelper");
|
||||
// nameHelperClass == null if running lite
|
||||
if (nameHelperClass != null) {
|
||||
try {
|
||||
helper = (ClassNameHelper)nameHelperClass.newInstance();
|
||||
} catch (IllegalAccessException x) {
|
||||
} catch (InstantiationException x) {
|
||||
}
|
||||
helper = (ClassNameHelper)ScriptRuntime.newInstanceOrNull(
|
||||
nameHelperClass);
|
||||
}
|
||||
if (helper != null) {
|
||||
savedNameHelper = helper;
|
||||
|
|
|
@ -2031,21 +2031,14 @@ public class Context {
|
|||
"org.mozilla.javascript.optimizer.Codegen");
|
||||
|
||||
private Interpreter createCompiler() {
|
||||
Interpreter result = null;
|
||||
if (optimizationLevel >= 0 && codegenClass != null) {
|
||||
try {
|
||||
return (Interpreter) codegenClass.newInstance();
|
||||
result = (Interpreter)ScriptRuntime.newInstanceOrNull(codegenClass);
|
||||
}
|
||||
catch (SecurityException x) {
|
||||
if (result == null) {
|
||||
result = new Interpreter();
|
||||
}
|
||||
catch (IllegalArgumentException x) {
|
||||
}
|
||||
catch (InstantiationException x) {
|
||||
}
|
||||
catch (IllegalAccessException x) {
|
||||
}
|
||||
// fall through
|
||||
}
|
||||
return new Interpreter();
|
||||
return result;
|
||||
}
|
||||
|
||||
private Parser createParser(IRFactory irf) {
|
||||
|
@ -2109,12 +2102,7 @@ public class Context {
|
|||
Class cl = ScriptRuntime.getClassOrNull(
|
||||
"org.mozilla.javascript.regexp.RegExpImpl");
|
||||
if (cl != null) {
|
||||
try {
|
||||
regExpProxy = (RegExpProxy) cl.newInstance();
|
||||
return regExpProxy;
|
||||
} catch (InstantiationException e) {
|
||||
} catch (IllegalAccessException e) {
|
||||
}
|
||||
regExpProxy = (RegExpProxy)ScriptRuntime.newInstanceOrNull(cl);
|
||||
}
|
||||
}
|
||||
return regExpProxy;
|
||||
|
|
|
@ -76,6 +76,25 @@ public class Delegator implements Function {
|
|||
this.obj = obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Crete new Delegator instance.
|
||||
* The default implementation calls this.getClass().newInstance().
|
||||
*
|
||||
* @see #construct(Context cx, Scriptable scope, Object[] args)
|
||||
*/
|
||||
protected Delegator newInstance()
|
||||
{
|
||||
Exception ex;
|
||||
try {
|
||||
return (Delegator)this.getClass().newInstance();
|
||||
} catch (InstantiationException e) {
|
||||
ex = e;
|
||||
} catch (IllegalAccessException e) {
|
||||
ex = e;
|
||||
}
|
||||
throw WrappedException.wrapException(ex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the delegee.
|
||||
*
|
||||
|
@ -232,17 +251,16 @@ public class Delegator implements Function {
|
|||
if (obj == null) {
|
||||
//this little trick allows us to declare prototype objects for
|
||||
//Delegators
|
||||
try {
|
||||
Delegator n = (Delegator)this.getClass().newInstance();
|
||||
n.setDelegee((Scriptable)args[0]);
|
||||
Delegator n = newInstance();
|
||||
Scriptable delegee;
|
||||
if (args.length == 0) {
|
||||
delegee = Undefined.instance;
|
||||
} else {
|
||||
delegee = ScriptRuntime.toObject(cx, scope, args[0]);
|
||||
}
|
||||
n.setDelegee(delegee);
|
||||
return n;
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.exit(0);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
return ((Function)obj).construct(cx,scope,args);
|
||||
}
|
||||
|
|
|
@ -689,12 +689,7 @@ public class FunctionObject extends BaseFunction {
|
|||
private static Invoker newInvokerMaster() {
|
||||
Class cl = ScriptRuntime.getClassOrNull(INVOKER_MASTER_CLASS);
|
||||
if (cl != null) {
|
||||
try {
|
||||
return (Invoker)cl.newInstance();
|
||||
}
|
||||
catch (IllegalAccessException ex) {}
|
||||
catch (InstantiationException ex) {}
|
||||
catch (SecurityException ex) {}
|
||||
return (Invoker)ScriptRuntime.newInstanceOrNull(cl);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -1826,12 +1826,7 @@ public class ScriptRuntime {
|
|||
{
|
||||
Context cx = Context.enter();
|
||||
try {
|
||||
Script script = null;
|
||||
try {
|
||||
script = (Script)scriptClass.newInstance();
|
||||
} catch (InstantiationException e) {
|
||||
} catch (IllegalAccessException e) {
|
||||
}
|
||||
Script script = (Script)newInstanceOrNull(scriptClass);
|
||||
if (script == null) {
|
||||
throw new RuntimeException("Error creating script object");
|
||||
}
|
||||
|
@ -2031,6 +2026,17 @@ public class ScriptRuntime {
|
|||
return null;
|
||||
}
|
||||
|
||||
static Object newInstanceOrNull(Class cl)
|
||||
{
|
||||
try {
|
||||
return cl.newInstance();
|
||||
} catch (SecurityException x) {
|
||||
} catch (InstantiationException x) {
|
||||
} catch (IllegalAccessException x) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static boolean hasProp(Scriptable start, String name) {
|
||||
Scriptable m = start;
|
||||
do {
|
||||
|
|
Загрузка…
Ссылка в новой задаче