在我的java代码中找出双重算术运算 [英] Find out double arithmetic operations in my java code

查看:319
本文介绍了在我的java代码中找出双重算术运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多的Java项目,我想在我的整个代码中找到双重算术运算(+, - ,*,/)。例如,我想从下面的代码中找到 a + b 行。一旦代码返回所有结果,我将手动将其更改为一些API。任何人有任何想法如何编写一个返回所有这些用法的程序?找到下面的结果的代码不一定要写成java。我没有找到这个问题在stackoverflow或谷歌,请发布,如果你找到任何。

  double a = 0.0,b = 0; 
if(a + b> 0){
...


double 的操作有自己的字节码指令 dadd ddiv dmul 等等,所以你可以在你的项目的类文件中搜索这些操作。



我确定有工具可以做,所以搜索这些。



但是,如果你找不到合适的,建立自己的并不难。在一个编译的类上,你可以使用 javap -c ClassName 来获得该类字节码的一个相当可读的版本。例如,这段代码:

  import java.util.Random; 

public class Example {
public static void main(String [] args){
Random r = new Random();
double d1 = r.nextDouble();
double d2 = r.nextDouble();
double d3 = d1 + d2;
System.out.println(d3);




$ p $结果在$



编译自Example.java
public class Example
{
public Example();
代码:
0:aload_0
1:invokespecial#1 //方法java / lang / Object。:(:)V
4:return

public static void main(java.lang.String []);
代码:
0:new#2 // class java / util / Random
3:dup
4:invokespecial#3 //方法java / util / Random。 :()V
7:astore_1
8:aload_1
9:invokevirtual#4 //方法java / util / Random.nextDouble :()D
12:dstore_2
13:aload_1
14:invokevirtual#4 //方法java / util / Random.nextDouble :()D
17:dstore 4
19:dload_2
20:dload 4
22:dadd
23:dstore 6
25:getstatic#5 //字段java / lang / System.out:Ljava / io / PrintStream;
28:dload 6
30:invokevirtual#6 //方法java / io / PrintStream.println:(D)V
33:return
}

 

所以你可以编写一个程序来:


  1. 获取每个类的 javap -c 输出

  2. double 相关的字节码指令(相当简单的正则表达式工作)



I have many java projects and I would like to find the double arithmetic operations (+, -, *, /) in my entire code. For example, I want to find a + b line from the below code. Once the code returns all the results, I will manually change them to some APIs. Any one has any idea how to write a program which returns all of such usages? The code to find the below results doesn't necessarily to be written java. I didn't find this question in stackoverflow or in google, please post it if you find any.

double a = 0.0, b=0;
if(a+b > 0){
...

解决方案

Operations on doubles have their own bytecode instructions: dadd, ddiv, dmul, etc. So you can search for those operations in the class files for your project.

I'm sure there are tools to do that, so search for those.

But if you can't find any suitable, it's not too hard to build your own. On a compiled class, you can use javap -c ClassName to get a fairly human-readable version of the bytecode for the class. For instance, this code:

import java.util.Random;

public class Example {
    public static void main(String[] args) {
        Random r = new Random();
        double d1 = r.nextDouble();
        double d2 = r.nextDouble();
        double d3 = d1 + d2;
        System.out.println(d3);
    }
}

...results in this output from javap -c:

Compiled from "Example.java"
public class Example {
  public Example();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: new           #2                  // class java/util/Random
       3: dup
       4: invokespecial #3                  // Method java/util/Random."":()V
       7: astore_1
       8: aload_1
       9: invokevirtual #4                  // Method java/util/Random.nextDouble:()D
      12: dstore_2
      13: aload_1
      14: invokevirtual #4                  // Method java/util/Random.nextDouble:()D
      17: dstore        4
      19: dload_2
      20: dload         4
      22: dadd
      23: dstore        6
      25: getstatic     #5                  // Field java/lang/System.out:Ljava/io/PrintStream;
      28: dload         6
      30: invokevirtual #6                  // Method java/io/PrintStream.println:(D)V
      33: return
}

So you could write a program to:

  1. Get the javap -c output for each class

  2. Look in that output for double-related bytecode instructions (fairly simple regular expression work)

这篇关于在我的java代码中找出双重算术运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆