Coverage Report - javafx.reflect.LocalReflectionContext
 
Classes in this File Line Coverage Branch Coverage Complexity
LocalClassRef
78%
29/37
79%
19/24
0
LocalClassRef$SortedClassArray
96%
23/24
83%
10/12
0
LocalObjectRef
71%
5/7
0%
0/2
0
LocalReflectionContext
81%
38/47
83%
10/12
0
LocalReflectionContext$1
0%
0/3
N/A
0
 
 1  
 /*
 2  
  * Copyright 1999-2008 Sun Microsystems, Inc.  All Rights Reserved.
 3  
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  
  *
 5  
  * This code is free software; you can redistribute it and/or modify it
 6  
  * under the terms of the GNU General Public License version 2 only, as
 7  
  * published by the Free Software Foundation.  Sun designates this
 8  
  * particular file as subject to the "Classpath" exception as provided
 9  
  * by Sun in the LICENSE file that accompanied this code.
 10  
  *
 11  
  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  
  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  
  * version 2 for more details (a copy is included in the LICENSE file that
 15  
  * accompanied this code).
 16  
  *
 17  
  * You should have received a copy of the GNU General Public License version
 18  
  * 2 along with this work; if not, write to the Free Software Foundation,
 19  
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  
  *
 21  
  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 22  
  * CA 95054 USA or visit www.sun.com if you need additional information or
 23  
  * have any questions.
 24  
  */
 25  
 
 26  
 package javafx.reflect;
 27  
 import java.util.*;
 28  
 
 29  
 /** Implementation of {@link ReflectionContext} using Java reflection.
 30  
  * Can only access objects and types in the current JVM.
 31  
  * Normally, this is a singleton, though in the future there might
 32  
  * be variants with different class search paths (similar to
 33  
  * {@code com.sun.jdi.PathSearchingVirtualMachine}).
 34  
  */
 35  
 
 36  
 public class LocalReflectionContext extends ReflectionContext {
 37  1
     static LocalReflectionContext instance = new LocalReflectionContext();
 38  
 
 39  1
     private LocalReflectionContext () { 
 40  1
     }
 41  
 
 42  
     /** Get the default instance. */
 43  1
     public static LocalReflectionContext getInstance() { return instance; }
 44  
 
 45  
     /** Create a reference to a given Object. */
 46  
     public ObjectRef makeObjectRef(Object obj) {
 47  1
         return new LocalObjectRef(obj, this);
 48  
         //throw new Error();
 49  
         //return new LocalObjectRef(this, obj);
 50  
     }
 51  
 
 52  
     /** Get the {@code ClassRef} for the class with the given name. */
 53  
     public ClassRef findClass(String cname) {
 54  
         ClassLoader loader;
 55  
         try {
 56  3
             loader = Thread.currentThread().getContextClassLoader();
 57  
         }
 58  0
         catch (java.lang.SecurityException ex) {
 59  0
            loader = getClass().getClassLoader();
 60  3
         }
 61  3
         return findClass(cname, loader);
 62  
     }
 63  
 
 64  
     /** Get the {@code ClassRef} for the class with the given name. */
 65  
     public ClassRef findClass(String cname, ClassLoader loader) {
 66  3
         String n = cname;
 67  3
         Exception ex0 = null;
 68  
         for (;;) {
 69  
             try {
 70  4
                 Class cl = Class.forName(n, false, loader);
 71  
                 // if (! cl.getCanonicalName().equals(cname)) break;
 72  3
                 return makeClassRef(cl);
 73  1
             } catch (Exception ex) {
 74  1
                 if (ex0 == null)
 75  1
                     ex0 = ex;
 76  1
                 int dot = n.lastIndexOf('.');
 77  1
                 if (dot < 0)
 78  0
                     break;
 79  1
                 n = n.substring(0, dot)+'$'+n.substring(dot+1);
 80  1
             }
 81  
         }
 82  0
         throw new RuntimeException(ex0);
 83  
     }
 84  
 
 85  
     /** Create a reference to a given Class. */
 86  
     public ClassRef makeClassRef(Class cls) {
 87  22
         int modifiers = 0;
 88  
         try {
 89  22
             String cname = cls.getName();
 90  22
             Class clsInterface = null;
 91  22
             if (cname.endsWith(INTERFACE_SUFFIX)) {
 92  12
                 clsInterface = cls;
 93  12
                 cname = cname.substring(0, cname.length()-INTERFACE_SUFFIX.length());
 94  12
                 cls = Class.forName(cname, false, cls.getClassLoader());
 95  12
                 modifiers = ClassRef.COMPOUND_CLASS|ClassRef.FX_CLASS;
 96  12
                 return new LocalClassRef(this, modifiers, cls, clsInterface);
 97  
             }
 98  10
             Class[] interfaces = cls.getInterfaces();
 99  10
             String intfName = cname + INTERFACE_SUFFIX;
 100  17
             for (int i = 0;  i < interfaces.length;  i++ ) {
 101  7
                 String iname = interfaces[i].getName();
 102  7
                 if (iname.equals(FXOBJECT_NAME))
 103  2
                     modifiers |= ClassRef.FX_CLASS;
 104  5
                 else if (iname.equals(intfName)) {
 105  1
                     clsInterface = interfaces[i];
 106  1
                     modifiers |= ClassRef.COMPOUND_CLASS;
 107  
                 }
 108  
             }
 109  10
             return new LocalClassRef(this, modifiers, cls, clsInterface);
 110  
         }
 111  0
         catch (Exception ex) {
 112  0
             throw new RuntimeException(ex);
 113  
         }
 114  
     }
 115  
 
 116  
     public SequenceBuilder makeSequenceBuilder (TypeRef elementType) {
 117  0
         return new SequenceBuilder() {
 118  0
             public void append(ValueRef value) { throw new Error(); }
 119  
 
 120  0
             public ValueRef getSequence() { throw new Error(); }
 121  
         };
 122  
     }
 123  
 
 124  0
     public TypeRef getIntegerType() { throw new Error(); }
 125  
 
 126  0
     public TypeRef getNumberType() { throw new Error(); }
 127  
 }
 128  
 
 129  0
 class LocalClassRef extends ClassRef {
 130  
     Class refClass;
 131  
     Class refInterface;
 132  
 
 133  
     public LocalClassRef(LocalReflectionContext context, int modifiers,
 134  
             Class refClass, Class refInterface) {
 135  22
         super(context, modifiers);
 136  22
         this.refClass = refClass;
 137  22
         this.refInterface = refInterface;
 138  22
         this.name = refClass.getCanonicalName();
 139  22
     }
 140  
 
 141  
     @Override
 142  
     public LocalReflectionContext getReflectionContect() {
 143  18
         return (LocalReflectionContext) super.getReflectionContect();
 144  
     }
 145  
 
 146  
     /** Returns a hash-code.
 147  
      * @return the hash-code of the name.
 148  
      */
 149  
     public int hashCode() {
 150  0
         return name.hashCode();
 151  
     }
 152  
     
 153  
     public boolean equals (Object obj) {
 154  0
         return obj instanceof LocalClassRef
 155  
             && refClass == ((LocalClassRef) obj).refClass;
 156  
     }
 157  
     
 158  23
     static class SortedClassArray extends AbstractList<ClassRef> {
 159  4
         LocalClassRef[] buffer = new LocalClassRef[4];
 160  
         int sz;
 161  
         public ClassRef get(int index) {
 162  19
             if (index >= sz)
 163  0
                 throw new IndexOutOfBoundsException();
 164  19
             return buffer[index];
 165  
         }
 166  23
         public int size() { return sz; }
 167  
         // This is basically 'add' under a different non-public name.
 168  
         boolean insert(LocalClassRef cl) {
 169  20
             String clname = cl.getName();
 170  
             // We could use binary search, but the lack of a total order
 171  
             // for ClassLoaders complicates that.  Linear search should be ok.
 172  20
             int i = 0;
 173  108
             for (; i < sz; i++) {
 174  56
                 LocalClassRef c = buffer[i];
 175  56
                 int cmp = c.getName().compareTo(clname);
 176  56
                 if (cmp > 0)
 177  11
                     break;
 178  45
                 if (cmp == 0) {
 179  1
                     if (c.refClass == cl.refClass)
 180  1
                         return false;
 181  
                     // Arbitrary order if same name but different loaders.
 182  
                     break;
 183  
                 }
 184  
             }
 185  19
             if (sz == buffer.length) {
 186  3
                 LocalClassRef[] tmp = new LocalClassRef[2*sz];
 187  3
                 System.arraycopy(buffer, 0, tmp, 0, sz);
 188  3
                 buffer = tmp;
 189  
             }
 190  19
             System.arraycopy(buffer, i, buffer, i+1, sz-i);
 191  19
             buffer[i] = cl;
 192  19
             sz++;
 193  19
             return true;
 194  
         }
 195  
     }
 196  
 
 197  
     void getSuperClasses(boolean all, SortedClassArray result) {
 198  18
         boolean isCompound = isCompoundClass();
 199  18
         Class cls = isCompound ? refInterface : refClass;
 200  18
         Class[] interfaces = cls.getInterfaces();
 201  18
         LocalReflectionContext context = getReflectionContect();
 202  18
         if (! isCompound) {
 203  8
             Class s = cls.getSuperclass();
 204  8
             if (s != null) {
 205  3
                 LocalClassRef cl = (LocalClassRef) context.makeClassRef(s);
 206  3
                 if (result.insert(cl) && all)
 207  2
                     cl.getSuperClasses(all, result);
 208  
             }
 209  
         }
 210  45
         for (int i = 0;  i < interfaces.length;  i++) {
 211  27
             Class iface = interfaces[i];
 212  27
             if (iface.getName().equals(LocalReflectionContext.FXOBJECT_NAME))
 213  12
                 continue;
 214  15
             LocalClassRef cl = (LocalClassRef) context.makeClassRef(iface);
 215  15
             if (result.insert(cl) && all)
 216  12
                 cl.getSuperClasses(all, result);
 217  
         }
 218  18
     }
 219  
 
 220  
     public List<ClassRef> getSuperClasses(boolean all) {
 221  4
         SortedClassArray result = new SortedClassArray();
 222  4
         if (all)
 223  2
             result.insert(this);
 224  4
         getSuperClasses(all, result);
 225  4
         return result;
 226  
     }
 227  
 
 228  0
     public MemberRef getMember(String name, TypeRef type) { throw new Error(); }
 229  0
     public AttributeRef getAttribute(String name) { throw new Error(); }
 230  0
     public MethodRef getMethod(String name, TypeRef... argType) { throw new Error(); }
 231  0
     public ObjectRef allocate () { throw new Error(); }
 232  
 
 233  0
     public void getMembers(MemberHandler handler, boolean all) { throw new Error(); }
 234  
   //public void setAttribute(AttributeRef field, ValueRef value) { throw new Error(); }
 235  
   //public void initAttribute(AttributeRef field, ValueRef value) { throw new Error(); }
 236  
 }
 237  
 
 238  0
 class LocalObjectRef extends ObjectRef {
 239  
     Object obj;
 240  
     ClassRef type;
 241  
     
 242  1
     public LocalObjectRef(Object obj, LocalReflectionContext context) {
 243  1
         type = context.makeClassRef(obj.getClass());
 244  1
         this.obj = obj;
 245  1
     }
 246  
     
 247  
     public ClassRef getType() {
 248  1
         return type;
 249  
     }
 250  
     
 251  
     public boolean isNull() {
 252  0
         return obj == null;
 253  
     }
 254  
 }