Skip to content

Commit f0ac0c0

Browse files
committed
This commit incorporates review changes from PR-#378
Signed-off-by: Rakshit Krishnappa Ravi <[email protected]> Below changes have been made: - replaced sysouts with logInfos - changed exception logging to go through Activator.getDefault.logError() - added curly braces to single line branch code - removed single use variables - used try-with-resource syntax instead of try-finally - changed return type of selectedDialogOption method
1 parent e0749a8 commit f0ac0c0

File tree

1 file changed

+34
-46
lines changed
  • plugins/de.cognicrypt.staticanalyzer.kotlin/src/de/cognicrypt/staticanalyzer/kotlin/utilities

1 file changed

+34
-46
lines changed

plugins/de.cognicrypt.staticanalyzer.kotlin/src/de/cognicrypt/staticanalyzer/kotlin/utilities/KotlinUtils.java

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
1414
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
1515
import org.apache.maven.project.MavenProject;
16+
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
1617
import org.eclipse.core.resources.IFile;
1718
import org.eclipse.core.resources.IProject;
1819
import org.eclipse.core.resources.IResource;
@@ -35,13 +36,14 @@
3536
import org.eclipse.ui.IWorkbenchWindow;
3637
import org.eclipse.ui.PlatformUI;
3738
import org.jetbrains.kotlin.core.compiler.KotlinCompiler;
38-
import org.jetbrains.kotlin.core.compiler.KotlinCompilerResult;
3939
import org.jetbrains.kotlin.core.model.KotlinNature;
4040

4141
import de.cognicrypt.staticanalyzer.kotlin.Activator;
4242

4343
public class KotlinUtils {
4444

45+
private static final String KOTLIN_FILE_EXTENSION = ".kt";
46+
4547
public static void compileKotlinFiles(IProject ip) {
4648
IJavaProject javaProject = JavaCore.create(ip);
4749

@@ -51,15 +53,14 @@ public static void compileKotlinFiles(IProject ip) {
5153
if(ip.hasNature("org.eclipse.m2e.core.maven2Nature")) {
5254
if(KotlinUtils.verifyKotlinDependency(javaProject)) {
5355

54-
System.out.println("Custom Builder started.");
5556
final Job builder = new Job("Custom Builder") {
5657

5758
@Override
5859
protected IStatus run(IProgressMonitor monitor) {
5960
try {
6061
ip.build(IncrementalProjectBuilder.CLEAN_BUILD, null);
6162
} catch (CoreException e) {
62-
e.printStackTrace();
63+
Activator.getDefault().logError(e);
6364
}
6465
return Status.OK_STATUS;
6566
}
@@ -70,30 +71,29 @@ protected IStatus run(IProgressMonitor monitor) {
7071
while(builder.getState() != Job.NONE) {
7172
Thread.sleep(3);
7273
}
73-
System.out.println("Custom Builder finished.");
7474

7575
KotlinUtils.waitForBuildAndRefreshJobs();
7676

77-
KotlinCompilerResult result = KotlinCompiler.compileKotlinFiles(javaProject);
78-
if(result.compiledCorrectly())
77+
if(KotlinCompiler.compileKotlinFiles(javaProject).compiledCorrectly()) {
7978
Activator.getDefault().logInfo("Finished compiling kotlin files.");
80-
else
79+
} else {
8180
Activator.getDefault().logInfo("Cannot compile some kotlin files due to errors. Static analysis skipped them.");
81+
}
8282
}
8383
else {
84-
Activator.getDefault().logInfo("Cannot compile kotlin files without dependency.");
84+
Activator.getDefault().logInfo("Cannot compile kotlin files without kotlin-stdlib dependency.");
8585
Activator.getDefault().logInfo("Static analysis skipped all kotlin files.");
8686
}
8787
}
8888
else {
89-
KotlinCompilerResult result = KotlinCompiler.compileKotlinFiles(javaProject);
90-
if(result.compiledCorrectly())
89+
if(KotlinCompiler.compileKotlinFiles(javaProject).compiledCorrectly()) {
9190
Activator.getDefault().logInfo("Finished compiling kotlin files.");
92-
else
91+
} else {
9392
Activator.getDefault().logInfo("Cannot compile some kotlin files due to errors. Static analysis skipped them.");
93+
}
9494
}
9595
} catch (CoreException | OperationCanceledException | InterruptedException e) {
96-
e.printStackTrace();
96+
Activator.getDefault().logError(e);
9797
}
9898
}
9999
}
@@ -113,14 +113,15 @@ public static IResource findKotlinClassByName(final String className, final IPro
113113
String srcFilename = "";
114114

115115
if(classFileName.substring(classFileName.length()-2).equals("Kt")) {
116-
srcFilename = classFileName.substring(0, classFileName.length()-2) + ".kt";
116+
srcFilename = classFileName.substring(0, classFileName.length()-2) + KOTLIN_FILE_EXTENSION;
117117
}
118118
else if(classFileName.contains("$")) {
119-
srcFilename = Arrays.asList(classFileName.split("\\$")).get(0) + ".kt";
119+
srcFilename = Arrays.asList(classFileName.split("\\$")).get(0) + KOTLIN_FILE_EXTENSION;
120120
}
121121
// because in some projects the class names aren't renamed
122-
else
123-
srcFilename = classFileName + ".kt";
122+
else {
123+
srcFilename = classFileName + KOTLIN_FILE_EXTENSION;
124+
}
124125

125126
for (final IPackageFragment l : JavaCore.create(currentProject).getPackageFragments()) {
126127
// this check is needed because IJavaProject.getPackageFragments() returns dependencies as well
@@ -131,8 +132,9 @@ else if(classFileName.contains("$")) {
131132
String packageName = String.join(File.separator, modifiedPath);
132133

133134
IFile sourceFile = currentProject.getFile(packageName + File.separator + srcFilename);
134-
if(sourceFile.exists())
135+
if(sourceFile.exists()) {
135136
return (IResource) sourceFile;
137+
}
136138
}
137139
}
138140
}
@@ -142,27 +144,26 @@ else if(classFileName.contains("$")) {
142144
}
143145

144146
public static boolean verifyKotlinDependency(IJavaProject javaProject) {
145-
IProject p = javaProject.getProject();
146-
IFile pomFile = p.getFile("pom.xml");
147+
IProject project = javaProject.getProject();
148+
IFile pomFile = project.getFile("pom.xml");
147149
boolean isPresent = false;
148150
try {
149-
MavenProject project = loadProject(pomFile.getLocation().toFile());
150-
List<Dependency> dependencies = project.getDependencies();
151+
MavenProject mavenProject = loadProject(pomFile.getLocation().toFile());
152+
List<Dependency> dependencies = mavenProject.getDependencies();
151153

152154
for (Dependency dependency : dependencies) {
153-
System.out.println(dependency);
154-
if(dependency.getArtifactId().equals("kotlin-stdlib"))
155+
if("kotlin-stdlib".equals(dependency.getArtifactId())) {
155156
isPresent = true;
157+
}
156158
}
157159
if(!isPresent) {
158-
boolean accepted = requestUsersPermission();
159-
if(accepted) {
160-
addKotlinDependency(project, dependencies);
160+
if(requestUsersPermission() == SWT.YES) {
161+
addKotlinDependency(mavenProject, dependencies);
161162
isPresent = true;
162163
}
163164
}
164165
} catch (Exception e) {
165-
e.printStackTrace();
166+
Activator.getDefault().logError(e);
166167
}
167168
return isPresent;
168169
}
@@ -177,10 +178,8 @@ public static void waitForBuildAndRefreshJobs() {
177178
jobs.addAll(Arrays.asList(Job.getJobManager().find(ResourcesPlugin.FAMILY_MANUAL_BUILD)));
178179
jobs.addAll(Arrays.asList(Job.getJobManager().find(ResourcesPlugin.FAMILY_MANUAL_REFRESH)));
179180
if(jobs.isEmpty()) {
180-
System.out.println("No pending jobs found.");
181181
return;
182182
}
183-
System.out.println("Waiting for " + jobs.size() + " Jobs to finish...");
184183

185184
boolean buildSuccess;
186185
do {
@@ -213,48 +212,37 @@ private static void storeProject(MavenProject project) {
213212
mavenWriter.write(writer, model);
214213
Activator.getDefault().logInfo("Required kotlin dependency added to pom.");
215214
} catch (IOException e) {
216-
e.printStackTrace();
215+
Activator.getDefault().logError(e);
217216
}
218217
}
219218

220-
private static MavenProject loadProject(File pomFile) throws Exception
219+
private static MavenProject loadProject(File pomFile) throws IOException, XmlPullParserException
221220
{
222221
MavenProject ret = null;
223222
MavenXpp3Reader mavenReader = new MavenXpp3Reader();
224223

225224
if (pomFile != null && pomFile.exists())
226225
{
227-
FileReader reader = null;
228-
try
229-
{
230-
reader = new FileReader(pomFile);
226+
try (FileReader reader = new FileReader(pomFile)) {
231227
Model model = mavenReader.read(reader);
232228
model.setPomFile(pomFile);
233229
ret = new MavenProject(model);
234230
}
235-
finally
236-
{
237-
reader.close();
238-
}
239231
}
240232
return ret;
241233
}
242234

243-
private static boolean requestUsersPermission() {
235+
private static int requestUsersPermission() {
244236
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
245237
MessageBox dialog = new MessageBox(window.getShell(),
246238
SWT.APPLICATION_MODAL | SWT.ICON_QUESTION | SWT.YES | SWT.NO);
247239
return selectedDialogOption(dialog);
248240
}
249241

250-
private static boolean selectedDialogOption(MessageBox dialog) {
242+
private static int selectedDialogOption(MessageBox dialog) {
251243
dialog.setMessage("Analysis requires maven kotlin-stdlib dependency. Would you like to add it?");
252244
dialog.setText("CAUTION");
253-
int opt = dialog.open();
254-
if (opt == SWT.YES)
255-
return true;
256-
else
257-
return false;
245+
return dialog.open();
258246
}
259247

260248

0 commit comments

Comments
 (0)