001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.debugger.jpda.models;
043:
044: import com.sun.jdi.AbsentInformationException;
045: import com.sun.jdi.ClassType;
046: import com.sun.jdi.InterfaceType;
047: import com.sun.jdi.ObjectReference;
048: import com.sun.jdi.PrimitiveValue;
049: import com.sun.jdi.ReferenceType;
050: import com.sun.jdi.Value;
051:
052: import java.util.AbstractList;
053: import java.util.ArrayList;
054: import java.util.Collections;
055: import java.util.List;
056: import java.util.logging.Level;
057: import java.util.logging.Logger;
058:
059: import org.netbeans.api.debugger.jpda.ClassVariable;
060: import org.netbeans.api.debugger.jpda.Field;
061: import org.netbeans.api.debugger.jpda.JPDAClassType;
062: import org.netbeans.api.debugger.jpda.ObjectVariable;
063: import org.netbeans.modules.debugger.jpda.JPDADebuggerImpl;
064: import org.netbeans.modules.debugger.jpda.Java6Methods;
065:
066: /**
067: *
068: * @author Martin Entlicher
069: */
070: public class JPDAClassTypeImpl implements JPDAClassType {
071:
072: private static final Logger loggerValue = Logger
073: .getLogger("org.netbeans.modules.debugger.jpda.getValue"); // NOI8N
074:
075: private JPDADebuggerImpl debugger;
076: private ReferenceType classType;
077:
078: // private long cachedInstanceCount = -1L;
079:
080: /**
081: * Creates a new instance of JPDAClassTypeImpl
082: */
083: public JPDAClassTypeImpl(JPDADebuggerImpl debugger,
084: ReferenceType classType) {
085: this .debugger = debugger;
086: this .classType = classType;
087: }
088:
089: public ReferenceType getType() {
090: return classType;
091: }
092:
093: public String getName() {
094: return classType.name();
095: }
096:
097: public String getSourceName() throws AbsentInformationException {
098: return classType.sourceName();
099: }
100:
101: public ClassVariable classObject() {
102: return new ClassVariableImpl(debugger, classType.classObject(),
103: "");
104: }
105:
106: public ObjectVariable getClassLoader() {
107: return new AbstractObjectVariable(debugger, classType
108: .classLoader(), "Loader " + getName());
109: }
110:
111: public SuperVariable getSuperClass() {
112: if (classType instanceof ClassType) {
113: return new SuperVariable(debugger, null,
114: ((ClassType) classType).super class(), getName());
115: } else {
116: return null;
117: }
118: }
119:
120: public List<JPDAClassType> getSubClasses() {
121: if (classType instanceof ClassType) {
122: List<ClassType> subclasses = ((ClassType) classType)
123: .subclasses();
124: if (subclasses.size() > 0) {
125: List<JPDAClassType> subClasses = new ArrayList(
126: subclasses.size());
127: for (ClassType subclass : subclasses) {
128: subClasses.add(new JPDAClassTypeImpl(debugger,
129: subclass));
130: }
131: return Collections.unmodifiableList(subClasses);
132: }
133: }
134: if (classType instanceof InterfaceType) {
135: List<InterfaceType> subinterfaces = ((InterfaceType) classType)
136: .subinterfaces();
137: List<ClassType> implementors = ((InterfaceType) classType)
138: .implementors();
139: int ss = subinterfaces.size();
140: int is = implementors.size();
141: if (ss > 0 || is > 0) {
142: List<JPDAClassType> subClasses = new ArrayList(ss + is);
143: for (InterfaceType subclass : subinterfaces) {
144: subClasses.add(new JPDAClassTypeImpl(debugger,
145: subclass));
146: }
147: for (ClassType subclass : implementors) {
148: subClasses.add(new JPDAClassTypeImpl(debugger,
149: subclass));
150: }
151: return Collections.unmodifiableList(subClasses);
152: }
153: }
154: return Collections.EMPTY_LIST;
155: }
156:
157: public List<Field> staticFields() {
158: List<com.sun.jdi.Field> allFieldsOrig = classType.allFields();
159: List<Field> staticFields = new ArrayList<Field>();
160: for (int i = 0; i < allFieldsOrig.size(); i++) {
161: Value value = null;
162: com.sun.jdi.Field origField = allFieldsOrig.get(i);
163: if (origField.isStatic()) {
164: if (loggerValue.isLoggable(Level.FINE)) {
165: loggerValue.fine("STARTED : " + classType
166: + ".getValue(" + origField + ")");
167: }
168: value = classType.getValue(origField);
169: if (loggerValue.isLoggable(Level.FINE)) {
170: loggerValue
171: .fine("FINISHED: " + classType
172: + ".getValue(" + origField + ") = "
173: + value);
174: }
175: if (value instanceof PrimitiveValue) {
176: staticFields.add(new FieldVariable(debugger,
177: (PrimitiveValue) value, origField, "",
178: (ObjectReference) null));
179: } else {
180: staticFields.add(new ObjectFieldVariable(debugger,
181: (ObjectReference) value, origField, "",
182: (ObjectReference) null));
183: }
184: }
185: }
186: return staticFields;
187: }
188:
189: public long getInstanceCount() {//boolean refresh) {
190: if (Java6Methods.isJDK6()) {
191: /*synchronized (this) {
192: if (!refresh && cachedInstanceCount > -1L) {
193: return cachedInstanceCount;
194: }
195: }*/
196: long[] counts = Java6Methods.instanceCounts(classType
197: .virtualMachine(), Collections
198: .singletonList(classType));
199: /*synchronized (this) {
200: cachedInstanceCount = counts[0];
201: }*/
202: return counts[0];
203: } else {
204: return 0L;
205: }
206: }
207:
208: public List<ObjectVariable> getInstances(long maxInstances) {
209: if (Java6Methods.isJDK6()) {
210: final List<ObjectReference> instances = Java6Methods
211: .instances(classType, maxInstances);
212: return new AbstractList<ObjectVariable>() {
213: public ObjectVariable get(int i) {
214: ObjectReference obj = instances.get(i);
215: return new AbstractObjectVariable(debugger, obj,
216: classType.name() + " instance " + i);
217: }
218:
219: public int size() {
220: return instances.size();
221: }
222: };
223: } else {
224: return Collections.emptyList();
225: }
226: }
227:
228: public boolean equals(Object o) {
229: if (!(o instanceof JPDAClassTypeImpl)) {
230: return false;
231: }
232: return classType.equals(((JPDAClassTypeImpl) o).classType);
233: }
234:
235: public int hashCode() {
236: return classType.hashCode() + 1000;
237: }
238: }
|