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:
igor%mir2.org 2003-03-20 15:46:37 +00:00
Родитель b7852204ac
Коммит 9a1096723f
5 изменённых файлов: 49 добавлений и 45 удалений

Просмотреть файл

@ -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();
}
catch (SecurityException x) {
}
catch (IllegalArgumentException x) {
}
catch (InstantiationException x) {
}
catch (IllegalAccessException x) {
}
// fall through
result = (Interpreter)ScriptRuntime.newInstanceOrNull(codegenClass);
}
return new Interpreter();
if (result == null) {
result = 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,16 +251,15 @@ 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]);
return n;
Delegator n = newInstance();
Scriptable delegee;
if (args.length == 0) {
delegee = Undefined.instance;
} else {
delegee = ScriptRuntime.toObject(cx, scope, args[0]);
}
catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
return null;
n.setDelegee(delegee);
return n;
}
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 {