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"); "org.mozilla.javascript.optimizer.OptClassNameHelper");
// nameHelperClass == null if running lite // nameHelperClass == null if running lite
if (nameHelperClass != null) { if (nameHelperClass != null) {
try { helper = (ClassNameHelper)ScriptRuntime.newInstanceOrNull(
helper = (ClassNameHelper)nameHelperClass.newInstance(); nameHelperClass);
} catch (IllegalAccessException x) {
} catch (InstantiationException x) {
}
} }
if (helper != null) { if (helper != null) {
savedNameHelper = helper; savedNameHelper = helper;

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

@ -2031,21 +2031,14 @@ public class Context {
"org.mozilla.javascript.optimizer.Codegen"); "org.mozilla.javascript.optimizer.Codegen");
private Interpreter createCompiler() { private Interpreter createCompiler() {
Interpreter result = null;
if (optimizationLevel >= 0 && codegenClass != null) { if (optimizationLevel >= 0 && codegenClass != null) {
try { result = (Interpreter)ScriptRuntime.newInstanceOrNull(codegenClass);
return (Interpreter) codegenClass.newInstance();
}
catch (SecurityException x) {
}
catch (IllegalArgumentException x) {
}
catch (InstantiationException x) {
}
catch (IllegalAccessException x) {
}
// fall through
} }
return new Interpreter(); if (result == null) {
result = new Interpreter();
}
return result;
} }
private Parser createParser(IRFactory irf) { private Parser createParser(IRFactory irf) {
@ -2109,12 +2102,7 @@ public class Context {
Class cl = ScriptRuntime.getClassOrNull( Class cl = ScriptRuntime.getClassOrNull(
"org.mozilla.javascript.regexp.RegExpImpl"); "org.mozilla.javascript.regexp.RegExpImpl");
if (cl != null) { if (cl != null) {
try { regExpProxy = (RegExpProxy)ScriptRuntime.newInstanceOrNull(cl);
regExpProxy = (RegExpProxy) cl.newInstance();
return regExpProxy;
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
}
} }
} }
return regExpProxy; return regExpProxy;

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

@ -76,6 +76,25 @@ public class Delegator implements Function {
this.obj = obj; 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. * Retrieve the delegee.
* *
@ -232,16 +251,15 @@ public class Delegator implements Function {
if (obj == null) { if (obj == null) {
//this little trick allows us to declare prototype objects for //this little trick allows us to declare prototype objects for
//Delegators //Delegators
try { Delegator n = newInstance();
Delegator n = (Delegator)this.getClass().newInstance(); Scriptable delegee;
n.setDelegee((Scriptable)args[0]); if (args.length == 0) {
return n; delegee = Undefined.instance;
} else {
delegee = ScriptRuntime.toObject(cx, scope, args[0]);
} }
catch (Exception e) { n.setDelegee(delegee);
e.printStackTrace(); return n;
System.exit(0);
}
return null;
} }
else { else {
return ((Function)obj).construct(cx,scope,args); return ((Function)obj).construct(cx,scope,args);

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

@ -689,12 +689,7 @@ public class FunctionObject extends BaseFunction {
private static Invoker newInvokerMaster() { private static Invoker newInvokerMaster() {
Class cl = ScriptRuntime.getClassOrNull(INVOKER_MASTER_CLASS); Class cl = ScriptRuntime.getClassOrNull(INVOKER_MASTER_CLASS);
if (cl != null) { if (cl != null) {
try { return (Invoker)ScriptRuntime.newInstanceOrNull(cl);
return (Invoker)cl.newInstance();
}
catch (IllegalAccessException ex) {}
catch (InstantiationException ex) {}
catch (SecurityException ex) {}
} }
return null; return null;
} }

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

@ -1826,12 +1826,7 @@ public class ScriptRuntime {
{ {
Context cx = Context.enter(); Context cx = Context.enter();
try { try {
Script script = null; Script script = (Script)newInstanceOrNull(scriptClass);
try {
script = (Script)scriptClass.newInstance();
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
}
if (script == null) { if (script == null) {
throw new RuntimeException("Error creating script object"); throw new RuntimeException("Error creating script object");
} }
@ -2031,6 +2026,17 @@ public class ScriptRuntime {
return null; 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) { static boolean hasProp(Scriptable start, String name) {
Scriptable m = start; Scriptable m = start;
do { do {