<commit_before>package com.leozc.lantern;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sun.org.apache.bcel.internal.classfile.StackMapEntry;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.plexus.util.FileUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;


@Mojo(name="blacklist")
public class Blacklister extends AbstractMojo {

    @Parameter(required = true,property = "lantern.rulefile")
    private String rulefile = null;
    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        File f = new File("target/dependency");
        File rule = new File(rulefile);
        List<RuleEntry> rules = new ArrayList<RuleEntry>();

        System.out.println("Rule => "+ rulefile);
//        for(Object e: this.getPluginContext().keySet()){
//            System.out.println(e + " => "+ this.getPluginContext().get(e));
//        }
//
        if(rule.exists()){
            try {
                Type listType = new TypeToken<ArrayList<RuleEntry>>() {
                }.getType();
                rules  = new Gson().fromJson(FileUtils.fileRead(rule),listType);

            } catch (IOException e) {
                throw new MojoFailureException("cannot read/parse "+rulefile + " ERROR "+e.getMessage());
            }
        }


        if(f.exists()){
            File[] scantargetJars =  f.listFiles(new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return name.toLowerCase().endsWith(".jar");
                }
            });
            if(scantargetJars.length==0){
                throw new MojoFailureException("No jar found ? please run 'mvn dependency:copy-dependencies' to pull down the dependencies to target/dependency folder before running this plugin");
            }
            System.out.println("====== Found the dependency folder " + scantargetJars.length + " scantargetJars found.");
            System.out.println(scantargetJars[0].getAbsolutePath());
            String outputfilename = "lantern.blacklist.out";
            try{
                FileWriter fw = new FileWriter(outputfilename,false);

                for(RuleEntry r : rules){
                    for(File jarpath:scantargetJars)
                        try {
                            MethodcallerMatcher app = new MethodcallerMatcher();
                            app.findCallingMethodsInJar(jarpath.getAbsolutePath(),r.className,r.methodDescriptor);
                            for (MethodcallerMatcher.Callee c : app.getCallees()) {
                                String outStr = jarpath.getName()+":"+c.source+":L"+c.line+":"+c.className+"."+c.methodName+"@"+c.methodDesc + ":calls "+r.className+"."+r.methodDescriptor;


                                System.out.println("@@:"+ c.source+":L"+c.line+":"+":calls "+r.className);
                                fw.write(outStr+"\n");
                            }
                            System.out.println("@@\n"+app.getCallees().size()+" methods invoke "+
                                    app.getTargetClass()+" "+
                                    app.getTargetMethod().getName()+" "+app.getTargetMethod().getDescriptor());

                        } catch (Exception e) {
                            System.err.println();
                        }
                }
                System.out.println(outputfilename +" is generated.");
            }   catch (IOException ioe){
                throw new MojoFailureException("Fail to write output log");
            }

        }
        else{
            System.out.println("======= NO");
            throw new MojoFailureException("please run 'mvn dependency:copy-dependencies' to pull down the dependencies to target/dependency folder before running this plugin");
        }
    }



    protected class RuleEntry{
        String className;
        String methodDescriptor;
    }
}<commit_msg>Add a fix on close<commit_after>package com.leozc.lantern;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sun.org.apache.bcel.internal.classfile.StackMapEntry;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.plexus.util.FileUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;


@Mojo(name="blacklist")
public class Blacklister extends AbstractMojo {

    @Parameter(required = true,property = "lantern.rulefile")
    private String rulefile = null;
    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        File f = new File("target/dependency");
        File rule = new File(rulefile);
        List<RuleEntry> rules = new ArrayList<RuleEntry>();

        System.out.println("Rule => "+ rulefile);
//        for(Object e: this.getPluginContext().keySet()){
//            System.out.println(e + " => "+ this.getPluginContext().get(e));
//        }
//
        if(rule.exists()){
            try {
                Type listType = new TypeToken<ArrayList<RuleEntry>>() {
                }.getType();
                rules  = new Gson().fromJson(FileUtils.fileRead(rule),listType);

            } catch (IOException e) {
                throw new MojoFailureException("cannot read/parse "+rulefile + " ERROR "+e.getMessage());
            }
        }


        if(f.exists()){
            File[] scantargetJars =  f.listFiles(new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return name.toLowerCase().endsWith(".jar");
                }
            });
            if(scantargetJars.length==0){
                throw new MojoFailureException("No jar found ? please run 'mvn dependency:copy-dependencies' to pull down the dependencies to target/dependency folder before running this plugin");
            }
            System.out.println("====== Found the dependency folder " + scantargetJars.length + " scantargetJars found.");
            System.out.println(scantargetJars[0].getAbsolutePath());
            String outputfilename = "lantern.blacklist.out";
            FileWriter fw = null;
            try{
                fw = new FileWriter(outputfilename,false);

                for(RuleEntry r : rules){
                    for(File jarpath:scantargetJars)
                        try {
                            MethodcallerMatcher app = new MethodcallerMatcher();
                            app.findCallingMethodsInJar(jarpath.getAbsolutePath(),r.className,r.methodDescriptor);
                            for (MethodcallerMatcher.Callee c : app.getCallees()) {
                                String outStr = jarpath.getName()+":"+c.source+":L"+c.line+":"+c.className+"."+c.methodName+"@"+c.methodDesc + ":calls "+r.className+"."+r.methodDescriptor;


                                System.out.println("@@:"+ c.source+":L"+c.line+":"+":calls "+r.className);
                                fw.write(outStr+"\n");
                            }
                            System.out.println("@@\n"+app.getCallees().size()+" methods invoke "+
                                    app.getTargetClass()+" "+
                                    app.getTargetMethod().getName()+" "+app.getTargetMethod().getDescriptor());

                        } catch (Exception e) {
                            System.err.println();
                        }
                }

                System.out.println(outputfilename +" is generated.");
            }   catch (IOException ioe){
                throw new MojoFailureException("Fail to write output log");
            }   finally {
               if(fw!=null)
                   try {
                       fw.close();
                   } catch (IOException e) {
                       e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                   }
            }

        }
        else{
            System.out.println("======= NO");
            throw new MojoFailureException("please run 'mvn dependency:copy-dependencies' to pull down the dependencies to target/dependency folder before running this plugin");
        }
    }



    protected class RuleEntry{
        String className;
        String methodDescriptor;
    }
}
{'fraction_non_alphanumeric': 0.1210667261771926, 'fraction_numerical': 0.0004463289444320464, 'mean_word_length': 1.9757636122177955, 'pattern_counts': {'":': 14, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7551149', 'n_tokens_mistral': 2376, 'n_tokens_neox': 2271, 'n_words': 465}