public static ReferenceType adjustBoxingType(ReferenceType type, PrimitiveType primitiveType, EvaluationContext evaluationContext) { Class typeClass = null; if (primitiveType instanceof BooleanType) { typeClass = Boolean.class; } else if (primitiveType instanceof ByteType) { typeClass = Byte.class; } else if (primitiveType instanceof CharType) { typeClass = Character.class; } else if (primitiveType instanceof ShortType) { typeClass = Short.class; } else if (primitiveType instanceof IntegerType) { typeClass = Integer.class; } else if (primitiveType instanceof LongType) { typeClass = Long.class; } else if (primitiveType instanceof FloatType) { typeClass = Float.class; } else if (primitiveType instanceof DoubleType) { typeClass = Double.class; } if (typeClass != null) { type = evaluationContext.getVMCache().getClass(typeClass.getName()); } return type; }
ExceptionRequestImpl(ReferenceType refType, boolean notifyCaught, boolean notifyUncaught) { exception = refType; caught = notifyCaught; uncaught = notifyUncaught; { ReferenceTypeImpl exc; if (exception == null) { exc = new ClassTypeImpl(vm, 0); } else { exc = (ReferenceTypeImpl)exception; } filters.add(JDWP.EventRequest.Set.Modifier.ExceptionOnly. create(exc, caught, uncaught)); } requestList().add(this); }
public String genericSignature() { // This gets both the signature and the generic signature if (vm.canGet1_5LanguageFeatures() && !genericSignatureGotten) { // Does not need synchronization, since worst-case // static info is fetched twice JDWP.ReferenceType.SignatureWithGeneric result; try { result = JDWP.ReferenceType.SignatureWithGeneric. process(vm, this); } catch (JDWPException exc) { throw exc.toJDIException(); } signature = result.signature; setGenericSignature(result.genericSignature); } return genericSignature; }
static ArrayType getArrayClass(VirtualMachine vm, String name) throws InternalExceptionWrapper, VMDisconnectedExceptionWrapper, ObjectCollectedExceptionWrapper { List<ReferenceType> classList = VirtualMachineWrapper.classesByName(vm, name); ReferenceType clazz = null; for (ReferenceType c : classList) { if (ReferenceTypeWrapper.classLoader(c) == null) { clazz = c; break; } } return (ArrayType) clazz; }
boolean isAssignableTo(ReferenceType destType) { if (destType instanceof ArrayType) { try { Type destComponentType = ((ArrayType)destType).componentType(); return isComponentAssignable(destComponentType, componentType()); } catch (ClassNotLoadedException e) { // One or both component types has not yet been // loaded => can't assign return false; } } else if (destType instanceof InterfaceType) { // Only valid InterfaceType assignee is Cloneable return destType.name().equals("java.lang.Cloneable"); } else { // Only valid ClassType assignee is Object return destType.name().equals("java.lang.Object"); } }
private String selectListenerClass(JavaComponentInfo ci) { List<ReferenceType> attachableListeners = RemoteServices.getAttachableListeners(ci); //System.err.println("Attachable Listeners = "+attachableListeners); String[] listData = new String[attachableListeners.size()]; for (int i = 0; i < listData.length; i++) { listData[i] = attachableListeners.get(i).name(); } JList jl = new JList(listData); JScrollPane jsp = new JScrollPane(jl); NotifyDescriptor nd = new DialogDescriptor(jsp, NbBundle.getMessage(EventsModel.class, "TTL_SelectListener"), true, null); Object res = DialogDisplayer.getDefault().notify(nd); if (DialogDescriptor.OK_OPTION.equals(res)) { String clazz = (String) jl.getSelectedValue(); return clazz; } else { return null; } }
/** * Creates a new instance of JPDABreakpointEvent. This method should be * called from debuggerjpda module only. Do not create a new instances * of this class! * * @param sourceBreakpoint a breakpoint * @param debugger a debugger this * @param conditionResult a result of condition * @param thread a context thread * @param referenceType a context class * @param variable a context variable */ public JPDABreakpointEvent ( JPDABreakpoint sourceBreakpoint, JPDADebugger debugger, int conditionResult, JPDAThread thread, ReferenceType referenceType, Variable variable ) { super (sourceBreakpoint); this.conditionResult = conditionResult; this.thread = thread; this.debugger = debugger; this.referenceType = referenceType; this.variable = variable; }
private static ReferenceType getOrLoadClass(VirtualMachine vm, String name) { List<ReferenceType> types = vm.classesByName(name); if (types.size() > 0) { if (types.size() == 1) { return types.get(0); } try { ReferenceType preferedType = JPDAUtils.getPreferredReferenceType(types, null); if (preferedType != null) { return preferedType; } } catch (VMDisconnectedExceptionWrapper ex) { throw ex.getCause(); } // No preferred, just take the first one: return types.get(0); } // DO NOT TRY TO LOAD CLASSES AT ALL! See http://www.netbeans.org/issues/show_bug.cgi?id=168949 return null; }
private List<Method> methods1_4() { List<Method> methods; JDWP.ReferenceType.Methods.MethodInfo[] declared; try { declared = JDWP.ReferenceType.Methods. process(vm, this).declared; } catch (JDWPException exc) { throw exc.toJDIException(); } methods = new ArrayList<Method>(declared.length); for (int i=0; i<declared.length; i++) { JDWP.ReferenceType.Methods.MethodInfo mi = declared[i]; Method method = MethodImpl.createMethodImpl(vm, this, mi.methodID, mi.name, mi.signature, null, mi.modBits); methods.add(method); } return methods; }
@Override public boolean isInstanceOf(String className) { List<ReferenceType> classTypes; try { classTypes = VirtualMachineWrapper.classesByName(MirrorWrapper.virtualMachine(classType), className); } catch (InternalExceptionWrapper | VMDisconnectedExceptionWrapper ex) { return false; } for (ReferenceType rt : classTypes) { try { if (EvaluatorVisitor.instanceOf(classType, rt)) { return true; } } catch (VMDisconnectedException vmdex) { return false; } catch (InternalException iex) { // procceed } } return false; }
/** * Test whether the method is considered to be synthetic * @param m The method * @param loc The current location in that method * @return 0 when not synthetic * positive when suggested step depth is returned * negative when is synthetic and no further step depth is suggested. */ public static int isSyntheticMethod(Method m, Location loc) throws InternalExceptionWrapper, VMDisconnectedExceptionWrapper { String name = TypeComponentWrapper.name(m); if (name.startsWith("lambda$")) { // NOI18N int lineNumber = LocationWrapper.lineNumber(loc); if (lineNumber == 1) { // We're in the initialization of the Lambda. We need to step over it. return StepRequest.STEP_OVER; } return 0; // Do not treat Lambda methods as synthetic, because they contain user code. } else { // Do check the class for being Lambda synthetic class: ReferenceType declaringType = LocationWrapper.declaringType(loc); try { String className = ReferenceTypeWrapper.name(declaringType); if (className.contains("$$Lambda$")) { // NOI18N // Lambda synthetic class return -1; } } catch (ObjectCollectedExceptionWrapper ex) { } } return TypeComponentWrapper.isSynthetic(m) ? -1 : 0; }
public String signature() { if (signature == null) { // Does not need synchronization, since worst-case // static info is fetched twice if (vm.canGet1_5LanguageFeatures()) { /* * we might as well get both the signature and the * generic signature. */ genericSignature(); } else { try { signature = JDWP.ReferenceType.Signature. process(vm, this).signature; } catch (JDWPException exc) { throw exc.toJDIException(); } } } return signature; }
private static Method getConcreteMethod(ReferenceType type, String methodName, String firstParamSignature, List<? extends TypeMirror> typeArguments) throws UnsuitableArgumentsException { List<Method> methods = type.methodsByName(methodName); String signature = createSignature(firstParamSignature, typeArguments); boolean constructor = "<init>".equals(methodName); for (Method method : methods) { if (!method.isAbstract() && (!constructor || type.equals(method.declaringType())) && equalMethodSignatures(method.signature(), signature)) { return method; } } if (methods.size() > 0) { throw new UnsuitableArgumentsException(); } return null; }
@Override boolean isAssignableTo(ReferenceType type) { ClassTypeImpl superclazz = (ClassTypeImpl) superclass(); if (this.equals(type)) { return true; } else if ((superclazz != null) && superclazz.isAssignableTo(type)) { return true; } else { List<InterfaceType> interfaces = interfaces(); Iterator<InterfaceType> iter = interfaces.iterator(); while (iter.hasNext()) { InterfaceTypeImpl interfaze = (InterfaceTypeImpl) iter.next(); if (interfaze.isAssignableTo(type)) { return true; } } return false; } }
private ReferenceType findEnclosingType(ReferenceType type, String name, VMCache cache) { if (type.name().equals(name)) { return type; } ReferenceType enclosingType; enclosingType = cache.getEnclosingType(type, name); if (enclosingType == null) { List<ReferenceType> classes = type.virtualMachine().classesByName(name); if (classes.size() == 1) { enclosingType = classes.get(0); } else { for (ReferenceType clazz : classes) { if (isNestedOf(clazz, type)) { enclosingType = clazz; break; } } } if (enclosingType != null) { cache.setEnclosingType(type, name, enclosingType); } } long end = System.nanoTime(); return enclosingType; }
public ClassObjectReference classObject() { if (classObject == null) { // Are classObjects unique for an Object, or // created each time? Is this spec'ed? synchronized(this) { if (classObject == null) { try { classObject = JDWP.ReferenceType.ClassObject. process(vm, this).classObject; } catch (JDWPException exc) { throw exc.toJDIException(); } } } } return classObject; }
private ArrayType getArrayType(NewArrayTree arg0, Type type, int depth, EvaluationContext evaluationContext) { String arrayClassName; if (depth < BRACKETS.length()/2) { arrayClassName = type.name() + BRACKETS.substring(0, 2*depth); } else { arrayClassName = type.name() + BRACKETS; for (int i = BRACKETS.length()/2; i < depth; i++) { arrayClassName += "[]"; // NOI18N } } ReferenceType rt = getOrLoadClass(type.virtualMachine(), arrayClassName, evaluationContext); if (rt == null) { Assert.error(arg0, "unknownType", arrayClassName); } return (ArrayType) rt; }
@Override public Mirror visitArrayType(ArrayTypeTree arg0, EvaluationContext evaluationContext) { Mirror arrayType = arg0.getType().accept(this, evaluationContext); if (!(arrayType instanceof Type)) { return arrayType; } Type type = (Type) arrayType; String arrayClassName = type.name()+"[]"; ReferenceType aType = getOrLoadClass(type.virtualMachine(), arrayClassName, evaluationContext); if (aType != null) { return aType; } else { Assert.error(arg0, "unknownType", arrayClassName); return null; } }
void addAllFields(List<Field> fieldList, Set<ReferenceType> typeSet) { /* Continue the recursion only if this type is new */ if (!typeSet.contains(this)) { typeSet.add(this); /* Add local fields */ fieldList.addAll(fields()); /* Add inherited fields */ List<? extends ReferenceType> types = inheritedTypes(); Iterator<? extends ReferenceType> iter = types.iterator(); while (iter.hasNext()) { ReferenceTypeImpl type = (ReferenceTypeImpl)iter.next(); type.addAllFields(fieldList, typeSet); } } }
@Override public Value getValue(Field field) { String name = field.name(); for (ReferenceType t : types) { if (field.equals(t.fieldByName(name))) { return t.getValue(field); } } return types[0].getValue(field); // Likely throws some appropriate error. }
public void stop(ObjectReference throwable) throws InvalidTypeException { validateMirrorOrNull(throwable); // Verify that the given object is a Throwable instance List<ReferenceType> list = vm.classesByName("java.lang.Throwable"); ClassTypeImpl throwableClass = (ClassTypeImpl)list.get(0); if ((throwable == null) || !throwableClass.isAssignableFrom(throwable)) { throw new InvalidTypeException("Not an instance of Throwable"); } try { JDWP.ThreadReference.Stop.process(vm, this, (ObjectReferenceImpl)throwable); } catch (JDWPException exc) { throw exc.toJDIException(); } }
private static void addBreakpoint(VirtualMachine vm, ReferenceType refType) { Location breakpointLocation = null; List<Location> locs; try { locs = refType.allLineLocations(); for (Location loc: locs) { if (loc.method().name().equals(METHOD_NAME)) { breakpointLocation = loc; break; } } } catch (AbsentInformationException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (breakpointLocation != null) { EventRequestManager evtReqMgr = vm.eventRequestManager(); BreakpointRequest bReq = evtReqMgr.createBreakpointRequest(breakpointLocation); bReq.setSuspendPolicy(BreakpointRequest.SUSPEND_ALL); bReq.enable(); } }
Type findComponentType(String signature) throws ClassNotLoadedException { byte tag = (byte)signature.charAt(0); if (PacketStream.isObjectTag(tag)) { // It's a reference type JNITypeParser parser = new JNITypeParser(componentSignature()); List<ReferenceType> list = vm.classesByName(parser.typeName()); Iterator<ReferenceType> iter = list.iterator(); while (iter.hasNext()) { ReferenceType type = iter.next(); ClassLoaderReference cl = type.classLoader(); if ((cl == null)? (classLoader() == null) : (cl.equals(classLoader()))) { return type; } } // Component class has not yet been loaded throw new ClassNotLoadedException(componentTypeName()); } else { // It's a primitive type return vm.primitiveTypeMirror(tag); } }
/** * Get the static variable of an stack frame. * * @param stackFrame * the stack frame * @return the static variable of an stack frame. */ public static List<Variable> listStaticVariables(StackFrame stackFrame) { List<Variable> res = new ArrayList<>(); ReferenceType type = stackFrame.location().declaringType(); type.allFields().stream().filter(TypeComponent::isStatic).forEach(field -> { Variable staticVar = new Variable(field.name(), type.getValue(field)); staticVar.field = field; res.add(staticVar); }); return res; }
private boolean ungrabWindowFX_OLD(ThreadReference tr) { // com.sun.javafx.tk.quantum.WindowStage has: // field static Map<Window, WindowStage> platformWindows = new HashMap<>(); // com.sun.glass.ui.Window.ungrabFocus() try { VirtualMachine vm = MirrorWrapper.virtualMachine(tr); List<ReferenceType> windowStageClassesByName = VirtualMachineWrapper.classesByName(vm, "com.sun.javafx.tk.quantum.WindowStage"); if (windowStageClassesByName.isEmpty()) { logger.info("Unable to release FX X grab, no quantum WindowStage class in target VM "+VirtualMachineWrapper.description(vm)); return false; } ClassType WindowStageClass = (ClassType) windowStageClassesByName.get(0); Field platformWindowsField = WindowStageClass.fieldByName("platformWindows"); if (platformWindowsField == null) { logger.info("Unable to release FX X grab, no platformWindows field found in WindowStage in target VM "+VirtualMachineWrapper.description(vm)); return false; } ObjectReference platformWindows = (ObjectReference) WindowStageClass.getValue(platformWindowsField); if (platformWindows == null) { logger.info("Unable to release FX X grab, no platformWindows field has null value in WindowStage in target VM "+VirtualMachineWrapper.description(vm)); return false; } } catch (VMDisconnectedExceptionWrapper vmdex) { return true; // Disconnected, all is good. } catch (Exception ex) { logger.log(Level.INFO, "Unable to release FX X grab (if any).", ex); return true; // We do not know whether there was any grab } return true; }
@Override public boolean initChooserUI(JPDADebuggerImpl debugger, String url, ReferenceType clazz, int methodLine) { final MethodChooserSupport cSupport = new MethodChooserSupport(debugger, url, clazz, methodLine); boolean continuedDirectly = cSupport.init(); if (cSupport.getSegmentsCount() == 0) { return false; } if (continuedDirectly) { return true; } MethodChooser.ReleaseListener releaseListener = new MethodChooser.ReleaseListener() { @Override public void released(boolean performAction) { synchronized (JPDAMethodChooserFactoryUIImpl.this) { currentMethodChooser = null; cSupport.tearDown(); if (performAction) { cSupport.doStepInto(); } } } }; MethodChooser chooser = cSupport.createChooser(); chooser.addReleaseListener(releaseListener); boolean success = chooser.showUI(); if (success && chooser.isUIActive()) { synchronized (this) { cSupport.tearUp(chooser); currentMethodChooser = chooser; } } else { chooser.removeReleaseListener(releaseListener); } return success; }
private static ClassType getClass(VirtualMachine vm, ThreadReference tr, String name) { ReferenceType t = getType(vm, tr, name); if (t instanceof ClassType) { return (ClassType)t; } logger.log(Level.WARNING, "{0} is not a class but {1}", new Object[]{name, t}); // NOI18N return null; }
private static InterfaceType getInterface(VirtualMachine vm, ThreadReference tr, String name) { ReferenceType t = getType(vm, tr, name); if (t instanceof InterfaceType) { return (InterfaceType)t; } logger.log(Level.WARNING, "{0} is not an interface but {1}", new Object[]{name, t}); // NOI18N return null; }
private static ReferenceType getType(VirtualMachine vm, ThreadReference tr, String name) { List<ReferenceType> classList = VirtualMachineWrapper.classesByName0(vm, name); if (!classList.isEmpty()) { return classList.iterator().next(); } List<ReferenceType> classClassList = VirtualMachineWrapper.classesByName0(vm, "java.lang.Class"); // NOI18N if (classClassList.isEmpty()) { throw new IllegalStateException("Cannot load class Class"); // NOI18N } ClassType cls = (ClassType) classClassList.iterator().next(); try { Method m = ClassTypeWrapper.concreteMethodByName(cls, "forName", "(Ljava/lang/String;)Ljava/lang/Class;"); // NOI18N StringReference mirrorOfName = VirtualMachineWrapper.mirrorOf(vm, name); ClassTypeWrapper.invokeMethod(cls, tr, m, Collections.singletonList(mirrorOfName), ObjectReference.INVOKE_SINGLE_THREADED); List<ReferenceType> classList2 = VirtualMachineWrapper.classesByName0(vm, name); if (!classList2.isEmpty()) { return classList2.iterator().next(); } } catch (ClassNotLoadedException | ClassNotPreparedExceptionWrapper | IncompatibleThreadStateException | InvalidTypeException | InvocationException | InternalExceptionWrapper | ObjectCollectedExceptionWrapper | UnsupportedOperationExceptionWrapper | VMDisconnectedExceptionWrapper ex) { logger.log(Level.FINE, "Cannot load class " + name, ex); // NOI18N } return null; }
static ClassType getClass(VirtualMachine vm, String name) throws InternalExceptionWrapper, VMDisconnectedExceptionWrapper, ObjectCollectedExceptionWrapper { List<ReferenceType> classList = VirtualMachineWrapper.classesByName(vm, name); ReferenceType clazz = null; for (ReferenceType c : classList) { if (ReferenceTypeWrapper.classLoader(c) == null) { clazz = c; break; } } if (clazz == null && classList.size() > 0) { clazz = classList.get(0); } return (ClassType) clazz; }
public List<ReferenceType> getClasses() { classes = vm.allClasses(); if (classesMap == null) classesMap = new HashMap<>(); for (ReferenceType type : classes) { if (!classesMap.containsKey(type.name())) classesMap.put(type.name(), type); } return classes; }
private Value handleSetValueForStackFrame(String name, String belongToClass, String valueString, boolean showStaticVariables, StackFrame container, Map<String, Object> options) throws AbsentInformationException, InvalidTypeException, ClassNotLoadedException { Value newValue; if (name.equals("this")) { throw new UnsupportedOperationException("SetVariableRequest: 'This' variable cannot be changed."); } LocalVariable variable = container.visibleVariableByName(name); if (StringUtils.isBlank(belongToClass) && variable != null) { newValue = this.setFrameValue(container, variable, valueString, options); } else { if (showStaticVariables && container.location().method().isStatic()) { ReferenceType type = container.location().declaringType(); if (StringUtils.isBlank(belongToClass)) { Field field = type.fieldByName(name); newValue = setStaticFieldValue(type, field, name, valueString, options); } else { newValue = setFieldValueWithConflict(null, type.allFields(), name, belongToClass, valueString, options); } } else { throw new UnsupportedOperationException( String.format("SetVariableRequest: Variable %s cannot be found.", name)); } } return newValue; }
@Override public List<JPDAClassType> getAllClasses() { //assert !java.awt.EventQueue.isDispatchThread() : "All classes retrieving in AWT Event Queue!"; List<ReferenceType> classes; synchronized (virtualMachineLock) { if (virtualMachine == null) { classes = Collections.emptyList(); } else { classes = VirtualMachineWrapper.allClasses0(virtualMachine); } } return new ClassTypeList(this, classes); }
@Override public List<JPDAClassType> getClassesByName(String name) { List<ReferenceType> classes; synchronized (virtualMachineLock) { if (virtualMachine == null) { classes = Collections.emptyList(); } else { classes = VirtualMachineWrapper.classesByName0(virtualMachine, name); } } return new ClassTypeList(this, classes); }
private static Map<Field, Value> getFieldValues(ObjectReference value, ReferenceType type, String... names) throws InternalExceptionWrapper, VMDisconnectedExceptionWrapper, ObjectCollectedExceptionWrapper, ClassNotPreparedExceptionWrapper { final int n = names.length; List<Field> fieldsToAskFor = new ArrayList<Field>(n); for (String name : names) { Field field = ReferenceTypeWrapper.fieldByName(type, name); if (field == null) { return null; } fieldsToAskFor.add(field); } Map<Field, Value> fieldValues = ObjectReferenceWrapper.getValues(value, fieldsToAskFor); return fieldValues; }
private static void setValueToFinalField(ObjectReference obj, String name, ClassType clazz, Value fv, VirtualMachine vm, ThreadReference thread) throws InternalExceptionWrapper, VMDisconnectedExceptionWrapper, ClassNotPreparedExceptionWrapper, ClassNotLoadedException, ObjectCollectedExceptionWrapper, IncompatibleThreadStateException, UnsupportedOperationExceptionWrapper, InvalidTypeException, InvalidObjectException { ObjectReference fieldRef = getDeclaredOrInheritedField(clazz, name, vm, thread); if (fieldRef == null) { InvalidObjectException ioex = new InvalidObjectException("No field "+name+" of class "+clazz); throw ioex; } // field.setAccessible(true); ClassType fieldClassType = (ClassType) ValueWrapper.type(fieldRef); com.sun.jdi.Method setAccessibleMethod = ClassTypeWrapper.concreteMethodByName( fieldClassType, "setAccessible", "(Z)V"); try { ObjectReferenceWrapper.invokeMethod(fieldRef, thread, setAccessibleMethod, Collections.singletonList(vm.mirrorOf(true)), ClassType.INVOKE_SINGLE_THREADED); // field.set(newInstance, fv); com.sun.jdi.Method setMethod = ClassTypeWrapper.concreteMethodByName( fieldClassType, "set", "(Ljava/lang/Object;Ljava/lang/Object;)V"); if (fv instanceof PrimitiveValue) { PrimitiveType pt = (PrimitiveType) ValueWrapper.type(fv); ReferenceType fieldBoxingClass = EvaluatorVisitor.adjustBoxingType(clazz, pt, null); fv = EvaluatorVisitor.box((PrimitiveValue) fv, fieldBoxingClass, thread, null); } List<Value> args = Arrays.asList(new Value[] { obj, fv }); ObjectReferenceWrapper.invokeMethod(fieldRef, thread, setMethod, args, ClassType.INVOKE_SINGLE_THREADED); } catch (InvocationException iex) { throw new InvalidObjectException( "Problem setting value "+fv+" to field "+name+" of class "+clazz+ " : "+iex.exception()); } }
@Override boolean isAssignableTo(ReferenceType type) { if (type.name().equals("java.lang.Object")) { // interfaces are always assignable to j.l.Object return true; } return super.isAssignableTo(type); }
private List<JPDAClassType> getTypes(Collection<? extends ReferenceType> types) { if (types.size() > 0) { types = new LinkedHashSet<>(types); // To remove duplicities List<JPDAClassType> interfaces = new ArrayList(types.size()); for (ReferenceType intrfc : types) { interfaces.add(debugger.getClassType(intrfc)); } return Collections.unmodifiableList(interfaces); } else { return Collections.EMPTY_LIST; } }