]> source.dussan.org Git - javassist.git/commitdiff
Fixed a bug of the token analyzer (double constant).
authorchiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3>
Sat, 20 Dec 2003 23:43:39 +0000 (23:43 +0000)
committerchiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3>
Sat, 20 Dec 2003 23:43:39 +0000 (23:43 +0000)
Improved compilation speed.

git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@58 30ef5769-5b8d-40dd-aea6-55b5d6557bb3

Readme.html
src/main/javassist/ClassPool.java
src/main/javassist/ClassPoolTail.java
src/main/javassist/CtClass.java
src/main/javassist/CtClassType.java
src/main/javassist/compiler/Lex.java
src/main/javassist/compiler/MemberCodeGen.java
src/main/javassist/compiler/MemberResolver.java

index cca4e09eeed78bc77fd379c39c51030ed3208cf4..bb6c53ba66cad9beb055f3ad84bff3910d2498b3 100644 (file)
@@ -534,7 +534,7 @@ Fabian Crabus, Bo Norregaard Jorgensen, Bob Lee, Bill Burke,
 Remy Sanlaville, Muga Nishizawa, Alexey Loubyansky, Saori Oki,
 Andreas Salathe, Dante Torres estrada, S. Pam, Nuno Santos,
 Denis Taye, Colin Sampaleanu, Robert Bialek, Asato Shimotaki,
-and Howard Lewis Ship for their contributions.
+Howard Lewis Ship, and Richard Jones for their contributions.
 
 <p><br>
 
index 29d8acb5c18bfab5b7531c0333d2426ce185bfcb..394c84100cf020369e74f8d8b9fdafb9868dabd6 100644 (file)
@@ -202,6 +202,20 @@ public class ClassPool {
         return source.toString();
     }
 
+    /**
+     * Records a name that never exists.  For example, a package name
+     * can be recorded by this method.
+     * This would improve execution performance
+     * since <code>get()</code> does not search the class path at all
+     * if the given name is an invalid name recorded by this method.
+     * Note that searching the class path takes relatively long time.
+     *
+     * @param name          a class name (separeted by dot).
+     */
+    public void recordInvalidClassName(String name) {
+        source.recordInvalidClassName(name);
+    }
+
     /**
      * Returns the <code>Translator</code> object associated with
      * this <code>ClassPool</code>.
index 5b013bf8720199b9bb1cae73f7e0d45d71d75d9f..645601475f507ff05c7a55e40d9c37bc757b280d 100644 (file)
@@ -17,6 +17,7 @@ package javassist;
 
 import java.io.*;
 import java.util.zip.*;
+import java.util.Hashtable;
 
 final class ClassPathList {
     ClassPathList next;
@@ -130,10 +131,12 @@ final class JarClassPath implements ClassPath {
 final class ClassPoolTail extends ClassPool {
     protected ClassPathList pathList;
     private Class thisClass;
+    private Hashtable packages;         // should be synchronized.
 
     public ClassPoolTail() {
         pathList = null;
         thisClass = getClass();
+        packages = new Hashtable();
     }
 
     public String toString() {
@@ -150,6 +153,14 @@ final class ClassPoolTail extends ClassPool {
         return buf.toString();
     }
 
+    /**
+     * You can record "System" so that java.lang.System can be quickly
+     * found although "System" is not a package name.
+     */
+    public void recordInvalidClassName(String name) {
+        packages.put(name, name);
+    }
+
     public byte[] write(String classname)
         throws NotFoundException, IOException
     {
@@ -274,6 +285,9 @@ final class ClassPoolTail extends ClassPool {
     public InputStream openClassfile(String classname)
         throws NotFoundException
     {
+        if (packages.get(classname) != null)
+            throw new NotFoundException(classname);
+
         ClassPathList list = pathList;
         InputStream ins = null;
         NotFoundException error = null;
index 8d50a7b69bf0993b5f26653ba09654836e513c10..b4f4e26a6f0e42fa99878b4231fe4be7a728969e 100644 (file)
@@ -35,7 +35,7 @@ public abstract class CtClass {
     /**
      * The version number of this release.
      */
-    public static final String version = "2.7 alpha 5";
+    public static final String version = "2.7 alpha 6";
 
     /**
      * Prints the version number and the copyright notice.
@@ -485,6 +485,11 @@ public abstract class CtClass {
         throw new NotFoundException(name);
     }
 
+    /**
+     * @return null     if the specified field is not found.
+     */
+    CtField getField2(String name) { return null; }
+
     /**
      * Gets all the fields declared in the class.  The inherited fields
      * are not included.
index de1b2f19ee85a7b955c589c1c8f0679b03cfe3a8..943f9ea971a001bc46b80c00dab6e4c9967e71f2 100644 (file)
@@ -331,30 +331,33 @@ class CtClassType extends CtClass {
     }
 
     public CtField getField(String name) throws NotFoundException {
-        try {
-            return getDeclaredField(name);
-        }
-        catch (NotFoundException e) {}
+        CtField f = getField2(name);
+        if (f == null)
+            throw new NotFoundException("field: " + name + " in " + getName());
+        else
+            return f;
+    }
+
+    CtField getField2(String name) {
+        CtField df = getDeclaredField2(name);
+        if (df != null)
+            return df;
 
         try {
             CtClass[] ifs = getInterfaces();
             int num = ifs.length;
-            for (int i = 0; i < num; ++i)
-                try {
-                    return ifs[i].getField(name);
-                }
-                catch (NotFoundException e) {}
-        }
-        catch (NotFoundException e) {}
+            for (int i = 0; i < num; ++i) {
+                CtField f = ifs[i].getField2(name);
+                if (f != null)
+                    return f;
+            }
 
-        try {
             CtClass s = getSuperclass();
             if (s != null)
-                return s.getField(name);
+                return s.getField2(name);
         }
         catch (NotFoundException e) {}
-
-        throw new NotFoundException(name);
+        return null;
     }
 
     public CtField[] getDeclaredFields() {
@@ -385,6 +388,14 @@ class CtClassType extends CtClass {
     }
 
     public CtField getDeclaredField(String name) throws NotFoundException {
+        CtField f = getDeclaredField2(name);
+        if (f == null)
+            throw new NotFoundException("field: " + name + " in " + getName());
+        else
+            return f;
+    }
+
+    private CtField getDeclaredField2(String name) {
         CtField cf = getFieldsCache();
         while (cf != null) {
             if (cf.getName().equals(name))
@@ -393,7 +404,7 @@ class CtClassType extends CtClass {
             cf = cf.next;
         }
 
-        throw new NotFoundException(name);
+        return null;
     }
 
     public CtBehavior[] getDeclaredBehaviors() {
index baf4742eb7e8338db1f6225e27486f2ff6354094..76a22fdf330225a7987308c7b03f09a6c84768e7 100644 (file)
@@ -293,7 +293,8 @@ public class Lex implements TokenId {
             token.doubleValue = (double)value;
             return FloatConstant;
         }
-        else if (c2 == 'E' || c2 == 'e' || c2 == '.') {
+        else if (c2 == 'E' || c2 == 'e'
+                 || c2 == 'D' || c2 == 'd' || c2 == '.') {
             StringBuffer tbuf = textBuffer;
             tbuf.setLength(0);
             tbuf.append(value);
@@ -308,7 +309,7 @@ public class Lex implements TokenId {
     }
 
     private int readDouble(StringBuffer sbuf, int c, Token token) {
-        if (c != 'E' && c != 'e') {
+        if (c != 'E' && c != 'e' && c != 'D' && c != 'd') {
             sbuf.append((char)c);
             for (;;) {
                 c = getc();
@@ -343,7 +344,9 @@ public class Lex implements TokenId {
         if (c == 'F' || c == 'f')
             return FloatConstant;
         else {
-            ungetc(c);
+            if (c != 'D' && c != 'd')
+                ungetc(c);
+
             return DoubleConstant;
         }
     }
index 8499dfb02e448d534c1399e597b82adf53c61567..2d1a747a03e2eeaa8bf51fc62248a184db7a9b86 100644 (file)
@@ -283,6 +283,7 @@ public class MemberCodeGen extends CodeGen {
                     exprType = CLASS;
                     arrayDim = 0;
                     className = nfe.getField(); // JVM-internal
+                    resolver.recordPackage(className);
                     isStatic = true;
                 }
 
@@ -594,10 +595,9 @@ public class MemberCodeGen extends CodeGen {
     /* This method also returns a value in resultStatic.
      */
     protected CtField fieldAccess(ASTree expr) throws CompileError {
-        CtField f = null;
-        boolean is_static = false;
         if (expr instanceof Member) {
             String name = ((Member)expr).get();
+            CtField f = null;
             try {
                 f = thisClass.getField(name);
             }
@@ -606,35 +606,47 @@ public class MemberCodeGen extends CodeGen {
                 throw new NoFieldException(name, expr);
             }
 
-            is_static = Modifier.isStatic(f.getModifiers());
+            boolean is_static = Modifier.isStatic(f.getModifiers());
             if (!is_static)
                 if (inStaticMethod)
                     throw new CompileError(
                                 "not available in a static method: " + name);
                 else
                     bytecode.addAload(0);       // this
+
+            resultStatic = is_static;
+            return f;
         }
         else if (expr instanceof Expr) {
             Expr e = (Expr)expr;
             int op = e.getOperator();
             if (op == MEMBER) {
                 // static member by # (extension by Javassist)
-                f = resolver.lookupField(((Symbol)e.oprand1()).get(),
+                CtField f = resolver.lookupField(((Symbol)e.oprand1()).get(),
                                          (Symbol)e.oprand2());
-                is_static = true;
+                resultStatic = true;
+                return f;
             }
             else if (op == '.') {
+                CtField f = null;
                 try {
                     e.oprand1().accept(this);
+                    /* Don't call lookupFieldByJvmName2().
+                     * The left operand of . is not a class name but
+                     * a normal expression.
+                     */
                     if (exprType == CLASS && arrayDim == 0)
                         f = resolver.lookupFieldByJvmName(className,
                                                     (Symbol)e.oprand2());
                     else
                         badLvalue();
 
-                    is_static = Modifier.isStatic(f.getModifiers());
+                    boolean is_static = Modifier.isStatic(f.getModifiers());
                     if (is_static)
                         bytecode.addOpcode(POP);
+
+                    resultStatic = is_static;
+                    return f;
                 }
                 catch (NoFieldException nfe) {
                     if (nfe.getExpr() != e.oprand1())
@@ -645,9 +657,11 @@ public class MemberCodeGen extends CodeGen {
                      * lookupFieldByJvmName2() throws NoFieldException.
                      */
                     Symbol fname = (Symbol)e.oprand2();
-                    f = resolver.lookupFieldByJvmName2(nfe.getField(),
-                                                       fname, expr);
-                    is_static = true;
+                    String cname = nfe.getField();
+                    f = resolver.lookupFieldByJvmName2(cname, fname, expr);
+                    resolver.recordPackage(cname);
+                    resultStatic = true;
+                    return f;
                 }
             }
             else
@@ -656,8 +670,8 @@ public class MemberCodeGen extends CodeGen {
         else
             badLvalue();
 
-        resultStatic = is_static;
-        return f;
+        resultStatic = false;
+        return null;    // never reach
     }
 
     private static void badLvalue() throws CompileError {
index 6e9c33c1434d21cdc0adfe98d4f0ba73b1bbd62a..f266ddf8260f082ee719a22b0b3a08c776f03f9d 100644 (file)
@@ -35,6 +35,22 @@ public class MemberResolver implements TokenId {
         throw new CompileError("fatal");
     }
 
+    /**
+     * @param jvmClassName      a class name.  Not a package name.
+     */
+    public void recordPackage(String jvmClassName) {
+        String classname = jvmToJavaName(jvmClassName);
+        for (;;) {
+            int i = classname.lastIndexOf('.');
+            if (i > 0) {
+                classname = classname.substring(0, i);
+                classPool.recordInvalidClassName(classname);
+            }
+            else
+                break;
+        }
+    }
+
     public static class Method {
         public CtClass declaring;
         public MethodInfo info;
@@ -336,12 +352,18 @@ public class MemberResolver implements TokenId {
             return lookupClass0(name, notCheckInner);
         }
         catch (NotFoundException e) {}
-
-        try {
-            if (name.indexOf('.') < 0)
-                return classPool.get("java.lang." + name);
+        if (name.indexOf('.') < 0) {
+            String jlangName = "java.lang." + name;
+            try {
+                CtClass cc = classPool.get(jlangName);
+                // if java.lang... is found, then
+                classPool.recordInvalidClassName(name);  
+                return cc;
+            }
+            catch (NotFoundException e) {
+                classPool.recordInvalidClassName(jlangName);
+            }
         }
-        catch (NotFoundException e) {}
 
         throw new CompileError("no such class: " + name);
     }