cvs: /php4/sapi/servlet cookies.php date.php jinfo.php jver.php reqheaders.php reqinfo.php reqparams.php sessions.php /php4/ext/java jver.php
reflect.java /php4/sapi/servlet README
| From: | Sam Ruby | Date: | Mon, 28 Feb 2000 17:02:26 +0000 |
| Subject: | cvs: /php4/sapi/servlet cookies.php date.php jinfo.php jver.php reqheaders.php reqinfo.php reqparams.php sessions.php /php4/ext/java jver.php reflect.java /php4/sapi/servlet README |
||
| Groups: | php.version4 | ||
| Request: | Send a blank email to php-version4+get-11145@lists.php.net to get a copy of this message | ||
rubys Mon Feb 28 09:02:26 2000 EDT
Added files:
/php4/sapi/servlet cookies.php date.php jinfo.php jver.php
reqheaders.php reqinfo.php reqparams.php
sessions.php
Modified files:
/php4/ext/java jver.php reflect.java
/php4/sapi/servlet README
Log:
Improved handing of instances of private classes. Added servlet examples.
Index: php4/ext/java/jver.php
diff -u php4/ext/java/jver.php:1.1 php4/ext/java/jver.php:1.2
--- php4/ext/java/jver.php:1.1 Tue Nov 9 04:02:19 1999
+++ php4/ext/java/jver.php Mon Feb 28 09:02:26 2000
@@ -1,3 +1,4 @@
+<html>
<?
$system = new Java("java.lang.System");
@@ -13,3 +14,4 @@
print $formatter->format(new Java("java.util.Date"))."\n";
?>
+</html>
Index: php4/ext/java/reflect.java
diff -u php4/ext/java/reflect.java:1.6 php4/ext/java/reflect.java:1.7
--- php4/ext/java/reflect.java:1.6 Sun Feb 20 04:09:33 2000
+++ php4/ext/java/reflect.java Mon Feb 28 09:02:26 2000
@@ -102,22 +102,30 @@
//
public static void CreateObject(String name, Object args[], long result) {
try {
+ Vector matches = new Vector();
+
Constructor cons[] = Class.forName(name).getConstructors();
for (int i=0; i<cons.length; i++) {
if (cons[i].getParameterTypes().length == args.length) {
- setResult(result, cons[i].newInstance(args));
- return;
+ matches.addElement(cons[i]);
}
}
- // for classes which have no visible constructor, return the class
- // useful for classes like java.lang.System and java.util.Calendar.
- if (args.length == 0) {
- setResult(result, Class.forName(name));
- return;
+ Constructor selected = (Constructor)select(matches, args);
+
+ if (selected == null) {
+ if (args.length > 0) {
+ throw new InstantiationException("No matching constructor found");
+ } else {
+ // for classes which have no visible constructor, return the class
+ // useful for classes like java.lang.System and java.util.Calendar.
+ setResult(result, Class.forName(name));
+ return;
+ }
}
- throw new InstantiationException("No matching constructor found");
+ Object coercedArgs[] = coerce(selected.getParameterTypes(), args);
+ setResult(result, selected.newInstance(coercedArgs));
} catch (Exception e) {
setException(result, e);
@@ -127,16 +135,20 @@
//
// Select the best match from a list of methods
//
- private static Method select(Vector methods, Object args[]) {
- if (methods.size() == 1) return (Method) methods.firstElement();
+ private static Object select(Vector methods, Object args[]) {
+ if (methods.size() == 1) return methods.firstElement();
- Method selected = null;
+ Object selected = null;
int best = Integer.MAX_VALUE;
for (Enumeration e = methods.elements(); e.hasMoreElements(); ) {
- Method method = (Method)e.nextElement();
+ Object element = e.nextElement();
int weight=0;
- Class parms[] = method.getParameterTypes();
+
+ Class parms[] = (element instanceof Method) ?
+ ((Method)element).getParameterTypes() :
+ ((Constructor)element).getParameterTypes();
+
for (int i=0; i<parms.length; i++) {
if (parms[i].isInstance(args[i])) {
for (Class c=parms[i]; (c=c.getSuperclass()) != null; ) {
@@ -168,9 +180,9 @@
}
if (weight < best) {
- if (weight == 0) return method;
+ if (weight == 0) return element;
best = weight;
- selected = method;
+ selected = element;
}
}
@@ -178,11 +190,13 @@
}
//
- // Select the best match from a list of methods
+ // Coerce arguments when possible to conform to the argument list.
+ // Java's reflection will automatically do widening conversions,
+ // unfortunately PHP only supports wide formats, so to be practical
+ // some (possibly lossy) conversions are required.
//
- private static Object[] coerce(Method method, Object args[]) {
+ private static Object[] coerce(Class parms[], Object args[]) {
Object result[] = args;
- Class parms[] = method.getParameterTypes();
for (int i=0; i<args.length; i++) {
if (parms[i].isInstance(args[i])) continue;
if (args[i] instanceof Number && parms[i].isPrimitive()) {
@@ -238,10 +252,10 @@
if (!(object instanceof Class) || (jclass==object)) break;
}
- Method selected = select(matches, args);
+ Method selected = (Method)select(matches, args);
if (selected == null) throw new NoSuchMethodException(method);
- Object coercedArgs[] = coerce(selected, args);
+ Object coercedArgs[] = coerce(selected.getParameterTypes(), args);
setResult(result, selected.invoke(object, coercedArgs));
} catch (Exception e) {
@@ -258,6 +272,18 @@
try {
for (Class jclass = object.getClass();;jclass=(Class)object) {
+ while (!Modifier.isPublic(jclass.getModifiers())) {
+ // OK, some joker gave us an instance of a non-public class
+ // Substitute the first public interface in its place,
+ // and barring that, try the superclass
+ Class interfaces[] = jclass.getInterfaces();
+ jclass=jclass.getSuperclass();
+ for (int i=interfaces.length; i-->0;) {
+ if (Modifier.isPublic(interfaces[i].getModifiers())) {
+ jclass=interfaces[i];
+ }
+ }
+ }
BeanInfo beanInfo = Introspector.getBeanInfo(jclass);
PropertyDescriptor props[] = beanInfo.getPropertyDescriptors();
for (int i=0; i<props.length; i++) {
Index: php4/sapi/servlet/README
diff -u php4/sapi/servlet/README:1.2 php4/sapi/servlet/README:1.3
--- php4/sapi/servlet/README:1.2 Sat Jan 29 05:22:46 2000
+++ php4/sapi/servlet/README Mon Feb 28 09:02:26 2000
@@ -12,17 +12,7 @@
reports, success stories and/or patches required to get this code
to run on other engines would be appreciated.
- 2) This code clean compiles on Win32 and Linux, and is able to process
- phpinfo() commands. Most of the code is in place, but very little
- testing has been done on even such basic things as cookies and
- sessions. Treat this code as early alpha at this point.
-
- 3) Until overload resolution is addressed in php/java, much of the
- javax.servlet interfaces can not be directly called. For example,
- don't try to get the output stream from $response, and expect to
- be able to use println. For now, use PHP's "echo" instead.
-
- 4) PHP has a habit of changing the working directory. Sapi/servlet will
+ 2) PHP has a habit of changing the working directory. Sapi/servlet will
eventually change it back, but while PHP is running the servlet engine
may not be able to load any classes from the CLASSPATH which are
specified using a relative directory syntax, or find the work directory