Example #1
0
  private ClassType defineClasses(
      Collection<OutputFileObject> classes,
      EvaluationContext context,
      DebugProcess process,
      ClassLoaderReference classLoader)
      throws EvaluateException, InvalidTypeException, ClassNotLoadedException {

    VirtualMachineProxyImpl proxy = (VirtualMachineProxyImpl) process.getVirtualMachineProxy();
    for (OutputFileObject cls : classes) {
      if (cls.getName().contains(GEN_CLASS_NAME)) {
        Method defineMethod =
            ((ClassType) classLoader.referenceType())
                .concreteMethodByName("defineClass", "(Ljava/lang/String;[BII)Ljava/lang/Class;");
        byte[] bytes = changeSuperToMagicAccessor(cls.toByteArray());
        ArrayList<Value> args = new ArrayList<Value>();
        StringReference name = proxy.mirrorOf(cls.myOrigName);
        keep(name, context);
        args.add(name);
        args.add(mirrorOf(bytes, context, process));
        args.add(proxy.mirrorOf(0));
        args.add(proxy.mirrorOf(bytes.length));
        process.invokeMethod(context, classLoader, defineMethod, args);
      }
    }
    return (ClassType) process.findClass(context, getGenClassQName(), classLoader);
  }
Example #2
0
  @Override
  public Value evaluate(final EvaluationContext evaluationContext) throws EvaluateException {
    DebugProcess process = evaluationContext.getDebugProcess();

    ClassLoaderReference classLoader;
    try {
      classLoader = getClassLoader(evaluationContext, process);
    } catch (Exception e) {
      throw new EvaluateException("Error creating evaluation class loader: " + e, e);
    }

    String version = ((VirtualMachineProxyImpl) process.getVirtualMachineProxy()).version();
    JavaSdkVersion sdkVersion = JdkVersionUtil.getVersion(version);
    Collection<OutputFileObject> classes =
        compile(sdkVersion != null ? sdkVersion.getDescription() : null);

    try {
      defineClasses(classes, evaluationContext, process, classLoader);
    } catch (Exception e) {
      throw new EvaluateException("Error during classes definition " + e, e);
    }

    try {
      // invoke base evaluator on call code
      final Project project =
          ApplicationManager.getApplication()
              .runReadAction(
                  new Computable<Project>() {
                    @Override
                    public Project compute() {
                      return myPsiContext.getProject();
                    }
                  });
      ExpressionEvaluator evaluator =
          DebuggerInvocationUtil.commitAndRunReadAction(
              project,
              new EvaluatingComputable<ExpressionEvaluator>() {
                @Override
                public ExpressionEvaluator compute() throws EvaluateException {
                  final TextWithImports callCode = getCallCode();
                  PsiElement copyContext = myData.getAnchor();
                  final CodeFragmentFactory factory =
                      DebuggerUtilsEx.findAppropriateCodeFragmentFactory(callCode, copyContext);
                  return factory
                      .getEvaluatorBuilder()
                      .build(
                          factory.createCodeFragment(callCode, copyContext, project),
                          ContextUtil.getSourcePosition(evaluationContext));
                }
              });
      ((EvaluationContextImpl) evaluationContext).setClassLoader(classLoader);
      return evaluator.evaluate(evaluationContext);
    } catch (Exception e) {
      throw new EvaluateException("Error during generated code invocation " + e, e);
    }
  }
 public static ArrayReference mirrorOf(
     byte[] bytes, EvaluationContext context, DebugProcess process)
     throws EvaluateException, InvalidTypeException, ClassNotLoadedException {
   ArrayType arrayClass =
       (ArrayType) process.findClass(context, "byte[]", context.getClassLoader());
   ArrayReference reference = process.newInstance(arrayClass, bytes.length);
   keep(reference, context);
   for (int i = 0; i < bytes.length; i++) {
     reference.setValue(
         i, ((VirtualMachineProxyImpl) process.getVirtualMachineProxy()).mirrorOf(bytes[i]));
   }
   return reference;
 }
 public static ClassLoaderReference getClassLoader(EvaluationContext context, DebugProcess process)
     throws EvaluateException, InvocationException, InvalidTypeException, ClassNotLoadedException,
         IncompatibleThreadStateException {
   // TODO: cache
   ClassType loaderClass =
       (ClassType) process.findClass(context, "java.net.URLClassLoader", context.getClassLoader());
   Method ctorMethod =
       loaderClass.concreteMethodByName("<init>", "([Ljava/net/URL;Ljava/lang/ClassLoader;)V");
   ClassLoaderReference reference =
       (ClassLoaderReference)
           process.newInstance(
               context,
               loaderClass,
               ctorMethod,
               Arrays.asList(createURLArray(context), context.getClassLoader()));
   keep(reference, context);
   return reference;
 }
  public static void defineClass(
      @NotNull String className,
      byte[] bytecodes,
      @NotNull EvaluationContext context,
      @NotNull DebugProcess process,
      @NotNull ClassLoaderReference classLoader)
      throws ClassNotLoadedException, EvaluateException, InvalidTypeException {
    VirtualMachineProxyImpl proxy = (VirtualMachineProxyImpl) process.getVirtualMachineProxy();

    Method defineMethod =
        ((ClassType) classLoader.referenceType())
            .concreteMethodByName("defineClass", "(Ljava/lang/String;[BII)Ljava/lang/Class;");
    byte[] bytes = changeSuperToMagicAccessor(bytecodes);
    ArrayList<Value> args = new ArrayList<Value>();
    StringReference name = proxy.mirrorOf(className);
    keep(name, context);
    args.add(name);
    args.add(mirrorOf(bytes, context, process));
    args.add(proxy.mirrorOf(0));
    args.add(proxy.mirrorOf(bytes.length));
    process.invokeMethod(context, classLoader, defineMethod, args);
  }
 private static ArrayReference createURLArray(EvaluationContext context)
     throws EvaluateException, InvocationException, InvalidTypeException, ClassNotLoadedException,
         IncompatibleThreadStateException {
   DebugProcess process = context.getDebugProcess();
   ArrayType arrayType =
       (ArrayType) process.findClass(context, "java.net.URL[]", context.getClassLoader());
   ArrayReference arrayRef = arrayType.newInstance(1);
   keep(arrayRef, context);
   ClassType classType =
       (ClassType) process.findClass(context, "java.net.URL", context.getClassLoader());
   VirtualMachineProxyImpl proxy = (VirtualMachineProxyImpl) process.getVirtualMachineProxy();
   StringReference url = proxy.mirrorOf("file:a");
   keep(url, context);
   ObjectReference reference =
       process.newInstance(
           context,
           classType,
           classType.concreteMethodByName("<init>", "(Ljava/lang/String;)V"),
           Collections.singletonList(url));
   keep(reference, context);
   arrayRef.setValues(Collections.singletonList(reference));
   return arrayRef;
 }
 @Override
 public boolean isApplicable(final SuspendContext context) {
   try {
     if ((Objects.equal(context, null) || (!this.isXtextSourced(context)))) {
       return false;
     }
     final DebugProcess debugProcess = context.getDebugProcess();
     final PositionManager positionManager = debugProcess.getPositionManager();
     StackFrameProxy _frameProxy = context.getFrameProxy();
     final Location location = _frameProxy.location();
     boolean _isEmptyAnonymousClassConstructor = this.isEmptyAnonymousClassConstructor(context);
     if (_isEmptyAnonymousClassConstructor) {
       return true;
     }
     SourcePosition _sourcePosition = positionManager.getSourcePosition(location);
     final boolean result = Objects.equal(_sourcePosition, null);
     if (result) {
       return true;
     }
     return false;
   } catch (Throwable _e) {
     throw Exceptions.sneakyThrow(_e);
   }
 }