Java 从UncaughtExceptionHandler引发的异常
我正在写一个apk分析程序。我不能深入细节,因为这是研究材料。然而,问题是,当我执行分析例程时,我不时会收到以下消息:Java 从UncaughtExceptionHandler引发的异常,java,multithreading,nullpointerexception,apk,Java,Multithreading,Nullpointerexception,Apk,我正在写一个apk分析程序。我不能深入细节,因为这是研究材料。然而,问题是,当我执行分析例程时,我不时会收到以下消息: Exception: java.lang.NullPointerException thrown from the UncaughtExceptionHandler in thread "main" 如果我不重写UncaughtExceptionHandler,或者我重写了UncaughtExceptionHandler,就会发生这种情况,您将从下面的代码中看到。由于没有st
Exception: java.lang.NullPointerException thrown from the UncaughtExceptionHandler in thread "main"
如果我不重写UncaughtExceptionHandler,或者我重写了UncaughtExceptionHandler,就会发生这种情况,您将从下面的代码中看到。由于没有stacktrace数据,我无法知道异常来自何处,其真正原因是什么
我希望你能帮助我
这只是主代码。我必须执行三个主要操作。为了不透露具体信息,我将它们重命名为A、B和C
public class MainScannerSequential {
public static void main(String[] args) throws ParserConfigurationException, IOException, InterruptedException {
final File target = new File(args[1]);
final File result = new File(args[2]);
final Options options = new Options(args);
silentMode = options.contains("-s");
noA = options.contains("-nl");
noC = options.contains("-ne");
aStrategy = Factory.createA();
bScanner = Factory.createB();
cDetector = Factory.createcC();
examinedFiles = new PersistentFileList(Globals.EXAMINED_FILES_LIST_FILE);
if (result.exists())
resultsWriter = new BufferedWriter(new FileWriter(result, true));
else {
resultsWriter = new BufferedWriter(new FileWriter(result));
resultsWriter.write("***");
resultsWriter.newLine();
}
if (Globals.PERFORMANCE_FILE.exists())
performancesWriter = new BufferedWriter(new FileWriter(Globals.PERFORMANCE_FILE, true));
else {
performancesWriter = new BufferedWriter(new FileWriter(Globals.PERFORMANCE_FILE));
performancesWriter.write("***");
performancesWriter.newLine();
}
Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
if ((t != null) && (t.getName() != null))
System.out.println("In thread : " + t.getName());
if ((e != null) && (e.getMessage() != null)) {
System.out.println(e.getClass().getName() + ": " + e.getMessage());
if (e.getStackTrace() != null)
for (StackTraceElement ste : e.getStackTrace())
if (ste != null)
System.out.println(ste.getFileName() + " at line " + ste.getLineNumber());
}
}
});
if (target.isDirectory()) {
enumerateDirectory(target);
} else {
String name = target.getName().toLowerCase();
if (name.endsWith(".apklist"))
readFileList(target);
else if (name.endsWith(".apk"))
checkFile(target);
}
closeWriters();
}
private static void println(String message) {
if (!silentMode)
System.out.println(message);
}
private static void print(String message) {
if (!silentMode)
System.out.print(message);
}
private static void enumerateDirectory(File directory) {
for (File file : directory.listFiles()) {
checkFile(file);
if (file.isDirectory())
enumerateDirectory(file);
}
}
private static void readFileList(File file) {
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = null;
while((line = reader.readLine()) != null) {
File readFile = new File(line);
if (readFile.exists())
checkFile(readFile);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void checkFile(File file) {
if (examinedFiles.contains(file)) {
println("Skipped: " + file.getName());
return;
}
if (!file.getName().toLowerCase().endsWith(".apk")) {
return;
}
final Wrapper<Double> unpackingTime = new Wrapper<Double>(0.0);
final ApplicationData data = unpack(file, unpackingTime);
if (data == null) {
return;
}
scanData(data, unpackingTime.value);
}
private static ApplicationData unpack(final File file, final Wrapper<Double> unpackingTime) {
final Wrapper<ApplicationData> resultWrapper = new Wrapper<ApplicationData>(null);
final Wrapper<Exception> exceptionWrapper = new Wrapper<Exception>(null);
println("Unpacking: " + file.getName());
unpackingTime.value = Stopwatch.time(new Runnable() {
@Override
public void run() {
try {
resultWrapper.value = ApplicationData.open(file);
} catch (Exception e) {
exceptionWrapper.value = e;
}
}
});
if (resultWrapper.value != null)
println("Unpacked: " + file.getName());
else if (exceptionWrapper.value != null)
println("Dropped: " + file.getName() + " : " + exceptionWrapper.value.getMessage());
return resultWrapper.value;
}
private static void scanData(final ApplicationData applicationData, Double unpackingTime) {
String apkName = applicationData.getDecodedPackage().getOriginalApk().getAbsolutePath();
println("Submitted: " + apkName);
examinedFiles.add(applicationData.getDecodedPackage().getOriginalApk());
final Wrapper<Boolean> aDetected = new Wrapper<Boolean>(false);
final Wrapper<Result> bDetected = new Wrapper<Result>(new Result());
final Wrapper<Boolean> cDetected = new Wrapper<Boolean>(false);
final Wrapper<Double> aDetectionTime = new Wrapper<Double>((double)ANALYSIS_TIMEOUT);
final Wrapper<Double> bDetectionTime = new Wrapper<Double>((double)ANALYSIS_TIMEOUT);
final Wrapper<Double> cDetectionTime = new Wrapper<Double>((double)ANALYSIS_TIMEOUT);
ExecutorService executor = Executors.newFixedThreadPool(3);
executor.submit(new Runnable() {
@Override
public void run() {
textDetectionTime.value = Stopwatch.time(new Runnable() {
@Override
public void run() {
bScanner.setUnpackedApkDirectory(applicationData.getDecodedPackage().getDecodedDirectory());
bDetected.value = bScanner.evaluate();
}
});
}
});
if (!noA)
executor.submit(new Runnable() {
@Override
public void run() {
lockDetectionTime.value = Stopwatch.time(new Runnable() {
@Override
public void run() {
aStrategy.setTarget(applicationData.getDecodedPackage());
aDetected.value = aStrategy.detect();
}
});
}
});
if (!noC)
executor.submit(new Runnable() {
@Override
public void run() {
encryptionDetectionTime.value = Stopwatch.time(new Runnable() {
@Override
public void run() {
cDetector.setTarget(applicationData.getDecodedPackage());
cDetected.value = cDetector.detect();
}
});
}
});
boolean timedOut = false;
executor.shutdown();
try {
if (!executor.awaitTermination(ANALYSIS_TIMEOUT, TimeUnit.SECONDS)) {
executor.shutdownNow();
timedOut = true;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
resultsWriter.write(String.format("%s, %b, %b, %f, %b, \"%s\", %b\n",
apkName,
aDetected.value,
bDetected.value.isAccepted(),
bDetected.value.getScore(),
cDetected.value,
bDetected.value.getComment(),
timedOut));
performancesWriter.write(String.format("%s, %f, %f, %f, %f, %d, %dl, %dl\n",
apkName,
aDetectionTime.value,
bDetectionTime.value,
cDetectionTime.value,
unpackingTime,
applicationData.getSmaliLoader().getClassesCount(),
applicationData.getSmaliLoader().getTotalClassesSize(),
applicationData.getDecodedPackage().getOriginalApk().length()));
resultsWriter.flush();
performancesWriter.flush();
} catch (IOException e) { }
// No longer deleting temp files
if (!timedOut)
println("Completed: " + apkName);
else {
print("Timeout");
if (bDetectionTime.value == 0) print(" TextDetection");
if (!noA && (aDetectionTime.value == 0)) print(" LockDetection");
if (!noC && (cDetectionTime.value == 0)) print(" EncryptionDetection");
println(": " + apkName);
}
}
private static void closeWriters() throws IOException {
resultsWriter.close();
performancesWriter.close();
examinedFiles.dispose();
}
private static final int ANALYSIS_TIMEOUT = 40; // seconds
private static Boolean silentMode = false;
private static Boolean noA = false;
private static Boolean noC = false;
private static PersistentFileList examinedFiles;
private static BufferedWriter resultsWriter;
private static BufferedWriter performancesWriter;
private static A aStrategy;
private static B bScanner;
private static C cDetector;
}
公共类主扫描程序{
公共静态void main(字符串[]args)抛出ParserConfiguration异常、IOException、InterruptedException{
最终文件目标=新文件(args[1]);
最终文件结果=新文件(args[2]);
最终选项=新选项(args);
silentMode=options.contains(“-s”);
noA=选项。包含(“-nl”);
noC=选项。包含(“-ne”);
astragy=Factory.createA();
bScanner=Factory.createB();
cDetector=Factory.createcC();
检查文件=新的持久文件列表(Globals.inspected\u FILES\u LIST\u FILE);
if(result.exists())
resultsWriter=new BufferedWriter(new FileWriter(result,true));
否则{
resultsWriter=new BufferedWriter(new FileWriter(result));
resultsWriter.write(“***”);
resultsWriter.newLine();
}
if(Globals.PERFORMANCE\u FILE.exists())
performancesWriter=新的BufferedWriter(新的FileWriter(Globals.PERFORMANCE_文件,true));
否则{
performancesWriter=new BufferedWriter(新文件编写器(Globals.PERFORMANCE_文件));
performancesWriter.write(“***”);
performancesWriter.newLine();
}
Thread.currentThread().setUncaughtExceptionHandler(新线程.UncaughtExceptionHandler()){
@凌驾
公共无效未捕获异常(线程t,可丢弃的e){
if((t!=null)&(t.getName()!=null))
System.out.println(“线程内:+t.getName());
如果((e!=null)&(e.getMessage()!=null)){
System.out.println(e.getClass().getName()+“:”+e.getMessage());
如果(如getStackTrace()!=null)
对于(StackTraceElement存储:e.getStackTrace())
如果(ste!=null)
System.out.println(ste.getFileName()+“在”+ste.getLineNumber()行);
}
}
});
if(target.isDirectory()){
枚举目录(目标);
}否则{
字符串名称=target.getName().toLowerCase();
if(name.endsWith(“.apklist”))
readFileList(目标);
else if(name.endsWith(“.apk”))
检查文件(目标);
}
closeWriters();
}
私有静态void println(字符串消息){
如果(!silentMode)
System.out.println(消息);
}
私有静态无效打印(字符串消息){
如果(!silentMode)
系统输出打印(消息);
}
专用静态目录(文件目录){
对于(文件:directory.listFiles()){
检查文件(文件);
if(file.isDirectory())
枚举目录(文件);
}
}
私有静态void readFileList(文件){
试一试{
BufferedReader reader=新的BufferedReader(新文件读取器(文件));
字符串行=null;
而((line=reader.readLine())!=null){
文件读取文件=新文件(行);
if(readFile.exists())
检查文件(readFile);
}
reader.close();
}捕获(例外e){
e、 printStackTrace();
}
}
专用静态无效检查文件(文件){
if(检查文件.包含(文件)){
println(“跳过:“+file.getName());
返回;
}
如果(!file.getName().toLowerCase().endsWith(“.apk”)){
返回;
}
最终包装拆包时间=新包装(0.0);
最终应用程序数据=解包(文件,解包时间);
如果(数据==null){
返回;
}
Scanda(数据,解包时间.value);
}
私有静态应用程序数据解包(最终文件文件,最终包装解包时间){
最终包装器resultWrapper=新包装器(null);
最终包装例外包装=新包装(空);
println(“解包:+file.getName());
unpackingTime.value=Stopwatch.time(new Runnable()){
@凌驾
公开募捐{
试一试{
resultWrapper.value=ApplicationData.open(文件);
}捕获(例外e){
exceptionWrapper.value=e;
}
}
});
if(resultWrapper.value!=null)
println(“解包:+file.getName());
else if(exceptionWrapper.value!=null)
println(“删除:“+file.getName()+”:“+exceptionWrapper.value.getMessage());
返回resultWrapper.value;
}
私有静态无效扫描数据(最终应用数据应用数据,双重解压时间){
字符串apkName=applicationData.getDecodedPackage().getOriginalApk().getAbsolutePath();
println(“提交:+apkName”);
检查文件.add(applicationData.getDecodedPackage().getOriginalApk());
最终包装器检测=新包装器(假);
最终包装器检测=新包装器(新结果());
最终包装cDetected=新包装(false);
最终包装器检测时间=新包装器((双)分析超时);
最终包装器bDetectionTime=新包装器((双)分析\u超时);
最终包装器cDetectionTime=新包装器((双)分析\u超时);
ExecutorService executor=Executors.newFixedThreadPool(3);
执行者提交(新的可运行(){
@凌驾
公开募捐{
textDetectionTime.value=秒表.time(新的可运行(){
@凌驾
公开募捐{
bScanner.setunpackapkdirec