java中的枚举文件在结构上可能会变得混乱,我试图只获取枚举名称
...
import com.mckesson.dex.model.activityLog.concreteFactory.ModifiedPropertyYesNoFactory;
public enum OrganizationActivityLogItemType implements IActivityLogItemType<OrganizationActivityLog>, DisplaysAs
{
@Deprecated GENE_VARIANT_PUBLIC( "Gene Variant/Mutation/Allele", null ),
VARIANTS_MUTATIONS( "Variants/Mutations/ Alleles/Loci/Probes", ALFactory.PUBLIC_PRIVATE ),
CATEGORIES( "Categories", null ),
PUBLIC_VISIBLITY_COVERED_WHEN_MEETS_NECESSITY_CRITERIA( "Covered - When Meets Medical Necessity Criteria",
ALFactory.PUBLIC_PRIVATE ),
PUBLIC_VISIBLITY_NOT_COVERED_INVESTIGATIONAL(
"Not Covered - Investigational",
ALFactory.PUBLIC_PRIVATE ),
PUBLIC_VISIBLITY_NOT_COVERED_NOT_MEDICALLY_NECESSARY(
"Not Covered - Not Medically Necessary",
ALFactory.PUBLIC_PRIVATE ),
...
这很接近,但我觉得它可能可以做得更好。
awk '{ print $1 }' My.java | grep -v '^"' | grep -v '^ALFactory' | sed -e 's/(//'
如何使用 unix cli 工具从枚举文件中获取枚举名称?
解决方案至少需要移植到 OS X,因为那是我的工作环境,尽管我更希望它也能在 Linux 上运行。
答案1
perl
这是一个执行此操作的脚本。仅在命令行中执行管道命令是不可能的或不可维护的。一个天真的方法是
perl -npe 's/\b|\s+/\n/g' < My.java | grep '^[A-Z]\+_[A-Z_]\+'
但下面的脚本更好:
#!perl -w
my $content = do { local $/; <> };
my @tokens = split /\b|\s+/, $content;
my $n = 0;
my $skip;
foreach (@tokens) {
$n++; $skip = 0;
next if /\s+/;
next if /"/ ... /"/; # skip strings
next if /\(/ .. /\)/; # skip parameters
next if /@/ .. /^\w+$/; # skip annotations
$skip ||= 1 if $n == 1 .. /\benum\b/; # skip from beginning to enum
$skip ||= 1 if /enum/ .. /\{/; # skip to open bracket
next if $skip;
last if /\}/;
print $_, "\n";
}