1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#include "javautil.h"
6
7#include <QProcess>
8#include <QDebug>
9
10QString JavaUtil::getMainClassPath(const QDir &dir)
11{
12 QFileInfoList entries = dir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks);
13 //Create a QRegExp object with the given regular expression
14 QRegExp regExp("public\\s+static\\s+void\\s+main\\s*\\(\\s*");
15
16 //Loop through files
17 foreach (QFileInfo entry, entries) {
18 if (entry.isDir()) {
19 //If the entry is a folder, call the browseRecursively function again
20 QDir dir(entry.filePath());
21 QString mainClass = getMainClassPath(dir);
22 if (!mainClass.isEmpty())
23 return mainClass;
24 } else {
25 //If the file is not a folder, then check if it has the .class extension
26 if (entry.suffix().toLower() == "class") {
27 qInfo() << entry.fileName();
28
29 QProcess process;
30 auto temp = entry.filePath();
31 process.start("javap " + entry.filePath());
32 if(!process.waitForFinished()) {
33 qDebug() << "process is error!";
34 break;
35 }
36 QString output = process.readAllStandardOutput();
37 //Check if the given regular expression matches the file content
38 if (regExp.indexIn(output) >= 0) {
39 return entry.filePath();
40 }
41 }
42 }
43 }
44 return {};
45}
46
47QString JavaUtil::getMainClass(const QString &mainClassPath, const QString &packageDirName)
48{
49 QString mainClass;
50 if (!mainClassPath.isEmpty()) {
51 int index = mainClassPath.indexOf(packageDirName);
52 mainClass = mainClassPath.mid(index + packageDirName.size() + 1);
53 mainClass.remove(".class");
54 mainClass.replace("/", ".");
55 }
56 return mainClass;
57}
58
59QString JavaUtil::getPackageDir(const QString &mainClassPath, const QString &packageDirName)
60{
61 QString packageDir;
62 if (!mainClassPath.isEmpty()) {
63 QString targetPath = mainClassPath.left(mainClassPath.indexOf(packageDirName));
64 packageDir = targetPath + packageDirName;
65 }
66 return packageDir;
67}
68