首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用Perl识别对Java类的引用?

如何使用Perl识别对Java类的引用?
EN

Stack Overflow用户
提问于 2018-12-05 00:29:20
回答 2查看 0关注 0票数 0

我正在写一个Perl脚本,但我遇到了一个瓶颈。我需要逐行解析Java源文件,检查对完全限定的Java类名的引用。我知道我正在寻找的类,它也是正在搜索的源文件的完全限定名称(基于其路径)。

例如,在com / bob / is / YourUncle.java文件中找到对foo.bar.Baz的所有有效引用。

此时我能想到的需要考虑的案例是:

  1. 正在解析的文件与搜索类位于同一个包中。 在foo / bar / Boing.java中找到foo.bar.Baz引用
  2. 它应该忽略comments。 // this is a comment saying this method returns a foo.bar.Baz or Baz instance // it shouldn't count /* a multiline comment as well this shouldn't count if I put foo.bar.Baz or Baz in here either */
  3. 在线完全限定参考。 foo.bar.Baz fb = new foo.bar.Baz();
  4. 引用基于import语句。 import foo.bar.Baz; ... Baz b = new Baz();

在Perl 5.8中最有效的方法是什么?或许有些花哨的正则表达式?

代码语言:javascript
复制
open F, $File::Find::name or die;
# these three things are already known
# $classToFind    looking for references of this class
# $pkgToFind      the package of the class you're finding references of
# $currentPkg     package name of the file being parsed
while(<F>){
  # ... do work here   
}
close F;
# the results are availble here in some form
EN

回答 2

Stack Overflow用户

发布于 2018-12-05 09:20:39

如果你仍然想继续探索,你尝试可以看看Parse :: RecDescent

票数 0
EN

Stack Overflow用户

发布于 2018-12-05 09:36:53

这是我想出来的,它适用于我设置的所有不同情况。我还是个新手,它可能不是世界上最先进最好的,但它应该适用于我需要的东西。感谢大家的回答,可以帮助我以不同的方式看待它。

代码语言:javascript
复制
  my $className = 'Baz';
  my $searchPkg = 'foo.bar';
  my @potentialRefs, my @confirmedRefs;
  my $samePkg = 0;
  my $imported = 0;
  my $currentPkg = 'com.bob';
  $currentPkg =~ s/\//\./g;
  if($currentPkg eq $searchPkg){
    $samePkg = 1;  
  }
  my $inMultiLineComment = 0;
  open F, $_ or die;
  my $lineNum = 0;
  while(<F>){
    $lineNum++;
    if($inMultiLineComment){
      if(m|^.*?\*/|){
        s|^.*?\*/||; #get rid of the closing part of the multiline comment we're in
        $inMultiLineComment = 0;
      }else{
        next;
      }
    }
    if(length($_) > 0){
      s|"([^"\\]*(\\.[^"\\]*)*)"||g; #remove strings first since java cannot have multiline string literals
      s|/\*.*?\*/||g;  #remove any multiline comments that start and end on the same line
      s|//.*$||;  #remove the // comments from what's left
      if (m|/\*.*$|){
        $inMultiLineComment = 1 ;#now if you have any occurence of /* then then at least some of the next line is in the multiline comment
        s|/\*.*$||g;
      }
    }else{
      next; #no sense continuing to process a blank string
    }

    if (/^\s*(import )?($searchPkg)?(.*)?\b$className\b/){
      if($imported || $samePkg){
        push(@confirmedRefs, $lineNum);
      }else {
        push(@potentialRefs, $lineNum);
      }
      if($1){
        $imported = 1;
      } elsif($2){
        push(@confirmedRefs, $lineNum);
      }
    }
  }
  close F;      
  if($imported){
    push(@confirmedRefs,@potentialRefs);
  }

  for (@confirmedRefs){
    print "$_\n";
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100000896

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档