简体   繁体   English

在运行时获取变量的值

[英]Get the value of variable during the runtime

I'm currently working on an Eclipse Plug-in. 我目前正在使用Eclipse插件。 The plugin should read a java class and get the value of a certain variable at runtime . 该插件应读取一个Java类并在运行时获取某个变量的值。

Example of a java class: Java类的示例:

public class MyClass {

 private int aVariable;

public int getaVariable() {
    return aVariable;
}

public void setaVariable(int aVariable) {
    this.aVariable = aVariable;
}

private int doSomethinng (){
    setaVariable(10);
    int x = getaVariable();
    return x;
}

}

Now my plugin should get the value of the variable x at RUNTIME and print it on the eclipse console(first). 现在我的插件应该在运行时获取变量x的值,并在eclipse控制台上打印(第一个)。 To read this class i created an Eclipse plugin "Hello World Commands": 要阅读此类,我创建了一个Eclipse插件“ Hello World Commands”:

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.text.Document;

public class SampleHandler extends AbstractHandler {

  public Object execute(ExecutionEvent event) throws ExecutionException {
    // Get the root of the workspace
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IWorkspaceRoot root = workspace.getRoot();
    // Get all projects in the workspace
    IProject[] projects = root.getProjects();
    // Loop over all projects
    for (IProject project : projects) {
      try {
        printProjectInfo(project);
      } catch (CoreException e) {
        e.printStackTrace();
      }
    }
    return null;
  }

  private void printProjectInfo(IProject project) throws CoreException,
      JavaModelException {
    System.out.println("Working in project " + project.getName());
    if (project.isNatureEnabled("org.eclipse.jdt.core.javanature")) {
      IJavaProject javaProject = JavaCore.create(project);
      printPackageInfos(javaProject);
    }
  }

  private void printPackageInfos(IJavaProject javaProject)
      throws JavaModelException {
    IPackageFragment[] packages = javaProject.getPackageFragments();
    for (IPackageFragment mypackage : packages) {
      if (mypackage.getKind() == IPackageFragmentRoot.K_SOURCE) {
        System.out.println("Package " + mypackage.getElementName());
        printICompilationUnitInfo(mypackage);

      }

    }
  }

  private void printICompilationUnitInfo(IPackageFragment mypackage)
      throws JavaModelException {
    for (ICompilationUnit unit : mypackage.getCompilationUnits()) {
      printCompilationUnitDetails(unit);

    }
  }

  private void printIMethods(ICompilationUnit unit) throws JavaModelException {
    IType[] allTypes = unit.getAllTypes();
    for (IType type : allTypes) {
      printIMethodDetails(type);
    }
  }

  private void printCompilationUnitDetails(ICompilationUnit unit)
      throws JavaModelException {
    System.out.println("Source file " + unit.getElementName());
    Document doc = new Document(unit.getSource());
    System.out.println("Has number of lines: " + doc.getNumberOfLines());
    printIMethods(unit);
  }

  private void printIMethodDetails(IType type) throws JavaModelException {
    IMethod[] methods = type.getMethods();
    for (IMethod method : methods) {

      System.out.println("Method name " + method.getElementName());
      System.out.println("Signature " + method.getSignature());
      System.out.println("Return Type " + method.getReturnType());

    }
  }
} 

I can get information package, number of lines, method's name etc.., but the problem is i want to print the value of a variable AT RUNTIME ! 我可以得到的信息包,行,方法的名称等的数量,但问题是我想打印一个变量在运行时的价值! Can you please help me? 你能帮我么?

This is a classic static vs. dynamic analysis problem. 这是经典的静态动态分析问题。 Currently your Eclipse plugin is viewing the projects statically because the projects are not being executed. 当前,您的Eclipse插件正在静态查看项目,因为未在执行项目。

For your example MyClass there is no associated main method so you can't actually run the project anyway. 对于您的示例MyClass ,没有关联的main方法,因此无论如何您实际上无法运行该项目。 Without adding a driver (main method with calls to this class) dynamic analysis is not going to be possible. 如果不添加驱动程序(使用此类的主要方法),则无法进行动态分析。 However if you went down the dynamic analysis route you might consider instrumenting MyClass with logging utilities to record the runtime values of aVariable (which may be more than one value!). 但是,如果沿着动态分析路线走下去,则可以考虑使用日志记录实用程序对MyClass进行检测,以记录aVariable的运行aVariable (可能超过一个值!)。

As for static analysis, any complete answer would have to conservatively report that aVariable could be any integer value because the setaVariable method is publicly accessible and could be passed any integer value. 对于静态分析,任何完整的答案都必须保守地报告aVariable可以是任何整数值,因为setaVariable方法是可公开访问的并且可以传递任何整数值。 If we restricted the potential call targets to only doSomething then you could report that 10 is the only possible value. 如果我们将潜在的通话目标限制为仅doSomething那么您可以报告10是唯一可能的值。 Finally if you did have other code that goes along with this class you could start from all valid program entry points (main methods or application specific lifecycle entry points) and do some analysis to find the places where doSomething and setaVariable are called from the within the rest of the project. 最后,如果您确实有与此类一起提供的其他代码,则可以从所有有效的程序入口点(主要方法或应用程序特定的生命周期入口点)开始,并进行一些分析,以查找在其中调用doSomethingsetaVariable位置。项目的其余部分。 You would then need to backtrack along data flow and control flow paths and perform some symbolic execution to find the potential runtime values (again there could be more than one!). 然后,您将需要沿数据流和控制流路径回溯,并执行一些符号执行以查找潜在的运行时值(同样可能有多个!)。

Consider the following main method as a driver. 考虑以下主要方法作为驱动程序。

public static void main(String[] args){
    Random rnd = new Random();
    MyClass c = new MyClass();
    if(rnd.nextBoolean()){
        c.setaVariable(100);
    } else {
        c.doSomething();
    }
}

What is the runtime value of aVariable ? aVariable的运行时值是aVariable It could have two values, 10 or 100 . 它可以有两个值10100 This is a tricky problem and a lot of research has gone into it. 这是一个棘手的问题,已经进行了很多研究。 The more precise you try to solve it the more headaches you will get. 您尝试解决的越精确,就会越头痛。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM