├── .gitignore
├── src
└── main
│ ├── resources
│ ├── META-INF
│ │ ├── MANIFEST.MF
│ │ └── plugin.xml
│ └── guard_mask_16x16.png
│ └── java
│ └── com
│ └── github
│ └── intelliguard
│ ├── runner
│ ├── ProgressInfoReceiver.java
│ ├── RunProgress.java
│ ├── ObfuscateTask.java
│ └── JarTask.java
│ ├── ui
│ ├── Icons.java
│ ├── ToolWindowPanel.java
│ ├── ToolWindowPanel.form
│ ├── MainClassChooser.java
│ ├── FormDialogWrapper.java
│ ├── ExportOptionsForm.java
│ ├── ExportOptionsForm.form
│ ├── FileChooserFactory.java
│ ├── JarOptionsForm.java
│ └── JarOptionsForm.form
│ ├── action
│ ├── ExportYGuard.java
│ ├── ExportProGuard.java
│ ├── ExportGroup.java
│ ├── AbstractGuardAction.java
│ ├── GutterAction.java
│ ├── AbstractExportAction.java
│ └── RunObfuscationAction.java
│ ├── util
│ ├── ModuleUtils.java
│ ├── UiUtils.java
│ ├── ObfuscatorUtils.java
│ ├── InspectionUtils.java
│ └── PsiUtils.java
│ ├── model
│ ├── JarConfig.java
│ └── Keeper.java
│ ├── facet
│ ├── GuardFacet.java
│ ├── GuardFacetEditorTab.java
│ ├── GuardFacetType.java
│ └── GuardFacetConfiguration.java
│ ├── gutter
│ ├── GuardMarkerEditorListener.java
│ ├── GuardGutterRenderer.java
│ ├── GuardGutterRendererComputation.java
│ └── GuardMarker.java
│ ├── ant
│ ├── YProject.java
│ └── YProjectHelper.java
│ ├── GuardComponent.java
│ ├── fix
│ ├── AddKeepFix.java
│ └── RemoveKeepFix.java
│ ├── GuardProjectComponent.java
│ ├── inspection
│ ├── ReflectionProblemsInspection.java
│ ├── SerializationProblemsInspection.java
│ ├── GuardInspectionBase.java
│ ├── PluginProblemsInspection.java
│ └── GuardInspection.java
│ ├── generator
│ ├── ProGuardGenerator.java
│ └── YGuardGenerator.java
│ └── refactor
│ └── RenameListenerProvider.java
├── license.txt
├── pom.xml
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea/
2 | *.iml
3 | *.iws
4 | target/
--------------------------------------------------------------------------------
/src/main/resources/META-INF/MANIFEST.MF:
--------------------------------------------------------------------------------
1 | Manifest-Version: 1.0
2 | Class-Path: ant-1.8.2.jar ant-launcher-1.8.2.jar
3 | Main-Class:
4 |
5 |
--------------------------------------------------------------------------------
/src/main/resources/guard_mask_16x16.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jorisguffens/intelliguard/HEAD/src/main/resources/guard_mask_16x16.png
--------------------------------------------------------------------------------
/license.txt:
--------------------------------------------------------------------------------
1 | Copyright 2009 Ronnie Kolehmainen
2 |
3 | Licensed under the Apache License, Version 2.0 (the "License");
4 | you may not use this file except in compliance with the License.
5 | You may obtain a copy of the License at
6 |
7 | http://www.apache.org/licenses/LICENSE-2.0
8 |
9 | Unless required by applicable law or agreed to in writing, software
10 | distributed under the License is distributed on an "AS IS" BASIS,
11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | See the License for the specific language governing permissions and
13 | limitations under the License.
14 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.github.intelliguard
8 | intelliguard
9 | 2.0.1
10 |
11 |
12 | 1.8
13 | 1.8
14 |
15 |
16 |
17 |
18 |
19 |
20 | org.apache.ant
21 | ant
22 | 1.8.2
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/runner/ProgressInfoReceiver.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.runner;
18 |
19 | /**
20 | * Created by IntelliJ IDEA.
21 | * User: Ronnie
22 | * Date: 2009-nov-10
23 | * Time: 20:05:56
24 | */
25 | public interface ProgressInfoReceiver
26 | {
27 | public void info(String info);
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/ui/Icons.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.ui;
18 |
19 | import com.intellij.openapi.util.IconLoader;
20 |
21 | import javax.swing.*;
22 |
23 | /**
24 | * Created by IntelliJ IDEA.
25 | * User: Ronnie
26 | * Date: 2009-nov-03
27 | * Time: 18:50:43
28 | */
29 | public interface Icons
30 | {
31 | public static final Icon OBFUSCATION_NODE_ICON = IconLoader.getIcon("/guard_mask_16x16.png");
32 | }
33 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | IntelliGuard 2
2 | ============
3 |
4 | >Updated and maintained by iGufGuf, originally created by [Ronniekk](https://github.com/ronniekk/intelliguard)
5 | >
6 | >Created and tested for intellij 2018.2
7 |
8 | ### Features
9 |
10 | Obfuscation made easy.
11 |
12 | IntelliGuard integrates yGuard - a free Java bytecode obfuscator - with IntelliJ IDEA. Key features are:
13 |
14 | * No more hazzles with configuration files. Just hit ALT+ENTER on any symbol in the editor for keep options.
15 | * Refactor support. Kept symbols remain unobfuscated after rename or move.
16 | * Inspections for common obfuscation mistakes.
17 | * Optional (toggleable) gutter icons for obfuscated symbols.
18 | * Export configuration for [yGuard](http://www.yworks.com/en/products_yguard_about.html) and [ProGuard](http://proguard.sourceforge.net/) format.
19 |
20 |
21 | ### General usage
22 |
23 | Add Obfuscation facet to your Java module to get going,
24 | "Project Structure (CTRL+SHIFT+ALT+S)" or "Module Settings" in project explorer.
25 | Obfuscation actions will only be visible in an Obfuscation facet.
26 |
27 | You can build the jar file from your module in the "build" menu.
28 |
29 |
30 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/action/ExportYGuard.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.action;
18 |
19 | import com.github.intelliguard.facet.GuardFacet;
20 | import com.github.intelliguard.generator.YGuardGenerator;
21 | import org.jetbrains.annotations.NotNull;
22 |
23 | /**
24 | * Created by IntelliJ IDEA.
25 | * User: Ronnie
26 | * Date: 2009-nov-10
27 | * Time: 20:48:09
28 | */
29 | public class ExportYGuard extends AbstractExportAction
30 | {
31 | private static final String FILE_EXTENSION = "xml";
32 |
33 | protected String generateConfiguration(@NotNull GuardFacet guardFacet)
34 | {
35 | return YGuardGenerator.generateBuildXml(guardFacet);
36 | }
37 |
38 | @Override
39 | protected String getConfigFileExtension()
40 | {
41 | return FILE_EXTENSION;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/action/ExportProGuard.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.action;
18 |
19 | import org.jetbrains.annotations.NotNull;
20 | import com.github.intelliguard.facet.GuardFacet;
21 | import com.github.intelliguard.generator.ProGuardGenerator;
22 |
23 | /**
24 | * Created by IntelliJ IDEA.
25 | * User: Ronnie
26 | * Date: 2009-nov-10
27 | * Time: 20:54:51
28 | */
29 | public class ExportProGuard extends AbstractExportAction
30 | {
31 | private static final String FILE_EXTENSION = "pro";
32 |
33 | protected String generateConfiguration(@NotNull GuardFacet guardFacet)
34 | {
35 | return ProGuardGenerator.generatePro(guardFacet);
36 | }
37 |
38 | @Override
39 | protected String getConfigFileExtension()
40 | {
41 | return FILE_EXTENSION;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/util/ModuleUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.util;
18 |
19 | import org.jetbrains.annotations.Nullable;
20 |
21 | import java.io.File;
22 |
23 | import com.sun.istack.internal.NotNull;
24 | import com.intellij.openapi.module.Module;
25 | import com.intellij.openapi.compiler.CompilerPaths;
26 |
27 | /**
28 | * Created by IntelliJ IDEA.
29 | * User: Ronnie
30 | * Date: 2009-nov-02
31 | * Time: 18:21:38
32 | */
33 | public class ModuleUtils
34 | {
35 | @Nullable
36 | public static File getModuleOutputDir(@NotNull Module module)
37 | {
38 | String outputPath = CompilerPaths.getModuleOutputPath(module, false);
39 | if (outputPath != null)
40 | {
41 | return new File(outputPath);
42 | }
43 | return null;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/ui/ToolWindowPanel.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.ui;
18 |
19 | import com.github.intelliguard.runner.ProgressInfoReceiver;
20 |
21 | import javax.swing.*;
22 |
23 | /**
24 | * Created by IntelliJ IDEA.
25 | * User: Ronnie
26 | * Date: 2009-nov-10
27 | * Time: 19:30:30
28 | */
29 | public class ToolWindowPanel implements ProgressInfoReceiver
30 | {
31 | private static final String NL = System.getProperty("line.separator", "\n");
32 | private JPanel panel;
33 | private JTextArea textArea;
34 |
35 | public ToolWindowPanel()
36 | {
37 | clear();
38 | }
39 |
40 | public JPanel getPanel()
41 | {
42 | return panel;
43 | }
44 |
45 | public void info(String info)
46 | {
47 | textArea.append(info + NL);
48 | }
49 |
50 | public void clear()
51 | {
52 | textArea.setText("");
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/model/JarConfig.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.model;
18 |
19 | import java.util.List;
20 | import java.util.ArrayList;
21 |
22 | /**
23 | * Created by IntelliJ IDEA.
24 | * User: Ronnie
25 | * Date: 2009-nov-01
26 | * Time: 09:21:17
27 | */
28 | public class JarConfig
29 | {
30 | private String linkLibraries;
31 |
32 | private List jarEntries = new ArrayList();
33 |
34 | public String getLinkLibraries()
35 | {
36 | return linkLibraries;
37 | }
38 |
39 | public void setLinkLibraries(String linkLibraries)
40 | {
41 | this.linkLibraries = linkLibraries;
42 | }
43 |
44 | public List getJarEntries()
45 | {
46 | return jarEntries;
47 | }
48 |
49 | public void setJarEntries(List jarEntries)
50 | {
51 | this.jarEntries = jarEntries;
52 | }
53 |
54 | public void addEntry(String entry)
55 | {
56 | if (!jarEntries.contains(entry))
57 | {
58 | jarEntries.add(entry);
59 | }
60 | }
61 |
62 | public void removeEntry(String entry)
63 | {
64 | jarEntries.remove(entry);
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/action/ExportGroup.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.action;
18 |
19 | import com.intellij.openapi.actionSystem.*;
20 | import com.intellij.openapi.module.Module;
21 | import com.github.intelliguard.facet.GuardFacet;
22 | import org.jetbrains.annotations.Nullable;
23 |
24 | /**
25 | * Created by IntelliJ IDEA.
26 | * User: Ronnie
27 | * Date: 2009-nov-10
28 | * Time: 21:22:59
29 | */
30 | public class ExportGroup extends DefaultActionGroup
31 | {
32 | @Nullable
33 | protected Module getModule(AnActionEvent e)
34 | {
35 | return DataKeys.MODULE.getData(e.getDataContext());
36 | }
37 |
38 | @Nullable
39 | protected GuardFacet getGuardFacet(@Nullable Module module)
40 | {
41 | return module == null ? null : GuardFacet.getInstance(module);
42 | }
43 |
44 | @Override
45 | public void update(AnActionEvent e)
46 | {
47 | final Presentation presentation = e.getPresentation();
48 | final Module module = getModule(e);
49 | if (module != null)
50 | {
51 | final GuardFacet guardFacet = getGuardFacet(module);
52 | if (guardFacet != null)
53 | {
54 | presentation.setEnabled(true);
55 | return;
56 | }
57 | }
58 | presentation.setEnabled(false);
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/facet/GuardFacet.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.facet;
18 |
19 | import com.intellij.facet.Facet;
20 | import com.intellij.facet.FacetManager;
21 | import com.intellij.facet.FacetType;
22 | import com.intellij.facet.FacetTypeId;
23 | import com.intellij.facet.FacetTypeRegistry;
24 | import com.intellij.openapi.module.Module;
25 | import org.jetbrains.annotations.NotNull;
26 | import org.jetbrains.annotations.Nullable;
27 |
28 | /**
29 | * Created by IntelliJ IDEA.
30 | * User: Ronnie
31 | * Date: 2009-okt-21
32 | * Time: 19:03:44
33 | */
34 | public class GuardFacet extends Facet
35 | {
36 | public static final FacetTypeId ID = new FacetTypeId("IntelliGuard");
37 |
38 | public GuardFacet(@NotNull Module module) {
39 | this(FacetTypeRegistry.getInstance().findFacetType(ID), module, "IntelliGuard", new GuardFacetConfiguration(), null);
40 | }
41 |
42 | public GuardFacet(@NotNull FacetType facetType, @NotNull Module module, String name, @NotNull GuardFacetConfiguration configuration, Facet underlyingFacet)
43 | {
44 | super(facetType, module, name, configuration, underlyingFacet);
45 | }
46 |
47 | @Nullable
48 | public static GuardFacet getInstance(@NotNull Module module)
49 | {
50 | return FacetManager.getInstance(module).getFacetByType(ID);
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/ui/ToolWindowPanel.form:
--------------------------------------------------------------------------------
1 |
2 |
43 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/runner/RunProgress.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.runner;
18 |
19 | import com.intellij.openapi.progress.ProgressIndicator;
20 | import com.intellij.openapi.progress.ProgressManager;
21 | import org.jetbrains.annotations.Nullable;
22 |
23 | /**
24 | * Created by IntelliJ IDEA.
25 | * User: Ronnie
26 | * Date: 2009-nov-02
27 | * Time: 18:40:18
28 | */
29 | public class RunProgress
30 | {
31 | private ProgressInfoReceiver infoReceiver;
32 | private int errors;
33 |
34 | public RunProgress(@Nullable ProgressInfoReceiver infoReceiver)
35 | {
36 | this.infoReceiver = infoReceiver;
37 | }
38 |
39 | public void markError(@Nullable String errorMessage)
40 | {
41 | errors++;
42 | if (errorMessage != null)
43 | {
44 | markMessage("[ERROR] " + errorMessage);
45 | }
46 | }
47 |
48 | public boolean lookingGood()
49 | {
50 | return errors == 0;
51 | }
52 |
53 | public void markMessage(@Nullable String text)
54 | {
55 | if (text == null)
56 | {
57 | return;
58 | }
59 | if (infoReceiver != null)
60 | {
61 | infoReceiver.info(text);
62 | }
63 | ProgressIndicator progressIndicator = ProgressManager.getInstance().getProgressIndicator();
64 | if (progressIndicator != null)
65 | {
66 | progressIndicator.setText2(text);
67 | }
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/util/UiUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.util;
18 |
19 | import com.intellij.openapi.application.ApplicationManager;
20 | import com.intellij.openapi.wm.StatusBar;
21 | import com.intellij.openapi.wm.WindowManager;
22 | import com.intellij.openapi.wm.ToolWindowManager;
23 | import com.intellij.openapi.project.Project;
24 | import com.intellij.openapi.ui.MessageType;
25 | import com.github.intelliguard.GuardProjectComponent;
26 |
27 | /**
28 | * Created by IntelliJ IDEA.
29 | * User: Ronnie
30 | * Date: 2009-nov-01
31 | * Time: 16:42:23
32 | */
33 | public class UiUtils
34 | {
35 | public static void showInfoBallon(final Project project, final String text)
36 | {
37 | Runnable r = new Runnable()
38 | {
39 | public void run()
40 | {
41 | ToolWindowManager.getInstance(project).notifyByBalloon(GuardProjectComponent.TOOLWINDOW_ID, MessageType.INFO, text);
42 | }
43 | };
44 | ApplicationManager.getApplication().invokeLater(r);
45 | }
46 |
47 | public static void showErrorBallon(final Project project, final String text)
48 | {
49 | Runnable r = new Runnable()
50 | {
51 | public void run()
52 | {
53 | ToolWindowManager.getInstance(project).notifyByBalloon(GuardProjectComponent.TOOLWINDOW_ID, MessageType.ERROR, text);
54 | }
55 | };
56 | ApplicationManager.getApplication().invokeLater(r);
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/ui/MainClassChooser.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.ui;
18 |
19 | import com.intellij.ide.util.TreeClassChooser;
20 | import com.intellij.ide.util.TreeClassChooserFactory;
21 | import com.intellij.psi.PsiClass;
22 | import com.intellij.openapi.module.Module;
23 | import com.intellij.openapi.ui.TextFieldWithBrowseButton;
24 | import com.github.intelliguard.util.PsiUtils;
25 |
26 | import java.awt.event.ActionListener;
27 | import java.awt.event.ActionEvent;
28 |
29 | /**
30 | * Created by IntelliJ IDEA.
31 | * User: Ronnie
32 | * Date: 2009-dec-03
33 | * Time: 19:58:54
34 | */
35 | public class MainClassChooser implements ActionListener
36 | {
37 | private final Module module;
38 | private final TextFieldWithBrowseButton mainClass;
39 |
40 | public MainClassChooser(Module module, TextFieldWithBrowseButton mainClass)
41 | {
42 | this.module = module;
43 | this.mainClass = mainClass;
44 | }
45 |
46 | public void actionPerformed(ActionEvent e)
47 | {
48 | TreeClassChooser classChooser = TreeClassChooserFactory.getInstance(module.getProject()).createProjectScopeChooser("Choose Main-Class");
49 | classChooser.showDialog();
50 | PsiClass psiClass = classChooser.getSelected();
51 | if (psiClass != null)
52 | {
53 | String className = PsiUtils.getKeeperName(psiClass);
54 | // state.mainclass = className;
55 | mainClass.getTextField().setText(className);
56 | }
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/util/ObfuscatorUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.util;
18 |
19 | import com.intellij.facet.ui.ValidationResult;
20 | import com.intellij.openapi.vfs.VirtualFile;
21 | import com.intellij.openapi.vfs.JarFileSystem;
22 | import com.intellij.openapi.util.io.FileUtil;
23 | import org.jetbrains.annotations.Nullable;
24 | import org.jetbrains.annotations.NotNull;
25 |
26 | /**
27 | * Created by IntelliJ IDEA.
28 | * User: Ronnie
29 | * Date: 2009-nov-09
30 | * Time: 22:11:49
31 | */
32 | public class ObfuscatorUtils
33 | {
34 | public static ValidationResult checkYGuard(@NotNull final String jarPath)
35 | {
36 | final VirtualFile jarFile = findJarFile(jarPath);
37 | if (jarFile == null)
38 | {
39 | return new ValidationResult("File " + jarPath + " does not exist");
40 | }
41 |
42 | String yGuardClassName = "com.yworks.yguard.YGuardTask";
43 | final VirtualFile yGuardClassFile = jarFile.findFileByRelativePath(yGuardClassName.replace('.', '/') + ".class");
44 | if (yGuardClassFile == null)
45 | {
46 | return new ValidationResult(jarPath + " does not contain class " + yGuardClassName);
47 | }
48 | return ValidationResult.OK;
49 | }
50 |
51 | @Nullable
52 | private static VirtualFile findJarFile(@NotNull final String jarPath)
53 | {
54 | return JarFileSystem.getInstance().refreshAndFindFileByPath(FileUtil.toSystemIndependentName(jarPath) + JarFileSystem.JAR_SEPARATOR);
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/gutter/GuardMarkerEditorListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.gutter;
18 |
19 | import com.intellij.openapi.fileEditor.FileEditorManagerListener;
20 | import com.intellij.openapi.fileEditor.FileEditorManager;
21 | import com.intellij.openapi.fileEditor.FileEditorManagerEvent;
22 | import com.intellij.openapi.vfs.VirtualFile;
23 | import com.intellij.openapi.project.Project;
24 | import com.intellij.psi.PsiManager;
25 | import com.intellij.psi.PsiFile;
26 |
27 | /**
28 | * Created by IntelliJ IDEA.
29 | * User: Ronnie
30 | * Date: 2009-nov-09
31 | * Time: 20:30:20
32 | */
33 | public class GuardMarkerEditorListener implements FileEditorManagerListener
34 | {
35 | private final Project project;
36 |
37 | public GuardMarkerEditorListener(Project project)
38 | {
39 | this.project = project;
40 | }
41 |
42 | public void fileOpened(FileEditorManager source, VirtualFile file)
43 | {
44 | }
45 |
46 | public void fileClosed(FileEditorManager source, VirtualFile file)
47 | {
48 | }
49 |
50 | public void selectionChanged(FileEditorManagerEvent event)
51 | {
52 | // Refresh the GuardMarker gutter
53 | final VirtualFile virtualFile = event.getNewFile();
54 | if (virtualFile == null)
55 | {
56 | return;
57 | }
58 | final PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
59 | final GuardMarker marker = GuardMarker.getGuardMarker(psiFile);
60 | if (marker != null)
61 | {
62 | marker.refresh();
63 | }
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/ant/YProject.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.ant;
18 |
19 | import org.apache.tools.ant.Project;
20 | import org.apache.tools.ant.BuildListener;
21 | import org.apache.tools.ant.BuildEvent;
22 | import org.jetbrains.annotations.NotNull;
23 | import com.github.intelliguard.runner.RunProgress;
24 |
25 | /**
26 | * Created by IntelliJ IDEA.
27 | * User: Ronnie
28 | * Date: 2009-nov-08
29 | * Time: 12:06:48
30 | */
31 | public class YProject extends Project
32 | {
33 | public YProject(@NotNull final RunProgress runProgress)
34 | {
35 |
36 | addBuildListener(new BuildListener()
37 | {
38 | public void buildStarted(BuildEvent buildEvent)
39 | {
40 | }
41 |
42 | public void buildFinished(BuildEvent buildEvent)
43 | {
44 | }
45 |
46 | public void targetStarted(BuildEvent buildEvent)
47 | {
48 | }
49 |
50 | public void targetFinished(BuildEvent buildEvent)
51 | {
52 | }
53 |
54 | public void taskStarted(BuildEvent buildEvent)
55 | {
56 | }
57 |
58 | public void taskFinished(BuildEvent buildEvent)
59 | {
60 | }
61 |
62 | public void messageLogged(BuildEvent buildEvent)
63 | {
64 | final String message = buildEvent.getMessage();
65 | if (message != null)
66 | {
67 | runProgress.markMessage(message);
68 | }
69 | }
70 | });
71 | }
72 | }
73 |
74 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/GuardComponent.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard;
18 |
19 | import com.intellij.openapi.components.ApplicationComponent;
20 | import com.intellij.facet.FacetTypeRegistry;
21 | import com.intellij.codeInspection.InspectionToolProvider;
22 | import com.github.intelliguard.facet.GuardFacetType;
23 | import com.github.intelliguard.inspection.GuardInspection;
24 | import com.github.intelliguard.inspection.SerializationProblemsInspection;
25 | import com.github.intelliguard.inspection.PluginProblemsInspection;
26 | import com.github.intelliguard.inspection.ReflectionProblemsInspection;
27 | import org.jetbrains.annotations.NotNull;
28 |
29 | /**
30 | * Created by IntelliJ IDEA.
31 | * User: Ronnie
32 | * Date: 2009-okt-21
33 | * Time: 19:27:37
34 | */
35 | public class GuardComponent implements ApplicationComponent, InspectionToolProvider
36 | {
37 | public GuardComponent()
38 | {
39 | }
40 |
41 | public void initComponent()
42 | {
43 | FacetTypeRegistry.getInstance().registerFacetType(GuardFacetType.getInstance());
44 | }
45 |
46 | public void disposeComponent()
47 | {
48 | // TODO: insert component disposal logic here
49 | }
50 |
51 | @NotNull
52 | public String getComponentName()
53 | {
54 | return "GuardComponent";
55 | }
56 |
57 | public Class[] getInspectionClasses()
58 | {
59 | return new Class[] {
60 | GuardInspection.class,
61 | SerializationProblemsInspection.class,
62 | PluginProblemsInspection.class,
63 | ReflectionProblemsInspection.class
64 | };
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/ui/FormDialogWrapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.ui;
18 |
19 | import com.github.intelliguard.facet.GuardFacet;
20 | import com.intellij.openapi.ui.DialogBuilder;
21 | import org.jetbrains.annotations.NotNull;
22 | import org.jetbrains.annotations.Nullable;
23 |
24 | /**
25 | * Created by IntelliJ IDEA.
26 | * User: Ronnie
27 | * Date: 2009-okt-30
28 | * Time: 11:56:51
29 | */
30 | public class FormDialogWrapper
31 | {
32 | @Nullable
33 | public static JarOptionsForm showJarOptionsForm(@NotNull GuardFacet guardFacet)
34 | {
35 | DialogBuilder builder = new DialogBuilder(guardFacet.getModule().getProject());
36 | JarOptionsForm jarOptionsForm = new JarOptionsForm(guardFacet);
37 | builder.setCenterPanel(jarOptionsForm.getContentPane());
38 | builder.setTitle("Obfuscate Jar");
39 | builder.addOkAction().setText("Build");
40 | builder.addCancelAction().setText("Cancel");
41 |
42 | int res = builder.show();
43 |
44 | return res == 0 ? jarOptionsForm : null;
45 | }
46 |
47 | @Nullable
48 | public static ExportOptionsForm showExportOptionsForm(@NotNull GuardFacet guardFacet)
49 | {
50 | DialogBuilder builder = new DialogBuilder(guardFacet.getModule().getProject());
51 | ExportOptionsForm exportOptionsForm = new ExportOptionsForm(guardFacet);
52 | builder.setCenterPanel(exportOptionsForm.getContentPane());
53 | builder.setTitle("Export Configuration");
54 | builder.addOkAction().setText("Export");
55 | builder.addCancelAction().setText("Cancel");
56 |
57 | int res = builder.show();
58 |
59 | return res == 0 ? exportOptionsForm : null;
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/fix/AddKeepFix.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.fix;
18 |
19 | import com.intellij.codeInspection.LocalQuickFix;
20 | import com.intellij.codeInspection.ProblemDescriptor;
21 | import com.intellij.openapi.project.Project;
22 | import com.intellij.psi.PsiElement;
23 | import com.github.intelliguard.model.Keeper;
24 | import com.github.intelliguard.facet.GuardFacetConfiguration;
25 | import com.github.intelliguard.inspection.GuardInspectionBase;
26 | import org.jetbrains.annotations.NotNull;
27 |
28 | /**
29 | * Created by IntelliJ IDEA.
30 | * User: Ronnie
31 | * Date: 2009-nov-02
32 | * Time: 21:32:04
33 | */
34 | public class AddKeepFix implements LocalQuickFix
35 | {
36 | private final GuardFacetConfiguration configuration;
37 | private final Keeper keeper;
38 | private final PsiElement element;
39 |
40 | public AddKeepFix(@NotNull final GuardFacetConfiguration configuration, @NotNull final Keeper keeper, @NotNull final PsiElement element)
41 | {
42 | this.configuration = configuration;
43 | this.keeper = keeper;
44 | this.element = element;
45 | }
46 |
47 | @NotNull
48 | public String getName()
49 | {
50 | return "Keep " + keeper.getType().getName() + " from obfuscation" + (keeper.getType() == Keeper.Type.CLASS ? "" : (keeper.getClazz() == null ? " in all classes" : " in class " + keeper.getClazz()));
51 | }
52 |
53 | @NotNull
54 | public String getFamilyName()
55 | {
56 | return "Add guard family";
57 | }
58 |
59 | public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor)
60 | {
61 | configuration.keepers.add(keeper);
62 | GuardInspectionBase.alertGuardMarkers(element);
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/fix/RemoveKeepFix.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.fix;
18 |
19 | import com.intellij.codeInspection.LocalQuickFix;
20 | import com.intellij.codeInspection.ProblemDescriptor;
21 | import com.intellij.openapi.project.Project;
22 | import com.intellij.psi.PsiElement;
23 | import com.github.intelliguard.facet.GuardFacetConfiguration;
24 | import com.github.intelliguard.model.Keeper;
25 | import com.github.intelliguard.inspection.GuardInspectionBase;
26 | import org.jetbrains.annotations.NotNull;
27 |
28 | /**
29 | * Created by IntelliJ IDEA.
30 | * User: Ronnie
31 | * Date: 2009-nov-02
32 | * Time: 21:26:45
33 | */
34 | public class RemoveKeepFix implements LocalQuickFix
35 | {
36 | private final GuardFacetConfiguration configuration;
37 | private final Keeper keeper;
38 | private final PsiElement element;
39 |
40 | public RemoveKeepFix(@NotNull final GuardFacetConfiguration configuration, @NotNull final Keeper keeper, @NotNull final PsiElement element)
41 | {
42 | this.configuration = configuration;
43 | this.keeper = keeper;
44 | this.element = element;
45 | }
46 |
47 | @NotNull
48 | public String getName()
49 | {
50 | return "Remove " + keeper.getType().getName() + " keeper" + (keeper.getType() == Keeper.Type.CLASS ? "" : (keeper.getClazz() == null ? " in all classes" : " in class " + keeper.getClazz()));
51 | }
52 |
53 | @NotNull
54 | public String getFamilyName()
55 | {
56 | return "Remove guard family";
57 | }
58 |
59 | public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor)
60 | {
61 | configuration.keepers.remove(keeper);
62 | GuardInspectionBase.alertGuardMarkers(element);
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/gutter/GuardGutterRenderer.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.gutter;
18 |
19 | import com.intellij.openapi.editor.markup.*;
20 | import com.intellij.openapi.util.TextRange;
21 | import org.jetbrains.annotations.NotNull;
22 | import org.jetbrains.annotations.Nullable;
23 |
24 | import javax.swing.*;
25 |
26 | /**
27 | * User: ronnie
28 | * Date: 2009-nov-09
29 | * Time: 12:51:43
30 | */
31 | public class GuardGutterRenderer extends GutterIconRenderer
32 | {
33 | private Icon icon;
34 | @Nullable
35 | private String tooltip;
36 | private TextRange range;
37 |
38 | public GuardGutterRenderer(@NotNull Icon icon, @Nullable String tooltip, @NotNull TextRange range)
39 | {
40 | this.icon = icon;
41 | this.tooltip = tooltip;
42 | this.range = range;
43 | }
44 |
45 | @Override
46 | public String getTooltipText()
47 | {
48 | return tooltip != null ? tooltip : super.getTooltipText();
49 | }
50 |
51 | @NotNull
52 | public Icon getIcon()
53 | {
54 | return icon;
55 | }
56 |
57 | public RangeHighlighter addLineHighlighter(@NotNull MarkupModel markupModel)
58 | {
59 | return markupModel.addRangeHighlighter(range.getStartOffset(), range.getEndOffset(), HighlighterLayer.LAST, null, HighlighterTargetArea.LINES_IN_RANGE);
60 | }
61 |
62 | @Override
63 | public boolean equals(Object o) {
64 | if (this == o) return true;
65 | if (o == null || getClass() != o.getClass()) return false;
66 |
67 | GuardGutterRenderer that = (GuardGutterRenderer) o;
68 |
69 | if (range != null ? !range.equals(that.range) : that.range != null) return false;
70 |
71 | return true;
72 | }
73 |
74 | @Override
75 | public int hashCode() {
76 | return range != null ? range.hashCode() : 0;
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/action/AbstractGuardAction.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.action;
18 |
19 | import com.intellij.openapi.actionSystem.AnAction;
20 | import com.intellij.openapi.actionSystem.AnActionEvent;
21 | import com.intellij.openapi.actionSystem.DataKeys;
22 | import com.intellij.openapi.module.Module;
23 | import com.intellij.openapi.project.Project;
24 | import com.intellij.openapi.editor.Document;
25 | import com.intellij.openapi.editor.Editor;
26 | import com.github.intelliguard.facet.GuardFacet;
27 | import org.jetbrains.annotations.Nullable;
28 |
29 | /**
30 | * Created by IntelliJ IDEA.
31 | * User: Ronnie
32 | * Date: 2009-nov-10
33 | * Time: 21:09:18
34 | */
35 | public abstract class AbstractGuardAction extends AnAction
36 | {
37 | // just a bunch of helpers
38 |
39 | @Nullable
40 | protected Module getModule(AnActionEvent e)
41 | {
42 | return DataKeys.MODULE.getData(e.getDataContext());
43 | }
44 |
45 | @Nullable
46 | protected GuardFacet getGuardFacet(@Nullable Module module)
47 | {
48 | return module == null ? null : GuardFacet.getInstance(module);
49 | }
50 |
51 | @Nullable
52 | protected GuardFacet getGuardFacet(AnActionEvent e)
53 | {
54 | final Module module = getModule(e);
55 | return module == null ? null : GuardFacet.getInstance(module);
56 | }
57 |
58 | @Nullable
59 | protected Project getProject(AnActionEvent e)
60 | {
61 | return DataKeys.PROJECT.getData(e.getDataContext());
62 | }
63 |
64 | @Nullable
65 | protected Document getDocument(AnActionEvent e)
66 | {
67 | final Editor editor = getEditor(e);
68 | return editor == null ? null : editor.getDocument();
69 | }
70 |
71 | @Nullable
72 | private Editor getEditor(AnActionEvent e)
73 | {
74 | return DataKeys.EDITOR.getData(e.getDataContext());
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/facet/GuardFacetEditorTab.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.facet;
18 |
19 | import com.intellij.facet.ui.FacetEditorTab;
20 | import com.intellij.facet.ui.FacetEditorContext;
21 | import com.intellij.facet.ui.FacetValidatorsManager;
22 | import com.intellij.openapi.options.ConfigurationException;
23 | import com.github.intelliguard.ui.YFacetConfigurationForm;
24 | import org.jetbrains.annotations.Nls;
25 |
26 | import javax.swing.*;
27 |
28 | /**
29 | * Created by IntelliJ IDEA.
30 | * User: Ronnie
31 | * Date: 2009-okt-21
32 | * Time: 19:08:51
33 | */
34 | public class GuardFacetEditorTab extends FacetEditorTab
35 | {
36 | private GuardFacetConfiguration originalState;
37 | private GuardFacetConfiguration currentState;
38 | private YFacetConfigurationForm yFacetConfigurationForm;
39 |
40 | public GuardFacetEditorTab(GuardFacetConfiguration originalState, FacetEditorContext editorContext, FacetValidatorsManager validatorsManager)
41 | {
42 | this.originalState = originalState;
43 | this.currentState = new GuardFacetConfiguration();
44 | this.currentState.loadState(originalState);
45 | yFacetConfigurationForm = new YFacetConfigurationForm(editorContext, validatorsManager, currentState);
46 | }
47 |
48 | @Nls
49 | public String getDisplayName()
50 | {
51 | return "IntelliGuard Configuration";
52 | }
53 |
54 | public JComponent createComponent()
55 | {
56 | return yFacetConfigurationForm.getPanel();
57 | }
58 |
59 | public boolean isModified()
60 | {
61 | return !currentState.equalsGlobalSettings(originalState);
62 | }
63 |
64 | public void apply() throws ConfigurationException
65 | {
66 | originalState.applyGlobalSettings(currentState);
67 | }
68 |
69 | public void reset()
70 | {
71 | }
72 |
73 | public void disposeUIResources()
74 | {
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/runner/ObfuscateTask.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.runner;
18 |
19 | import com.github.intelliguard.ant.YProject;
20 | import com.github.intelliguard.ant.YProjectHelper;
21 | import com.github.intelliguard.facet.GuardFacet;
22 | import com.github.intelliguard.generator.YGuardGenerator;
23 | import org.apache.tools.ant.BuildException;
24 | import org.apache.tools.ant.Project;
25 | import org.apache.tools.ant.ProjectHelper;
26 | import org.jetbrains.annotations.NotNull;
27 |
28 | import java.io.*;
29 |
30 | /**
31 | * Created by IntelliJ IDEA.
32 | * User: Ronnie
33 | * Date: 2009-nov-02
34 | * Time: 18:37:23
35 | */
36 | public class ObfuscateTask implements Runnable
37 | {
38 | private RunProgress runProgress;
39 | private GuardFacet guardFacet;
40 |
41 | public ObfuscateTask(@NotNull final RunProgress runProgress, @NotNull final GuardFacet guardFacet)
42 | {
43 | this.runProgress = runProgress;
44 | this.guardFacet = guardFacet;
45 | }
46 |
47 | public void run()
48 | {
49 |
50 | final String buildXml = YGuardGenerator.generateBuildXml(guardFacet);
51 | final Project project = new YProject(runProgress);
52 | final ProjectHelper projectHelper = new YProjectHelper();
53 | projectHelper.parse(project, new ByteArrayInputStream(buildXml.getBytes()));
54 | project.init();
55 |
56 | try
57 | {
58 | project.executeTarget(YGuardGenerator.YGUARD_TARGET_NAME);
59 | }
60 | catch (BuildException e)
61 | {
62 | runProgress.markError(e.getMessage());
63 | }
64 |
65 | /* // DEBUG
66 |
67 | File outFile = new File(guardFacet.getConfiguration().outFile);
68 | outFile = new File(outFile.getParent(), "yguard.xml");
69 |
70 | try (
71 | FileOutputStream fos = new FileOutputStream(outFile)
72 | ){
73 | PrintWriter writer = new PrintWriter(fos);
74 | writer.print(buildXml);
75 | writer.close();
76 | } catch (IOException e) {
77 | e.printStackTrace();
78 | }
79 | */
80 |
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/facet/GuardFacetType.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.facet;
18 |
19 | import com.intellij.facet.FacetType;
20 | import com.intellij.facet.Facet;
21 | import com.intellij.openapi.module.Module;
22 | import com.intellij.openapi.module.ModuleType;
23 | import com.github.intelliguard.ui.Icons;
24 | import org.jetbrains.annotations.NotNull;
25 | import org.jetbrains.annotations.Nullable;
26 |
27 | import javax.swing.*;
28 |
29 | /**
30 | * Created by IntelliJ IDEA.
31 | * User: Ronnie
32 | * Date: 2009-okt-21
33 | * Time: 19:06:57
34 | */
35 | public class GuardFacetType extends FacetType
36 | {
37 | private static final GuardFacetType instance = new GuardFacetType();
38 | private static final String JAVA_MODULE = "JAVA_MODULE";
39 | private static final String PLUGIN_MODULE = "PLUGIN_MODULE";
40 | private static final String J2ME_MODULE = "J2ME_MODULE";
41 | private static final String ANDROID_MODULE = "ANDROID_MODULE";
42 |
43 | private GuardFacetType()
44 | {
45 | super(GuardFacet.ID, "IntelliGuard", "Obfuscation");
46 | }
47 |
48 | public static GuardFacetType getInstance()
49 | {
50 | return instance;
51 | }
52 |
53 | @Override
54 | public Icon getIcon()
55 | {
56 | return Icons.OBFUSCATION_NODE_ICON;
57 | }
58 |
59 | public GuardFacetConfiguration createDefaultConfiguration()
60 | {
61 | return new GuardFacetConfiguration();
62 | }
63 |
64 | public GuardFacet createFacet(@NotNull Module module, String name, @NotNull GuardFacetConfiguration configuration, @Nullable Facet underlyingFacet)
65 | {
66 | return new GuardFacet(getInstance(), module, name, configuration, underlyingFacet);
67 | }
68 |
69 | public boolean isSuitableModuleType(ModuleType moduleType)
70 | {
71 | if (moduleType == null)
72 | {
73 | return false;
74 | }
75 | final String moduleId = moduleType.getId();
76 | return J2ME_MODULE.equals(moduleId) || JAVA_MODULE.equals(moduleId) || PLUGIN_MODULE.equals(moduleId) || ANDROID_MODULE.equals(moduleId);
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/action/GutterAction.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.action;
18 |
19 | import com.github.intelliguard.facet.GuardFacet;
20 | import com.github.intelliguard.gutter.GuardMarker;
21 | import com.github.intelliguard.ui.Icons;
22 | import com.intellij.openapi.actionSystem.AnActionEvent;
23 | import com.intellij.openapi.actionSystem.Presentation;
24 | import com.intellij.openapi.editor.Document;
25 | import com.intellij.openapi.project.Project;
26 | import com.intellij.psi.PsiDocumentManager;
27 | import com.intellij.psi.PsiFile;
28 |
29 | /**
30 | * User: ronnie
31 | * Date: 2009-nov-09
32 | * Time: 16:28:07
33 | */
34 | public class GutterAction extends AbstractGuardAction
35 | {
36 | @Override
37 | public void update(AnActionEvent e)
38 | {
39 | final Presentation presentation = e.getPresentation();
40 | final GuardFacet guardFacet = getGuardFacet(e);
41 | if (guardFacet == null)
42 | {
43 | presentation.setVisible(false);
44 | }
45 | else
46 | {
47 | final Document document = getDocument(e);
48 | if (document == null)
49 | {
50 | presentation.setVisible(false);
51 | }
52 | else
53 | {
54 | presentation.setVisible(true);
55 | presentation.setIcon(Icons.OBFUSCATION_NODE_ICON);
56 | final GuardMarker marker = document.getUserData(GuardMarker.KEY);
57 | if (marker == null)
58 | {
59 | presentation.setText("Show Obfuscated symbols");
60 | }
61 | else
62 | {
63 | presentation.setText("Hide Obfuscated symbols");
64 | }
65 | }
66 | }
67 | }
68 |
69 | public void actionPerformed(AnActionEvent e)
70 | {
71 | final Document document = getDocument(e);
72 | if (document == null)
73 | {
74 | return;
75 | }
76 | final Project project = getProject(e);
77 | if (project == null)
78 | {
79 | return;
80 | }
81 | final PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document);
82 | final GuardMarker marker = document.getUserData(GuardMarker.KEY);
83 | if (marker == null)
84 | {
85 | GuardMarker.createMarkers(psiFile);
86 | }
87 | else
88 | {
89 | GuardMarker.clearMarkers(psiFile);
90 | }
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/src/main/resources/META-INF/plugin.xml:
--------------------------------------------------------------------------------
1 |
2 | IntelliGuard 2
3 |
4 | Java bytecode obfuscation made easy.
6 | Seamless integration of yGuard in
7 | Intellij IDEA. Add Obfuscation facet to your Java module and choose which symbols to keep from obfuscation
8 | with IDEA inspections.
9 | Key features:
10 |
11 | - No more hazzles with configuration files. Just hit ALT+ENTER on any symbol in the editor for keep options.
12 | - Create obfuscated archive directly from the IDE.
13 | - Refactor support. Kept symbols remain unobfuscated after rename or move.
14 | - Inspections for common obfuscation pitfalls.
15 | - Optional (toggleable) gutter icons for obfuscated symbols.
16 | - Export configuration for yGuard
17 | and ProGuard format.
18 |
19 |
20 | Project home: https://github.com/iGufGuf/intelliguard
21 | ]]>
22 |
23 | 2.0.1
24 |
25 |
26 |
27 | iGufGuf
28 |
29 | 2.0.1
32 | Added property "naming-scheme" and "language-conformity" to the ui
33 |
34 |
35 | 2.0
36 | - Build for intellij IDEA 2018.2
37 | - Fixed icon issue
38 | - Migrate from deprecated intellij openapi
39 |
40 |
41 | ]]>
42 |
43 |
44 |
45 | com.github.intelliguard.GuardProjectComponent
46 |
47 |
48 |
49 |
50 |
51 | com.github.intelliguard.GuardComponent
52 |
53 |
54 |
55 |
56 |
59 |
60 |
61 |
63 |
64 |
65 |
69 |
72 |
73 |
74 |
77 |
78 |
79 |
80 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/ui/ExportOptionsForm.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.ui;
18 |
19 | import com.intellij.openapi.ui.TextFieldWithBrowseButton;
20 | import com.intellij.openapi.module.Module;
21 | import com.github.intelliguard.facet.GuardFacetConfiguration;
22 | import com.github.intelliguard.facet.GuardFacet;
23 |
24 | import javax.swing.*;
25 | import java.awt.event.ActionListener;
26 | import java.awt.event.ActionEvent;
27 |
28 | import org.jetbrains.annotations.NotNull;
29 |
30 | /**
31 | * Created by IntelliJ IDEA.
32 | * User: Ronnie
33 | * Date: 2009-dec-03
34 | * Time: 19:49:59
35 | */
36 | public class ExportOptionsForm
37 | {
38 | private JPanel contentPane;
39 | private TextFieldWithBrowseButton mainClass;
40 | private TextFieldWithBrowseButton obfuscatedJarPath;
41 | private TextFieldWithBrowseButton jarPath;
42 |
43 | public ExportOptionsForm(@NotNull GuardFacet guardFacet)
44 | {
45 | final Module module = guardFacet.getModule();
46 | final GuardFacetConfiguration facetConfiguration = guardFacet.getConfiguration();
47 |
48 | mainClass.getTextField().setText(facetConfiguration.mainclass == null ? "" : facetConfiguration.mainclass);
49 | mainClass.addActionListener(new MainClassChooser(module, mainClass));
50 |
51 | jarPath.getTextField().setText(facetConfiguration.inFile == null ? "" : facetConfiguration.inFile);
52 | jarPath.addActionListener(new ActionListener()
53 | {
54 | public void actionPerformed(ActionEvent e)
55 | {
56 | JFileChooser chooser = FileChooserFactory.createSaveJarChooser(jarPath.getText(), module.getModuleFilePath());
57 | int res = chooser.showSaveDialog(contentPane);
58 | if (res == JFileChooser.APPROVE_OPTION && chooser.getFileFilter().accept(chooser.getSelectedFile()))
59 | {
60 | jarPath.getTextField().setText(chooser.getSelectedFile().getAbsolutePath());
61 | }
62 | }
63 | });
64 |
65 | obfuscatedJarPath.getTextField().setText(facetConfiguration.outFile == null ? "" : facetConfiguration.outFile);
66 | obfuscatedJarPath.addActionListener(new ActionListener()
67 | {
68 | public void actionPerformed(ActionEvent e)
69 | {
70 | JFileChooser chooser = FileChooserFactory.createSaveJarChooser(obfuscatedJarPath.getText(), jarPath.getText(), module.getModuleFilePath());
71 | int res = chooser.showSaveDialog(contentPane);
72 | if (res == JFileChooser.APPROVE_OPTION && chooser.getFileFilter().accept(chooser.getSelectedFile()))
73 | {
74 | obfuscatedJarPath.getTextField().setText(chooser.getSelectedFile().getAbsolutePath());
75 | }
76 | }
77 | });
78 | }
79 |
80 | public JPanel getContentPane()
81 | {
82 | return contentPane;
83 | }
84 |
85 | public String getJarPath()
86 | {
87 | return jarPath.getText();
88 | }
89 |
90 | public String getObfuscatedJarPath()
91 | {
92 | return obfuscatedJarPath.getText();
93 | }
94 |
95 | public String getMainClass()
96 | {
97 | return mainClass.getText();
98 | }
99 | }
100 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/ui/ExportOptionsForm.form:
--------------------------------------------------------------------------------
1 |
2 |
73 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/ant/YProjectHelper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.ant;
18 |
19 | import org.apache.tools.ant.helper.ProjectHelper2;
20 | import org.apache.tools.ant.Project;
21 | import org.apache.tools.ant.BuildException;
22 | import org.apache.tools.ant.Location;
23 | import org.apache.tools.ant.util.JAXPUtils;
24 | import org.xml.sax.*;
25 | import org.xml.sax.helpers.DefaultHandler;
26 |
27 | import java.io.*;
28 |
29 | /**
30 | * A slightly modified version of {@link ProjectHelper2} in the way
31 | * that it only deals with in-memory build files.
32 | */
33 | public class YProjectHelper extends ProjectHelper2
34 | {
35 | /**
36 | * Only parses java.io.InputStream.
37 | * @param project the project
38 | * @param source the input stream
39 | * @param handler the handler
40 | * @throws BuildException if source is not of type java.io.InputStream
41 | */
42 | @Override
43 | public void parse(Project project, Object source, RootHandler handler) throws BuildException
44 | {
45 | if (!(source instanceof InputStream))
46 | {
47 | throw new BuildException("Only InpuStream source supported");
48 | }
49 |
50 | InputStream inputStream = (InputStream) source;
51 | InputSource inputSource = null;
52 |
53 |
54 | try {
55 | /**
56 | * SAX 2 style parser used to parse the given file.
57 | */
58 | XMLReader parser = JAXPUtils.getNamespaceXMLReader();
59 |
60 | inputSource = new InputSource(inputStream);
61 | project.log("parsing inputstream", Project.MSG_VERBOSE);
62 |
63 | DefaultHandler hb = handler;
64 |
65 | parser.setContentHandler(hb);
66 | parser.setEntityResolver(hb);
67 | parser.setErrorHandler(hb);
68 | parser.setDTDHandler(hb);
69 | parser.parse(inputSource);
70 | } catch (SAXParseException exc) {
71 | Location location = new Location(exc.getSystemId(),
72 | exc.getLineNumber(), exc.getColumnNumber());
73 |
74 | Throwable t = exc.getException();
75 | if (t instanceof BuildException) {
76 | BuildException be = (BuildException) t;
77 | if (be.getLocation() == Location.UNKNOWN_LOCATION) {
78 | be.setLocation(location);
79 | }
80 | throw be;
81 | }
82 |
83 | throw new BuildException(exc.getMessage(), t, location);
84 | } catch (SAXException exc) {
85 | Throwable t = exc.getException();
86 | if (t instanceof BuildException) {
87 | throw (BuildException) t;
88 | }
89 | throw new BuildException(exc.getMessage(), t);
90 | } catch (FileNotFoundException exc) {
91 | throw new BuildException(exc);
92 | } catch (UnsupportedEncodingException exc) {
93 | throw new BuildException("Encoding of project file is invalid.",
94 | exc);
95 | } catch (IOException exc) {
96 | throw new BuildException("Error reading project file: " + exc.getMessage(),
97 | exc);
98 | } finally {
99 | try {
100 | inputStream.close();
101 | } catch (IOException ioe) {
102 | // ignore this
103 | }
104 | }
105 | }
106 | }
107 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/gutter/GuardGutterRendererComputation.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.gutter;
18 |
19 | import com.github.intelliguard.facet.GuardFacet;
20 | import com.github.intelliguard.ui.Icons;
21 | import com.github.intelliguard.util.InspectionUtils;
22 | import com.intellij.openapi.editor.Document;
23 | import com.intellij.openapi.module.Module;
24 | import com.intellij.openapi.module.ModuleUtil;
25 | import com.intellij.openapi.util.Computable;
26 | import com.intellij.psi.JavaRecursiveElementVisitor;
27 | import com.intellij.psi.PsiClass;
28 | import com.intellij.psi.PsiElement;
29 | import com.intellij.psi.PsiField;
30 | import com.intellij.psi.PsiFile;
31 | import com.intellij.psi.PsiMethod;
32 |
33 | import java.util.ArrayList;
34 | import java.util.Collections;
35 | import java.util.List;
36 |
37 | import org.jetbrains.annotations.NotNull;
38 |
39 | /**
40 | * User: ronnie
41 | * Date: 2009-nov-09
42 | * Time: 12:55:44
43 | */
44 | public class GuardGutterRendererComputation implements Computable>
45 | {
46 | private PsiFile psiFile;
47 |
48 | public GuardGutterRendererComputation(@NotNull PsiFile psiFile)
49 | {
50 | this.psiFile = psiFile;
51 | }
52 |
53 | public List compute()
54 | {
55 | final List guardGutterRenderers = new ArrayList();
56 | final Document document = psiFile.getViewProvider().getDocument();
57 | if (document == null)
58 | {
59 | return Collections.emptyList();
60 | }
61 | final Module module = ModuleUtil.findModuleForPsiElement(psiFile);
62 | if (module == null)
63 | {
64 | return Collections.emptyList();
65 | }
66 | final GuardFacet guardFacet = GuardFacet.getInstance(module);
67 | if (guardFacet == null)
68 | {
69 | return Collections.emptyList();
70 | }
71 |
72 | psiFile.accept(new JavaRecursiveElementVisitor()
73 | {
74 | @Override
75 | public void visitClass(PsiClass psiClass)
76 | {
77 | checkElement(psiClass, "class");
78 | super.visitClass(psiClass);
79 | }
80 |
81 | @Override
82 | public void visitField(PsiField psiField)
83 | {
84 | checkElement(psiField, "field");
85 | super.visitField(psiField);
86 | }
87 |
88 | @Override
89 | public void visitMethod(PsiMethod psiMethod)
90 | {
91 | if (!InspectionUtils.isDefinedInLibrary(psiMethod))
92 | {
93 | checkElement(psiMethod, "method");
94 | }
95 | super.visitMethod(psiMethod);
96 | }
97 |
98 | private void checkElement(PsiElement element, String type)
99 | {
100 | if (guardFacet.getConfiguration().findConfiguredGuardKeepers(element).length != 0)
101 | {
102 | return;
103 | }
104 | if (guardFacet.getConfiguration().isKeptByMainClass(element))
105 | {
106 | return;
107 | }
108 | // no keeper
109 | final PsiElement nameIdentifierElement = InspectionUtils.getNameIdentifierElement(element);
110 | GuardGutterRenderer gradeGutterRenderer = new GuardGutterRenderer(Icons.OBFUSCATION_NODE_ICON, "Obfuscated " + type, nameIdentifierElement.getTextRange());
111 | guardGutterRenderers.add(gradeGutterRenderer);
112 | }
113 | });
114 |
115 | return guardGutterRenderers;
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/GuardProjectComponent.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard;
18 |
19 | import com.github.intelliguard.refactor.RenameListenerProvider;
20 | import com.github.intelliguard.ui.Icons;
21 | import com.github.intelliguard.ui.ToolWindowPanel;
22 | import com.github.intelliguard.gutter.GuardMarkerEditorListener;
23 | import com.github.intelliguard.runner.ProgressInfoReceiver;
24 | import com.intellij.openapi.components.ProjectComponent;
25 | import com.intellij.openapi.project.Project;
26 | import com.intellij.openapi.wm.ToolWindow;
27 | import com.intellij.openapi.wm.ToolWindowAnchor;
28 | import com.intellij.openapi.wm.ToolWindowManager;
29 | import com.intellij.openapi.fileEditor.FileEditorManagerListener;
30 | import com.intellij.refactoring.listeners.RefactoringListenerManager;
31 | import com.intellij.util.messages.MessageBusConnection;
32 | import com.intellij.ui.content.Content;
33 | import com.intellij.ui.content.ContentFactory;
34 | import org.jetbrains.annotations.NotNull;
35 |
36 | /**
37 | * Created by IntelliJ IDEA.
38 | * User: Ronnie
39 | * Date: 2009-nov-03
40 | * Time: 18:47:48
41 | */
42 | public class GuardProjectComponent implements ProjectComponent
43 | {
44 | public static final String TOOLWINDOW_ID = "IntelliGuard";
45 |
46 | private Project project;
47 | private ToolWindow toolWindow;
48 | private ToolWindowPanel toolWindowPanel;
49 | private RenameListenerProvider renameListenerProvider;
50 | private MessageBusConnection messageBusConnection;
51 |
52 | public GuardProjectComponent(Project project)
53 | {
54 | this.project = project;
55 | }
56 |
57 | public void initComponent()
58 | {
59 | // TODO: insert component initialization logic here
60 | }
61 |
62 | public void disposeComponent()
63 | {
64 | // TODO: insert component disposal logic here
65 | }
66 |
67 | @NotNull
68 | public String getComponentName()
69 | {
70 | return "GuardProjectComponent";
71 | }
72 |
73 | public ProgressInfoReceiver createProgressInfoReceiver()
74 | {
75 | toolWindow.setAvailable(true, null);
76 | toolWindowPanel.clear();
77 | return toolWindowPanel;
78 | }
79 |
80 | public void projectOpened()
81 | {
82 | final ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);
83 | toolWindow = toolWindowManager.registerToolWindow(TOOLWINDOW_ID, true, ToolWindowAnchor.BOTTOM);
84 | toolWindowPanel = new ToolWindowPanel();
85 |
86 | final ContentFactory contentFactory = toolWindow.getContentManager().getFactory();
87 | final Content content = contentFactory.createContent(toolWindowPanel.getPanel(), "", true);
88 |
89 | toolWindow.getContentManager().addContent(content);
90 | toolWindow.setIcon(Icons.OBFUSCATION_NODE_ICON);
91 | toolWindow.setAutoHide(false);
92 | toolWindow.setAvailable(false, null);
93 |
94 | final RefactoringListenerManager manager = RefactoringListenerManager.getInstance(project);
95 | renameListenerProvider = new RenameListenerProvider();
96 | manager.addListenerProvider(renameListenerProvider);
97 |
98 | messageBusConnection = project.getMessageBus().connect();
99 | messageBusConnection.subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, new GuardMarkerEditorListener(project));
100 | }
101 |
102 | public void projectClosed()
103 | {
104 | ToolWindowManager.getInstance(project).unregisterToolWindow(TOOLWINDOW_ID);
105 |
106 | final RefactoringListenerManager manager = RefactoringListenerManager.getInstance(project);
107 | manager.removeListenerProvider(renameListenerProvider);
108 |
109 | messageBusConnection.disconnect();
110 | }
111 | }
112 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/ui/FileChooserFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.ui;
18 |
19 | import com.intellij.openapi.module.Module;
20 |
21 | import javax.swing.*;
22 | import javax.swing.filechooser.FileFilter;
23 | import java.io.File;
24 |
25 | import org.jetbrains.annotations.NotNull;
26 |
27 | /**
28 | * Created by IntelliJ IDEA.
29 | * User: Ronnie
30 | * Date: 2009-nov-01
31 | * Time: 11:22:50
32 | */
33 | public class FileChooserFactory
34 | {
35 | /**
36 | * Creates a filechooser for saving jar files.
37 | * @param preferredDirectory the preferred 'current' directory/directories, in order. The first
38 | * usable directory will be used as current directory.
39 | * @return a filechooser instance
40 | */
41 | public static JFileChooser createSaveJarChooser(String... preferredDirectory)
42 | {
43 | JFileChooser jFileChooser = new JFileChooser();
44 | FileFilter fileFilter = new FileFilter()
45 | {
46 | @Override
47 | public boolean accept(File f)
48 | {
49 | return f.isDirectory() || f.getName().endsWith(".jar");
50 | }
51 |
52 | @Override
53 | public String getDescription()
54 | {
55 | return "Archive files";
56 | }
57 | };
58 | jFileChooser.setFileFilter(fileFilter);
59 |
60 | setPreferedDirectory(jFileChooser, preferredDirectory);
61 |
62 | jFileChooser.setDialogTitle("Save Jar File");
63 |
64 | return jFileChooser;
65 | }
66 |
67 | public static JFileChooser createPreferredDirectoryFileChooser(String dialogTitle, String... preferredDirectory)
68 | {
69 | JFileChooser jFileChooser = new JFileChooser();
70 |
71 | setPreferedDirectory(jFileChooser, preferredDirectory);
72 |
73 | jFileChooser.setDialogTitle(dialogTitle);
74 |
75 | return jFileChooser;
76 | }
77 |
78 | private static void setPreferedDirectory(JFileChooser jFileChooser, String... preferredDirectory)
79 | {
80 | for (String f : preferredDirectory)
81 | {
82 | File file = new File(f);
83 | if (file.exists())
84 | {
85 | jFileChooser.setCurrentDirectory(file);
86 | return;
87 | }
88 | }
89 | }
90 |
91 | public static JFileChooser createFindJarChooser(String... preferredDirectory)
92 | {
93 | final JFileChooser jFileChooser = createSaveJarChooser(preferredDirectory);
94 | jFileChooser.setDialogTitle("Open Jar File");
95 | return jFileChooser;
96 | }
97 |
98 | /**
99 | * Creates a filechooser for files and directories within the module.
100 | * The filechooser created prohibits ascending to a directory above the module directory.
101 | * @param module the module
102 | * @return a filechooser instance
103 | */
104 | public static JFileChooser createModuleFileChooser(@NotNull final Module module)
105 | {
106 | String moduleFilePath = module.getModuleFilePath();
107 | final File moduleDirectory = new File(moduleFilePath).getParentFile();
108 | JFileChooser jFileChooser = new JFileChooser()
109 | {
110 | @Override
111 | public void changeToParentDirectory()
112 | {
113 | if (getCurrentDirectory().equals(moduleDirectory))
114 | {
115 | return;
116 | }
117 | super.changeToParentDirectory();
118 | }
119 | };
120 |
121 | jFileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
122 | jFileChooser.setDialogTitle("Choose File or Directory");
123 | jFileChooser.setCurrentDirectory(moduleDirectory);
124 |
125 | return jFileChooser;
126 | }
127 |
128 | }
129 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/util/InspectionUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.util;
18 |
19 | import org.jetbrains.annotations.NotNull;
20 | import org.jetbrains.annotations.Nullable;
21 | import com.intellij.psi.PsiElement;
22 | import com.intellij.psi.PsiNameIdentifierOwner;
23 | import com.intellij.psi.PsiMethod;
24 | import com.intellij.psi.PsiClass;
25 | import com.intellij.openapi.module.Module;
26 | import com.intellij.openapi.module.ModuleUtil;
27 |
28 | import java.util.Collection;
29 | import java.util.ArrayList;
30 |
31 | /**
32 | * Created by IntelliJ IDEA.
33 | * User: Ronnie
34 | * Date: 2009-nov-09
35 | * Time: 18:44:16
36 | */
37 | public class InspectionUtils
38 | {
39 | @NotNull
40 | public static PsiElement getNameIdentifierElement(@NotNull PsiElement owner)
41 | {
42 | if (owner instanceof PsiNameIdentifierOwner)
43 | {
44 | PsiElement identifierElement = ((PsiNameIdentifierOwner) owner).getNameIdentifier();
45 | if (identifierElement != null)
46 | {
47 | return identifierElement;
48 | }
49 | }
50 | return owner;
51 | }
52 |
53 | public static boolean isDefinedInLibrary(@NotNull PsiMethod method)
54 | {
55 | final PsiMethod[] superMethods = method.findDeepestSuperMethods();
56 | if (superMethods.length != 0)
57 | {
58 | for (PsiMethod superMethod : superMethods)
59 | {
60 | final Module superModule = ModuleUtil.findModuleForPsiElement(superMethod);
61 | if (superModule == null || !superModule.equals(ModuleUtil.findModuleForPsiElement(method)))
62 | {
63 | return true;
64 | }
65 | }
66 | }
67 |
68 | return false;
69 | }
70 |
71 | /**
72 | * Find every superclass until (but not inclusive) java.lang.Object.
73 | * @param aClass starting class
74 | * @return a collection of classes not including neither
75 | * the starting class nor the ending class java.jang.Object
76 | */
77 | @NotNull
78 | public static Collection resolveAllSuperClasses(@NotNull PsiClass aClass)
79 | {
80 | Collection superClasses = new ArrayList();
81 | collectSuperClasses(superClasses, aClass);
82 | return superClasses;
83 | }
84 |
85 | private static void collectSuperClasses(Collection collector, PsiClass aClass)
86 | {
87 | final PsiClass superClass = aClass.getSuperClass();
88 | if (superClass != null)
89 | {
90 | final String name = superClass.getQualifiedName();
91 | if ("java.lang.Object".equals(name))
92 | {
93 | return;
94 | }
95 | collector.add(superClass);
96 | collectSuperClasses(collector, superClass);
97 | }
98 | }
99 |
100 | /**
101 | * Find every interface implemented by a given class and it's superclasses (if given).
102 | * @param aClass starting class
103 | * @param superClasses the superclasses to include in search - may be null or empty
104 | * @return a collection containing every interface implemented
105 | */
106 | @NotNull
107 | public static Collection resolveInterfaces(@NotNull PsiClass aClass, @Nullable Collection superClasses)
108 | {
109 | Collection collector = new ArrayList();
110 | for (PsiClass iface : aClass.getInterfaces())
111 | {
112 | collectInterfaces(collector, iface);
113 | }
114 |
115 | if (superClasses != null)
116 | {
117 | for (PsiClass superClass : superClasses)
118 | {
119 | collector.addAll(resolveInterfaces(superClass, null));
120 | }
121 | }
122 |
123 | return collector;
124 | }
125 |
126 | private static void collectInterfaces(@NotNull Collection collector, @NotNull PsiClass anInterface)
127 | {
128 | collector.add(anInterface);
129 | PsiClass[] interfaces = anInterface.getInterfaces();
130 | if (interfaces != null)
131 | {
132 | for (PsiClass iface : interfaces)
133 | {
134 | collectInterfaces(collector, iface);
135 | }
136 | }
137 | }
138 |
139 | }
140 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/model/Keeper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.model;
18 |
19 | import com.intellij.psi.PsiElement;
20 | import com.intellij.psi.PsiField;
21 | import com.intellij.psi.PsiMethod;
22 | import com.intellij.psi.PsiClass;
23 | import com.github.intelliguard.util.PsiUtils;
24 |
25 | import java.text.MessageFormat;
26 |
27 | /**
28 | * Created by IntelliJ IDEA.
29 | * User: Ronnie
30 | * Date: 2009-okt-27
31 | * Time: 20:49:32
32 | */
33 | public class Keeper
34 | {
35 | public enum Type
36 | {
37 | CLASS { public String getName() { return "class"; } },
38 | METHOD { public String getName() { return "method"; } },
39 | FIELD { public String getName() { return "field"; } };
40 |
41 | public abstract String getName();
42 | }
43 |
44 | // mandatory
45 | private Type type;
46 |
47 | // optional in "class" only, otherwise mandatory
48 | private String name;
49 |
50 | // optional
51 | private String clazz;
52 |
53 | public String toAntElement()
54 | {
55 | switch (type)
56 | {
57 | case CLASS:
58 | return MessageFormat.format("<{0} name=\"{1}\" />", getType().getName(), getName());
59 | default:
60 | return getClazz() == null
61 | ? MessageFormat.format("<{0} name=\"{1}\" />", getType().getName(), getName())
62 | : MessageFormat.format("<{0} name=\"{1}\" class=\"{2}\" />", getType().getName(), getName(), getClazz());
63 | }
64 | }
65 |
66 | public boolean satisfies(PsiElement element)
67 | {
68 | switch (type)
69 | {
70 | case CLASS:
71 | if (element instanceof PsiClass)
72 | {
73 | PsiClass psiClass = (PsiClass) element;
74 | if (getName() != null)
75 | {
76 | return getName().equals(PsiUtils.getKeeperName(psiClass));
77 | }
78 | }
79 | // TODO: deal with fields, methods...
80 | return false;
81 | case FIELD:
82 | if (element instanceof PsiField)
83 | {
84 | PsiField psiField = (PsiField) element;
85 | if (psiField.getName().equals(getName()))
86 | {
87 | return getClazz() == null || getClazz().equals(PsiUtils.getKeeperName(psiField.getContainingClass()));
88 | }
89 | }
90 | return false;
91 | case METHOD:
92 | if (element instanceof PsiMethod)
93 | {
94 | PsiMethod psiMethod = (PsiMethod) element;
95 | String signature = PsiUtils.getSignatureString(psiMethod);
96 | if (signature.equals(getName()))
97 | {
98 | if (getClazz() == null || getClazz().equals(PsiUtils.getKeeperName(psiMethod.getContainingClass())))
99 | {
100 | return true;
101 | }
102 | PsiMethod[] superMethods = psiMethod.findDeepestSuperMethods();
103 | for (PsiMethod superMethod : superMethods)
104 | {
105 | if (getClazz().equals(PsiUtils.getKeeperName(superMethod.getContainingClass())))
106 | {
107 | return true;
108 | }
109 | }
110 | }
111 | }
112 | return false;
113 | }
114 | return false;
115 | }
116 |
117 | public String getName()
118 | {
119 | return name;
120 | }
121 |
122 | public void setName(String name)
123 | {
124 | this.name = name;
125 | }
126 |
127 | public String getClazz()
128 | {
129 | return clazz;
130 | }
131 |
132 | public void setClazz(String clazz)
133 | {
134 | this.clazz = clazz;
135 | }
136 |
137 | public Type getType()
138 | {
139 | return type;
140 | }
141 |
142 | public void setType(Type type)
143 | {
144 | this.type = type;
145 | }
146 |
147 | public String toText()
148 | {
149 | return toAntElement();
150 | }
151 | }
152 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/util/PsiUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.util;
18 |
19 | import com.intellij.psi.*;
20 | import org.jetbrains.annotations.NotNull;
21 | import org.jetbrains.annotations.Nullable;
22 |
23 | /**
24 | * Created by IntelliJ IDEA.
25 | * User: Ronnie
26 | * Date: 2009-nov-02
27 | * Time: 20:44:14
28 | */
29 | public class PsiUtils
30 | {
31 | private static final String VOID_MAIN_JAVA_LANG_STRING = "void main(java.lang.String[])";
32 |
33 | @NotNull
34 | public static String getSignatureString(@NotNull PsiMethod method)
35 | {
36 | StringBuilder sb = new StringBuilder();
37 |
38 | PsiType returnType = method.getReturnType();
39 | if (returnType != null)
40 | {
41 | final String text = returnType.getCanonicalText();
42 | sb.append(simplify(text)).append(" ");
43 | }
44 |
45 | sb.append(method.getName());
46 | sb.append("(");
47 |
48 | String prepend = "";
49 | for (PsiParameter psiParameter : method.getParameterList().getParameters())
50 | {
51 | sb.append(prepend);
52 | prepend = ", ";
53 | final String text = psiParameter.getType().getCanonicalText();
54 | sb.append(simplify(text));
55 | }
56 |
57 | sb.append(")");
58 |
59 | return sb.toString();
60 | }
61 |
62 | private static String simplify(@NotNull String text)
63 | {
64 | while (text.contains(">"))
65 | {
66 | int eix = text.indexOf('>');
67 | String before = text.substring(0, eix);
68 | int six = before.lastIndexOf('<');
69 | if (six == -1)
70 | {
71 | // unmatched pair - abort...
72 | return text;
73 | }
74 | text = text.substring(0, six) + text.substring(eix + 1);
75 | }
76 | return text;
77 | }
78 |
79 | @Nullable
80 | public static String getKeeperName(@Nullable PsiElement element)
81 | {
82 | if (element == null)
83 | {
84 | return null;
85 | }
86 |
87 | if (element instanceof PsiClass)
88 | {
89 | final PsiClass psiClass = (PsiClass) element;
90 | final PsiClass containingClass = psiClass.getContainingClass();
91 | if (containingClass != null)
92 | {
93 | return getKeeperName(containingClass) + "$" + psiClass.getName();
94 | }
95 | return psiClass.getQualifiedName();
96 | }
97 | else if (element instanceof PsiMethod)
98 | {
99 | return getSignatureString((PsiMethod) element);
100 | }
101 | else if (element instanceof PsiField)
102 | {
103 | return ((PsiField) element).getName();
104 | }
105 | else if (element instanceof PsiPackage)
106 | {
107 | return ((PsiPackage) element).getQualifiedName();
108 | }
109 |
110 | return null;
111 | }
112 |
113 | @Nullable
114 | public static String getClazzName(@Nullable PsiElement element)
115 | {
116 | if (element == null)
117 | {
118 | return null;
119 | }
120 |
121 | if (element instanceof PsiMethod)
122 | {
123 | final PsiClass psiClass = ((PsiMethod) element).getContainingClass();
124 | return psiClass == null ? null : PsiUtils.getKeeperName(psiClass);
125 | }
126 | else if (element instanceof PsiField)
127 | {
128 | final PsiClass psiClass = ((PsiField) element).getContainingClass();
129 | return psiClass == null ? null : PsiUtils.getKeeperName(psiClass);
130 | }
131 |
132 | return null;
133 | }
134 |
135 | public static boolean isPublicStaticVoidMain(@NotNull PsiMethod method)
136 | {
137 | final PsiModifierList modifierList = method.getModifierList();
138 | return modifierList.hasModifierProperty("public")
139 | && modifierList.hasModifierProperty("static")
140 | && VOID_MAIN_JAVA_LANG_STRING.equals(getSignatureString(method));
141 | }
142 |
143 | public static void main(String[] args)
144 | {
145 | String in = "java.util.Collection foobar(java.util.List>)";
146 | System.out.println("in = " + in);
147 | String out = simplify(in);
148 | System.out.println("out = " + out);
149 | }
150 | }
151 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/gutter/GuardMarker.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.gutter;
18 |
19 | import com.intellij.openapi.application.ApplicationManager;
20 | import com.intellij.openapi.application.ModalityState;
21 | import com.intellij.openapi.editor.Document;
22 | import com.intellij.openapi.editor.impl.DocumentMarkupModel;
23 | import com.intellij.openapi.editor.markup.GutterIconRenderer;
24 | import com.intellij.openapi.editor.markup.MarkupModel;
25 | import com.intellij.openapi.editor.markup.RangeHighlighter;
26 | import com.intellij.openapi.project.Project;
27 | import com.intellij.openapi.util.Key;
28 | import com.intellij.psi.PsiFile;
29 | import org.jetbrains.annotations.Nullable;
30 | import org.jetbrains.annotations.NotNull;
31 |
32 | import java.util.Collections;
33 | import java.util.List;
34 |
35 | /**
36 | * User: ronnie
37 | * Date: 2009-nov-09
38 | * Time: 13:11:58
39 | */
40 | public class GuardMarker
41 | {
42 | public static final Key KEY = Key.create("com.github.intelliguard.gutter.GuardMarker");
43 |
44 | private final PsiFile psiFile;
45 |
46 | public GuardMarker(@NotNull PsiFile psiFile)
47 | {
48 | this.psiFile = psiFile;
49 | }
50 |
51 | public void refresh()
52 | {
53 | createMarkers(psiFile);
54 | }
55 |
56 | public static void clearMarkers(@Nullable final PsiFile psiFile)
57 | {
58 | final MarkupModel markupModel = getMarkupModel(psiFile);
59 | if (markupModel == null)
60 | {
61 | return;
62 | }
63 |
64 | final List guardGutterRenderers = Collections.emptyList();
65 |
66 | ApplicationManager.getApplication().invokeLater(new Runnable()
67 | {
68 | public void run()
69 | {
70 | applyRenderers(markupModel, guardGutterRenderers);
71 | final GuardMarker marker = markupModel.getDocument().getUserData(KEY);
72 | if (marker != null)
73 | {
74 | markupModel.getDocument().putUserData(KEY, null);
75 | }
76 | }
77 | }, ModalityState.NON_MODAL);
78 | }
79 |
80 | public static void createMarkers(@Nullable final PsiFile psiFile)
81 | {
82 | final MarkupModel markupModel = getMarkupModel(psiFile);
83 | if (markupModel == null)
84 | {
85 | return;
86 | }
87 |
88 | final List guardGutterRenderers = ApplicationManager.getApplication().runReadAction(new GuardGutterRendererComputation(psiFile));
89 |
90 | ApplicationManager.getApplication().invokeLater(new Runnable()
91 | {
92 | public void run()
93 | {
94 | applyRenderers(markupModel, guardGutterRenderers);
95 | final GuardMarker marker = new GuardMarker(psiFile);
96 | markupModel.getDocument().putUserData(KEY, marker);
97 | }
98 | }, ModalityState.NON_MODAL);
99 | }
100 |
101 | @Nullable
102 | private static MarkupModel getMarkupModel(@Nullable final PsiFile psiFile)
103 | {
104 | if (psiFile == null) return null;
105 |
106 | final Document document = psiFile.getViewProvider().getDocument();
107 | final Project project = psiFile.getProject();
108 | if (document != null)
109 | {
110 | return DocumentMarkupModel.forDocument(document, project, true);
111 | }
112 | return null;
113 | }
114 |
115 | @Nullable
116 | public static GuardMarker getGuardMarker(@Nullable PsiFile psiFile)
117 | {
118 | final MarkupModel markupModel = GuardMarker.getMarkupModel(psiFile);
119 | return markupModel == null ? null : markupModel.getDocument().getUserData(KEY);
120 | }
121 |
122 | private static void applyRenderers(MarkupModel markupModel, List guardGutterRenderers)
123 | {
124 | RangeHighlighter[] allHighlighters = markupModel.getAllHighlighters();
125 |
126 | for (RangeHighlighter highlighter : allHighlighters)
127 | {
128 | GutterIconRenderer gutterIconRenderer = highlighter.getGutterIconRenderer();
129 | if (gutterIconRenderer instanceof GuardGutterRenderer)
130 | {
131 | markupModel.removeHighlighter(highlighter);
132 | }
133 | }
134 |
135 | for (GuardGutterRenderer guardGutterRenderer : guardGutterRenderers)
136 | {
137 | RangeHighlighter rangeHighlighter = guardGutterRenderer.addLineHighlighter(markupModel);
138 | rangeHighlighter.setGutterIconRenderer(guardGutterRenderer);
139 | }
140 | }
141 | }
142 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/inspection/ReflectionProblemsInspection.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.inspection;
18 |
19 | import com.github.intelliguard.facet.GuardFacetConfiguration;
20 | import com.github.intelliguard.model.Keeper;
21 | import com.github.intelliguard.util.InspectionUtils;
22 | import com.intellij.codeInspection.ProblemsHolder;
23 | import com.intellij.psi.JavaElementVisitor;
24 | import com.intellij.psi.PsiClass;
25 | import com.intellij.psi.PsiElementVisitor;
26 | import com.intellij.psi.PsiReferenceExpression;
27 | import org.jetbrains.annotations.Nls;
28 | import org.jetbrains.annotations.NotNull;
29 |
30 | import java.util.Collection;
31 | import java.util.HashMap;
32 | import java.util.Map;
33 |
34 | /**
35 | * Created by IntelliJ IDEA.
36 | * User: Ronnie
37 | * Date: 2009-nov-21
38 | * Time: 14:58:13
39 | */
40 | public class ReflectionProblemsInspection extends GuardInspectionBase
41 | {
42 | private static final String STATIC_DESCRIPTION = "This inspection detects classes which " +
43 | "under usual conditions need to have their class names kept for reflection purposes, e.g. servlets, " +
44 | "applets, etcetera.";
45 |
46 | private static final Map EXTENDS = new HashMap();
47 | private static final Map IMPLEMENTS = new HashMap();
48 | static
49 | {
50 | EXTENDS.put("javax.servlet.http.HttpServlet", "HTTP Servlet");
51 | EXTENDS.put("java.applet.Applet", "Applet");
52 | EXTENDS.put("javax.microedition.midlet.MIDlet", "MIDlet");
53 | EXTENDS.put("android.app.Activity", "Android Activity");
54 | EXTENDS.put("android.app.Service", "Android Service");
55 | EXTENDS.put("android.content.BroadcastReceiver", "Android Broadcast Receiver");
56 | EXTENDS.put("android.content.ContentProvider", "Android Content Provider");
57 | EXTENDS.put("org.apache.struts.action.Action", "Struts Action");
58 | IMPLEMENTS.put("javacard.framework.Applet", "Java Card");
59 | IMPLEMENTS.put("javax.tv.xlet.Xlet", "Xlet");
60 | IMPLEMENTS.put("java.sql.Driver", "SQL Driver");
61 | IMPLEMENTS.put("com.opensymphony.xwork2.Action", "Struts 2 Action");
62 | }
63 |
64 | @Nls
65 | @NotNull
66 | public String getDisplayName()
67 | {
68 | return "Reflection problems";
69 | }
70 |
71 | @NotNull
72 | public String getShortName()
73 | {
74 | return "ReflectionProblems";
75 | }
76 |
77 | @Override
78 | public String getStaticDescription()
79 | {
80 | return STATIC_DESCRIPTION;
81 | }
82 |
83 | @NotNull
84 | @Override
85 | public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly)
86 | {
87 | return new JavaElementVisitor()
88 | {
89 | @Override
90 | public void visitReferenceExpression(PsiReferenceExpression expression)
91 | {
92 | }
93 |
94 | @Override
95 | public void visitClass(PsiClass aClass)
96 | {
97 | final GuardFacetConfiguration facetConfiguration = getLocalConfiguration();
98 | if (facetConfiguration != null)
99 | {
100 | if (aClass != null && !aClass.isInterface() && !aClass.hasModifierProperty("abstract"))
101 | {
102 | final Keeper[] obfuscationKeepers = facetConfiguration.findConfiguredGuardKeepers(aClass);
103 | if (obfuscationKeepers.length == 0)
104 | {
105 | checkClass(holder, aClass);
106 | }
107 | }
108 | }
109 |
110 | super.visitClass(aClass);
111 | }
112 | };
113 | }
114 |
115 | private void checkClass(@NotNull final ProblemsHolder holder, @NotNull PsiClass aClass)
116 | {
117 | final Collection supers = InspectionUtils.resolveAllSuperClasses(aClass);
118 | for (PsiClass superClass : supers)
119 | {
120 | final String description = EXTENDS.get(superClass.getQualifiedName());
121 | if (description != null)
122 | {
123 | holder.registerProblem(InspectionUtils.getNameIdentifierElement(aClass), description + " name should probably not be obfuscated");
124 | }
125 | }
126 |
127 | final Collection interfaces = InspectionUtils.resolveInterfaces(aClass, supers);
128 | for (PsiClass iface : interfaces)
129 | {
130 | final String description = IMPLEMENTS.get(iface.getQualifiedName());
131 | if (description != null)
132 | {
133 | holder.registerProblem(InspectionUtils.getNameIdentifierElement(aClass), description + " name should probably not be obfuscated");
134 | }
135 | }
136 |
137 | }
138 |
139 | }
140 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/generator/ProGuardGenerator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.generator;
18 |
19 | import com.github.intelliguard.facet.GuardFacet;
20 | import com.github.intelliguard.facet.GuardFacetConfiguration;
21 | import com.github.intelliguard.model.Keeper;
22 | import com.intellij.openapi.roots.OrderEnumerator;
23 | import com.intellij.openapi.vfs.VfsUtil;
24 | import com.intellij.openapi.vfs.VirtualFile;
25 | import com.intellij.util.PathsList;
26 | import org.jetbrains.annotations.NotNull;
27 |
28 | import java.io.File;
29 | import java.text.MessageFormat;
30 | import java.util.List;
31 |
32 | /**
33 | * User: ronnie
34 | * Date: 2009-nov-09
35 | * Time: 08:08:06
36 | */
37 | public class ProGuardGenerator
38 | {
39 | public static String generatePro(@NotNull GuardFacet facet)
40 | {
41 | final GuardFacetConfiguration configuration = facet.getConfiguration();
42 | final String inFile = configuration.inFile != null ? new File(configuration.inFile).getAbsolutePath() : "injar.jar";
43 | final String outFile = configuration.outFile != null ? new File(configuration.outFile).getAbsolutePath() : "outjar.jar";
44 |
45 | final StringBuilder sb = new StringBuilder();
46 |
47 | sb.append(MessageFormat.format(IN_JARS, escape(inFile)));
48 | sb.append(MessageFormat.format(OUT_JARS, escape(outFile)));
49 |
50 | final PathsList dependenciesList = OrderEnumerator.orderEntries(facet.getModule()).withoutModuleSourceEntries().getPathsList();
51 | final List externalDependencies = dependenciesList.getVirtualFiles();
52 | for (VirtualFile dependencyJar : externalDependencies)
53 | {
54 | final String path = VfsUtil.virtualToIoFile(dependencyJar).getAbsolutePath();
55 | sb.append(MessageFormat.format(LIBRARY_JARS, escape(path)));
56 | }
57 |
58 | sb.append(DONT_SHRINK);
59 | sb.append(DONT_OPTIMIZE);
60 |
61 | if (configuration.sourcefile)
62 | {
63 | sb.append(MessageFormat.format(KEEP_ATTRS, "SourceFile"));
64 | }
65 | if (configuration.linenumbertable)
66 | {
67 | sb.append(MessageFormat.format(KEEP_ATTRS, "LineNumberTable"));
68 | }
69 | if (configuration.localvariabletable)
70 | {
71 | sb.append(MessageFormat.format(KEEP_ATTRS, "LocalVariableTable"));
72 | }
73 | if (configuration.localvariabletypetable)
74 | {
75 | sb.append(MessageFormat.format(KEEP_ATTRS, "LocalVariableTypeTable"));
76 | }
77 | if (configuration.runtimevisibleannotations)
78 | {
79 | sb.append(MessageFormat.format(KEEP_ATTRS, "RuntimeVisibleAnnotations"));
80 | }
81 | if (configuration.runtimevisibleparameterannotations)
82 | {
83 | sb.append(MessageFormat.format(KEEP_ATTRS, "RuntimeVisibleParameterAnnotations"));
84 | }
85 | if (configuration.runtimeinvisibleannotations)
86 | {
87 | sb.append(MessageFormat.format(KEEP_ATTRS, "RuntimeInvisibleAnnotations"));
88 | }
89 | if (configuration.runtimeinvisibleparameterannotations)
90 | {
91 | sb.append(MessageFormat.format(KEEP_ATTRS, "RuntimeInvisibleParameterAnnotations"));
92 | }
93 |
94 | if (configuration.mainclass.length() != 0)
95 | {
96 | sb.append(MessageFormat.format(KEEP_MAIN_CLASS, configuration.mainclass));
97 | }
98 |
99 | for (Keeper keeper : configuration.keepers)
100 | {
101 | switch (keeper.getType())
102 | {
103 | case CLASS:
104 | sb.append(MessageFormat.format(KEEP_CLASS, keeper.getName()));
105 | break;
106 | default:
107 | sb.append(MessageFormat.format(OPEN_KEEP_CLASS_MEMBERS, keeper.getClazz() == null ? "*" : keeper.getClazz()));
108 | sb.append(MessageFormat.format(KEEP_CLASS_MEMBER, keeper.getName()));
109 | sb.append(CLOSE_KEEP_CLASS_MEMBERS);
110 | break;
111 | }
112 | }
113 |
114 | return sb.toString();
115 | }
116 |
117 | private static String escape(String in)
118 | {
119 | /*
120 | if (in.contains(" "))
121 | {
122 | return "\"" + in + "\"";
123 | }
124 | */
125 | return in;
126 | }
127 |
128 | private static final String IN_JARS = "-injars {0}\n";
129 | private static final String OUT_JARS = "-outjars {0}\n";
130 | private static final String LIBRARY_JARS = "-libraryjars {0}\n";
131 | private static final String DONT_SHRINK = "-dontshrink\n";
132 | private static final String DONT_OPTIMIZE = "-dontoptimize\n";
133 | private static final String KEEP_ATTRS = "-keepattributes {0}\n";
134 | private static final String KEEP_CLASS = "-keep class {0}\n";
135 | private static final String OPEN_KEEP_CLASS_MEMBERS = "-keepclassmembernames class {0} '{'\n";
136 | private static final String KEEP_CLASS_MEMBER = " *** {0};\n";
137 | private static final String CLOSE_KEEP_CLASS_MEMBERS = "}\n";
138 | private static final String KEEP_MAIN_CLASS = "-keepclasseswithmembers public class {0} '{'\n public static void main(java.lang.String[]);\n'}'\n";
139 | }
140 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/refactor/RenameListenerProvider.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.refactor;
18 |
19 | import com.intellij.refactoring.listeners.RefactoringElementListenerProvider;
20 | import com.intellij.refactoring.listeners.RefactoringElementListener;
21 | import com.intellij.psi.*;
22 | import com.github.intelliguard.inspection.GuardInspectionBase;
23 | import com.github.intelliguard.facet.GuardFacetConfiguration;
24 | import com.github.intelliguard.model.Keeper;
25 | import com.github.intelliguard.util.PsiUtils;
26 | import org.jetbrains.annotations.NotNull;
27 |
28 | /**
29 | * Created by IntelliJ IDEA.
30 | * User: Ronnie
31 | * Date: 2009-nov-03
32 | * Time: 19:31:35
33 | */
34 | public class RenameListenerProvider implements RefactoringElementListenerProvider
35 | {
36 | public RefactoringElementListener getListener(final PsiElement element)
37 | {
38 | if (element instanceof PsiPackage || element instanceof PsiClass || element instanceof PsiMethod || element instanceof PsiField)
39 | {
40 | final String oldName = PsiUtils.getKeeperName(element);
41 | if (oldName == null)
42 | {
43 | return null;
44 | }
45 |
46 | final GuardFacetConfiguration configuration = GuardInspectionBase.getConfiguration(element);
47 | if (configuration == null)
48 | {
49 | return null;
50 | }
51 |
52 | return new RefactoringElementListener()
53 | {
54 | public void elementMoved(@NotNull PsiElement newElement)
55 | {
56 | elementRenamed(newElement);
57 | }
58 |
59 | public void elementRenamed(@NotNull PsiElement newElement)
60 | {
61 | final String newName = PsiUtils.getKeeperName(newElement);
62 | if (newName == null)
63 | {
64 | return;
65 | }
66 |
67 | // renamed package
68 | if (newElement instanceof PsiPackage)
69 | {
70 | for (Keeper keeper : configuration.keepers)
71 | {
72 | if (keeper.getType() == Keeper.Type.CLASS)
73 | {
74 | final String clazz = keeper.getName();
75 | String oldPackage = getPackageName(clazz);
76 | if (oldPackage.equals(oldName))
77 | {
78 | keeper.setName(newName + "." + getSimpleName(clazz));
79 | }
80 | }
81 | else
82 | {
83 | final String clazz = keeper.getClazz();
84 | if (clazz != null)
85 | {
86 | String oldPackage = getPackageName(clazz);
87 | if (oldPackage.equals(oldName))
88 | {
89 | keeper.setClazz(newName + "." + getSimpleName(clazz));
90 | }
91 | }
92 | }
93 | }
94 | return;
95 | }
96 |
97 | // renamed Main-Class
98 | if (newElement instanceof PsiClass && oldName.equals(configuration.mainclass))
99 | {
100 | configuration.mainclass = newName;
101 | }
102 |
103 | for (Keeper keeper : configuration.keepers)
104 | {
105 | if (newElement instanceof PsiClass)
106 | {
107 | if (keeper.getType() == Keeper.Type.CLASS)
108 | {
109 | if (oldName.equals(keeper.getName()))
110 | {
111 | keeper.setName(newName);
112 | }
113 | }
114 | else
115 | {
116 | if (oldName.equals(keeper.getClazz()))
117 | {
118 | keeper.setClazz(newName);
119 | }
120 | }
121 | }
122 | else
123 | {
124 | if (oldName.equals(keeper.getName()))
125 | {
126 | keeper.setName(newName);
127 | }
128 | }
129 | }
130 | }
131 | };
132 | }
133 | return null;
134 | }
135 |
136 | private String getSimpleName(String clazz)
137 | {
138 | return clazz.substring(clazz.lastIndexOf('.') + 1);
139 | }
140 |
141 | private String getPackageName(String clazz)
142 | {
143 | return clazz.indexOf('.') != -1 ? clazz.substring(0, clazz.lastIndexOf('.')) : clazz;
144 | }
145 | }
146 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/action/AbstractExportAction.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.action;
18 |
19 | import com.intellij.openapi.actionSystem.AnActionEvent;
20 | import com.intellij.openapi.actionSystem.DataKeys;
21 | import com.intellij.openapi.module.Module;
22 | import com.intellij.openapi.ui.Messages;
23 | import com.github.intelliguard.facet.GuardFacet;
24 | import com.github.intelliguard.facet.GuardFacetConfiguration;
25 | import com.github.intelliguard.GuardProjectComponent;
26 | import com.github.intelliguard.ui.FileChooserFactory;
27 | import com.github.intelliguard.ui.FormDialogWrapper;
28 | import com.github.intelliguard.ui.ExportOptionsForm;
29 | import com.github.intelliguard.ui.Icons;
30 | import com.github.intelliguard.util.UiUtils;
31 | import com.github.intelliguard.runner.ProgressInfoReceiver;
32 | import org.jetbrains.annotations.NotNull;
33 |
34 | import javax.swing.*;
35 | import java.awt.*;
36 | import java.io.*;
37 |
38 | /**
39 | * Created by IntelliJ IDEA.
40 | * User: Ronnie
41 | * Date: 2009-nov-10
42 | * Time: 20:55:33
43 | */
44 | public abstract class AbstractExportAction extends AbstractGuardAction
45 | {
46 | private static final String NL = "\n";
47 | private static final String LINE_SEP = System.getProperty("line.separator");
48 |
49 | public void actionPerformed(AnActionEvent e)
50 | {
51 | final Module module = getModule(e);
52 | if (module == null)
53 | {
54 | return;
55 | }
56 | final GuardFacet guardFacet = getGuardFacet(module);
57 | if (guardFacet == null)
58 | {
59 | return;
60 | }
61 |
62 | final ExportOptionsForm exportOptionsForm = FormDialogWrapper.showExportOptionsForm(guardFacet);
63 | if (exportOptionsForm == null)
64 | {
65 | // user aborted
66 | return;
67 | }
68 |
69 | GuardFacetConfiguration configuration = guardFacet.getConfiguration();
70 | configuration.mainclass = exportOptionsForm.getMainClass();
71 | configuration.inFile = exportOptionsForm.getJarPath();
72 | configuration.outFile = exportOptionsForm.getObfuscatedJarPath();
73 |
74 | String errorMessage = null;
75 | if (configuration.inFile.length() == 0)
76 | {
77 | errorMessage = "Output jar path not specified";
78 | }
79 | else if (configuration.outFile.length() == 0)
80 | {
81 | errorMessage = "Obfuscation jar path not specified";
82 | }
83 | else if (configuration.inFile.equals(configuration.outFile))
84 | {
85 | errorMessage = "Output jar path and obfuscated jar path can not be the same";
86 | }
87 | if (errorMessage != null)
88 | {
89 | Messages.showErrorDialog(module.getProject(), errorMessage, "Export error");
90 | return;
91 | }
92 |
93 | // output configuration to toolwindow
94 | final String config = generateConfiguration(guardFacet);
95 | final ProgressInfoReceiver receiver = module.getProject().getComponent(GuardProjectComponent.class).createProgressInfoReceiver();
96 | receiver.info(config);
97 |
98 | // ask for saving to file
99 | final int answer = Messages.showYesNoCancelDialog(module.getProject(), "Would you like to export configuration to a file?", "Export configuration", Icons.OBFUSCATION_NODE_ICON);
100 | if (answer == 0)
101 | {
102 | // show file chooser
103 | final Component component = DataKeys.CONTEXT_COMPONENT.getData(e.getDataContext());
104 | final JFileChooser jFileChooser = FileChooserFactory.createPreferredDirectoryFileChooser("Save '" + module.getName() + "' obfuscation settings",
105 | module.getModuleFilePath());
106 | // suggest a suitable name for the output file
107 | jFileChooser.setSelectedFile(new File(jFileChooser.getCurrentDirectory(), module.getName() + "-obfuscation." + getConfigFileExtension()));
108 | int res = jFileChooser.showSaveDialog(component);
109 | if (res == JFileChooser.APPROVE_OPTION)
110 | {
111 | final File selectedFile = jFileChooser.getSelectedFile();
112 | if (!selectedFile.exists() || selectedFile.canWrite())
113 | {
114 | dumpFile(config, selectedFile);
115 | }
116 | }
117 | }
118 |
119 | UiUtils.showInfoBallon(module.getProject(), "Generated obfuscation settings");
120 | }
121 |
122 | private void dumpFile(@NotNull String content, @NotNull File file)
123 | {
124 | if (!NL.equals(LINE_SEP))
125 | {
126 | content = content.replace(NL, LINE_SEP);
127 | }
128 | OutputStream os = null;
129 | try
130 | {
131 | os = new BufferedOutputStream(new FileOutputStream(file));
132 | os.write(content.getBytes("utf-8"));
133 | }
134 | catch (IOException e)
135 | {
136 | e.printStackTrace();
137 | }
138 | if (os != null)
139 | {
140 | try
141 | {
142 | os.close();
143 | }
144 | catch (IOException e)
145 | {
146 | e.printStackTrace();
147 | }
148 | }
149 | }
150 |
151 | protected abstract String generateConfiguration(@NotNull GuardFacet guardFacet);
152 |
153 | protected abstract String getConfigFileExtension();
154 | }
155 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/inspection/SerializationProblemsInspection.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.inspection;
18 |
19 | import com.github.intelliguard.facet.GuardFacetConfiguration;
20 | import com.github.intelliguard.model.Keeper;
21 | import com.github.intelliguard.util.InspectionUtils;
22 | import com.github.intelliguard.util.PsiUtils;
23 | import com.intellij.codeInspection.ProblemsHolder;
24 | import com.intellij.psi.JavaElementVisitor;
25 | import com.intellij.psi.PsiClass;
26 | import com.intellij.psi.PsiElementVisitor;
27 | import com.intellij.psi.PsiField;
28 | import com.intellij.psi.PsiMethod;
29 | import com.intellij.psi.PsiReferenceExpression;
30 | import org.jetbrains.annotations.Nls;
31 | import org.jetbrains.annotations.NotNull;
32 |
33 | import java.text.MessageFormat;
34 |
35 | /**
36 | * Created by IntelliJ IDEA.
37 | * User: Ronnie
38 | * Date: 2009-nov-08
39 | * Time: 19:56:13
40 | */
41 | public class SerializationProblemsInspection extends GuardInspectionBase
42 | {
43 | private static final String STATIC_DESCRIPTION = "This inspection detects fields and methods in serializable " +
44 | "classes which are used for serialization and must have their exact signature kept.";
45 | private static final String JAVA_IO_SERIALIZABLE = "java.io.Serializable";
46 | private static final String SERIAL_VERSION_UID = "serialVersionUID";
47 | private static final String SERIAL_PERSISTENT_FIELDS = "serialPersistentFields";
48 | private static final String METHOD_WRITE_OBJECT = "void writeObject(java.io.ObjectOutputStream)";
49 | private static final String METHOD_READ_OBJECT = "void readObject(java.io.ObjectInputStream)";
50 | private static final String METHOD_WRITE_REPLACE = "java.lang.Object writeReplace()";
51 | private static final String METHOD_READ_RESOLVE = "java.lang.Object readResolve()";
52 |
53 | @Nls
54 | @NotNull
55 | public String getDisplayName()
56 | {
57 | return "Serialization problems";
58 | }
59 |
60 | @NotNull
61 | public String getShortName()
62 | {
63 | return "SerializationProblems";
64 | }
65 |
66 | @Override
67 | public String getStaticDescription()
68 | {
69 | return STATIC_DESCRIPTION;
70 | }
71 |
72 | @NotNull
73 | @Override
74 | public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly)
75 | {
76 | return new JavaElementVisitor()
77 | {
78 | public void visitReferenceExpression(PsiReferenceExpression expression)
79 | {
80 | }
81 |
82 | @Override
83 | public void visitClass(PsiClass aClass)
84 | {
85 | super.visitClass(aClass);
86 | }
87 |
88 | @Override
89 | public void visitField(PsiField field)
90 | {
91 | GuardFacetConfiguration configuration = getLocalConfiguration();
92 | if (configuration != null)
93 | {
94 | if (implementsSerializable(field.getContainingClass()))
95 | {
96 | final Keeper[] configuredGuardKeepers = configuration.findConfiguredGuardKeepers(field);
97 | if (configuredGuardKeepers.length == 0)
98 | {
99 | final String name = PsiUtils.getKeeperName(field);
100 | if (SERIAL_VERSION_UID.equals(name) || SERIAL_PERSISTENT_FIELDS.equals(name))
101 | {
102 | holder.registerProblem(InspectionUtils.getNameIdentifierElement(field),
103 | MessageFormat.format("Class implements {0} but field should not be obfuscated in order for serialization to work.", JAVA_IO_SERIALIZABLE));
104 | }
105 | }
106 | }
107 | }
108 |
109 | super.visitField(field);
110 | }
111 |
112 | @Override
113 | public void visitMethod(PsiMethod method)
114 | {
115 | GuardFacetConfiguration configuration = getLocalConfiguration();
116 | if (configuration != null)
117 | {
118 | if (implementsSerializable(method.getContainingClass()))
119 | {
120 | final Keeper[] configuredGuardKeepers = configuration.findConfiguredGuardKeepers(method);
121 | if (configuredGuardKeepers.length == 0)
122 | {
123 | final String name = PsiUtils.getKeeperName(method);
124 | if (METHOD_READ_OBJECT.equals(name)
125 | || METHOD_WRITE_OBJECT.equals(name)
126 | || METHOD_READ_RESOLVE.equals(name)
127 | || METHOD_WRITE_REPLACE.equals(name))
128 | {
129 | holder.registerProblem(InspectionUtils.getNameIdentifierElement(method),
130 | MessageFormat.format("Class implements {0} but method should not be obfuscated in order for serialization to work.", JAVA_IO_SERIALIZABLE));
131 | }
132 | }
133 | }
134 | }
135 |
136 | super.visitMethod(method);
137 | }
138 | };
139 | }
140 |
141 | private boolean implementsSerializable(PsiClass psiClass)
142 | {
143 | final PsiClass[] interfaces = psiClass.getInterfaces();
144 | for (PsiClass anInterface : interfaces)
145 | {
146 | if (JAVA_IO_SERIALIZABLE.equals(anInterface.getQualifiedName()))
147 | {
148 | return true;
149 | }
150 | }
151 | return false;
152 | }
153 | }
154 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/inspection/GuardInspectionBase.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.inspection;
18 |
19 | import com.intellij.codeInspection.LocalInspectionTool;
20 | import com.intellij.codeInspection.LocalInspectionToolSession;
21 | import com.intellij.codeInspection.ProblemsHolder;
22 | import com.intellij.openapi.module.ModuleType;
23 | import com.intellij.psi.PsiElement;
24 | import com.intellij.psi.PsiFile;
25 | import com.intellij.psi.PsiPackage;
26 | import com.intellij.psi.PsiClass;
27 | import com.intellij.psi.search.GlobalSearchScope;
28 | import com.intellij.openapi.module.Module;
29 | import com.intellij.openapi.module.ModuleUtil;
30 | import com.intellij.openapi.vfs.VfsUtil;
31 | import com.intellij.openapi.vfs.VirtualFile;
32 | import com.intellij.openapi.util.JDOMUtil;
33 | import com.intellij.ide.plugins.PluginManager;
34 | import com.intellij.ide.plugins.PluginBean;
35 | import com.intellij.util.xmlb.XmlSerializer;
36 | import com.intellij.util.xmlb.JDOMXIncluder;
37 | import com.github.intelliguard.facet.GuardFacetConfiguration;
38 | import com.github.intelliguard.facet.GuardFacet;
39 | import com.github.intelliguard.gutter.GuardMarker;
40 | import org.jetbrains.annotations.Nls;
41 | import org.jetbrains.annotations.NotNull;
42 | import org.jetbrains.annotations.Nullable;
43 | import org.jdom.Document;
44 | import org.jdom.Element;
45 | import org.jdom.JDOMException;
46 |
47 | import java.io.File;
48 | import java.io.IOException;
49 | import java.net.URL;
50 |
51 | /**
52 | * Created by IntelliJ IDEA.
53 | * User: Ronnie
54 | * Date: 2009-nov-02
55 | * Time: 21:58:10
56 | */
57 | public abstract class GuardInspectionBase extends LocalInspectionTool
58 | {
59 | private GuardFacetConfiguration localConfiguration;
60 | private PluginBean localPluginDescriptor;
61 |
62 | @Nls
63 | @NotNull
64 | public String getGroupDisplayName()
65 | {
66 | return "Obfuscation";
67 | }
68 |
69 | @Override
70 | public boolean isEnabledByDefault()
71 | {
72 | return true;
73 | }
74 |
75 | @Override
76 | public void inspectionStarted(LocalInspectionToolSession session, boolean isOnTheFly) {
77 | this.localConfiguration = getConfiguration(session.getFile());
78 |
79 | this.localPluginDescriptor = getPluginDescriptor(session.getFile());
80 |
81 | super.inspectionStarted(session, isOnTheFly);
82 | }
83 |
84 | @Override
85 | public void inspectionFinished(LocalInspectionToolSession session, ProblemsHolder problemsHolder) {
86 | this.localConfiguration = null;
87 |
88 | this.localPluginDescriptor = null;
89 |
90 | super.inspectionFinished(session, problemsHolder);
91 | }
92 |
93 | @Nullable
94 | public static PluginBean getPluginDescriptor(@NotNull PsiElement element)
95 | {
96 | final Module module = ModuleUtil.findModuleForPsiElement(element);
97 | if (module == null)
98 | {
99 | return null;
100 | }
101 | final ModuleType moduleType = ModuleType.get(module);
102 | if (moduleType == null)
103 | {
104 | return null;
105 | }
106 | if (!"PLUGIN_MODULE".equals(moduleType.getId()))
107 | {
108 | return null;
109 | }
110 | final VirtualFile moduleFile = module.getModuleFile();
111 | if (moduleFile == null)
112 | {
113 | return null;
114 | }
115 | final VirtualFile moduleDir = moduleFile.getParent();
116 | if (moduleDir == null)
117 | {
118 | return null;
119 | }
120 | final File moduleIoDir = VfsUtil.virtualToIoFile(moduleDir);
121 |
122 | try
123 | {
124 | // Extensions are private in IdeaPluginDescriptorImpl so we need to parse the xml
125 | final URL url = new File(new File(moduleIoDir, "META-INF"), PluginManager.PLUGIN_XML).toURI().toURL();
126 | Document document = JDOMUtil.loadDocument(url);
127 | document = JDOMXIncluder.resolve(document, url.toExternalForm());
128 | final Element rootElement = document.getRootElement();
129 | return XmlSerializer.deserialize(rootElement, PluginBean.class);
130 | }
131 | catch (JDOMException e)
132 | {
133 | e.printStackTrace();
134 | }
135 | catch (IOException e)
136 | {
137 | e.printStackTrace();
138 | }
139 | return null;
140 | }
141 |
142 | @Nullable
143 | public static GuardFacetConfiguration getConfiguration(@NotNull PsiElement element)
144 | {
145 | Module module = ModuleUtil.findModuleForPsiElement(element);
146 | if (module == null)
147 | {
148 | // ModuleUtil does not find module for a package so we search classes in package until we find a module for one
149 | if (element instanceof PsiPackage)
150 | {
151 | final PsiClass[] classesInProjectPackage = ((PsiPackage) element).getClasses(GlobalSearchScope.allScope(element.getProject()));
152 | for (PsiClass psiClass : classesInProjectPackage)
153 | {
154 | module = ModuleUtil.findModuleForPsiElement(psiClass);
155 | if (module != null)
156 | {
157 | break;
158 | }
159 | }
160 | }
161 | }
162 | if (module == null)
163 | {
164 | return null;
165 | }
166 |
167 | GuardFacet guardFacet = GuardFacet.getInstance(module);
168 | return guardFacet != null ? guardFacet.getConfiguration() : null;
169 | }
170 |
171 | @Nullable
172 | protected GuardFacetConfiguration getLocalConfiguration()
173 | {
174 | return localConfiguration;
175 | }
176 |
177 | @Nullable
178 | protected PluginBean getLocalPluginDescriptor()
179 | {
180 | return localPluginDescriptor;
181 | }
182 |
183 | public static void alertGuardMarkers(@NotNull PsiElement element)
184 | {
185 | final PsiFile psiFile = element.getContainingFile();
186 | final GuardMarker marker = GuardMarker.getGuardMarker(psiFile);
187 | if (marker != null)
188 | {
189 | marker.refresh();
190 | }
191 | }
192 | }
193 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/generator/YGuardGenerator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.generator;
18 |
19 | import com.github.intelliguard.facet.GuardFacet;
20 | import com.github.intelliguard.facet.GuardFacetConfiguration;
21 | import com.github.intelliguard.model.Keeper;
22 | import com.intellij.openapi.roots.OrderEnumerator;
23 | import com.intellij.openapi.vfs.VfsUtil;
24 | import com.intellij.openapi.vfs.VirtualFile;
25 | import com.intellij.util.PathsList;
26 | import org.jetbrains.annotations.NotNull;
27 |
28 | import java.io.File;
29 | import java.text.MessageFormat;
30 | import java.util.List;
31 |
32 | /**
33 | * Created by IntelliJ IDEA.
34 | * User: Ronnie
35 | * Date: 2009-nov-08
36 | * Time: 12:09:04
37 | */
38 | public class YGuardGenerator
39 | {
40 | public static final String YGUARD_TARGET_NAME = "yguard";
41 |
42 | public static String generateBuildXml(@NotNull GuardFacet facet)
43 | {
44 | final GuardFacetConfiguration configuration = facet.getConfiguration();
45 | final String inFile = configuration.inFile != null ? new File(configuration.inFile).getAbsolutePath() : "injar.jar";
46 | final File file = configuration.outFile != null ? new File(configuration.outFile) : null;
47 | final String outFile = file != null ? file.getAbsolutePath() : "outjar.jar";
48 | final String yguardFile = configuration.yGuardJar != null ? new File(configuration.yGuardJar).getAbsolutePath() : "yguard.jar";
49 | final String logFile = file != null ? new File(file.getParent(), file.getName() + "-yguard.xml").getAbsolutePath() : "logfile.xml";
50 |
51 | final StringBuilder sb = new StringBuilder();
52 |
53 | sb.append(MessageFormat.format(OPEN_PROJECT, YGUARD_TARGET_NAME));
54 | sb.append(MessageFormat.format(OPEN_TARGET, YGUARD_TARGET_NAME));
55 | sb.append(MessageFormat.format(TASK_DEF, yguardFile));
56 | sb.append(OPEN_YGUARD);
57 | sb.append(MessageFormat.format(IN_OUT_PAIR, inFile, outFile));
58 |
59 | final PathsList dependenciesList = OrderEnumerator.orderEntries(facet.getModule()).withoutModuleSourceEntries().getPathsList();
60 | final List externalDependencies = dependenciesList.getVirtualFiles();
61 | if (!externalDependencies.isEmpty())
62 | {
63 | sb.append(OPEN_EXTERNAL_CLASSES);
64 | for (VirtualFile dependencyJar : externalDependencies)
65 | {
66 | final String path = VfsUtil.virtualToIoFile(dependencyJar).getAbsolutePath();
67 | sb.append(MessageFormat.format(EXTERNAL_PATH_ELEMENT, path));
68 | }
69 | sb.append(CLOSE_EXTERNAL_CLASSES);
70 | }
71 |
72 | if (configuration.mainclass.length() != 0)
73 | {
74 | sb.append(MessageFormat.format(OPEN_RENAME_WITH_MAIN_CLASS, configuration.mainclass, logFile, configuration.conservemanifest, configuration.replaceClassNameStrings));
75 | }
76 | else
77 | {
78 | sb.append(MessageFormat.format(OPEN_RENAME, logFile, configuration.conservemanifest, configuration.replaceClassNameStrings));
79 | }
80 |
81 | if (configuration.errorChecking)
82 | {
83 | sb.append(PEDANTIC_ERROR_CHECKING);
84 | }
85 |
86 | if ( configuration.namingScheme != null ) {
87 | sb.append(MessageFormat.format(NAMING_SCHEME, configuration.namingScheme));
88 | }
89 |
90 | if ( configuration.namingSafety != null ) {
91 | sb.append(MessageFormat.format(NAMING_SAFETY, configuration.namingSafety));
92 | }
93 |
94 | if (!configuration.keepers.isEmpty())
95 | {
96 | final String keep = MessageFormat.format(OPEN_KEEP,
97 | configuration.sourcefile,
98 | configuration.linenumbertable,
99 | configuration.localvariabletable,
100 | configuration.localvariabletypetable,
101 | configuration.runtimevisibleannotations,
102 | configuration.runtimevisibleparameterannotations,
103 | configuration.runtimeinvisibleannotations,
104 | configuration.runtimeinvisibleparameterannotations);
105 | sb.append(keep);
106 | for (Keeper keeper : configuration.keepers)
107 | {
108 | sb.append(" ");
109 | sb.append(keeper.toAntElement());
110 | sb.append("\n");
111 | }
112 | sb.append(CLOSE_KEEP);
113 | }
114 |
115 | sb.append(CLOSE_RENAME);
116 | sb.append(CLOSE_YGUARD);
117 | sb.append(CLOSE_TARGET);
118 | sb.append(CLOSE_PROJECT);
119 |
120 | return sb.toString();
121 | }
122 |
123 | private static final String OPEN_PROJECT = "\n";
124 | private static final String OPEN_TARGET = " \n";
125 | private static final String OPEN_YGUARD = " \n";
126 | private static final String OPEN_RENAME = " \n";
127 | private static final String OPEN_RENAME_WITH_MAIN_CLASS = " \n";
128 | private static final String OPEN_KEEP = " \n";
129 | private static final String CLOSE_KEEP = " \n";
130 | private static final String OPEN_EXTERNAL_CLASSES = " \n";
131 | private static final String EXTERNAL_PATH_ELEMENT = " \n";
132 | private static final String CLOSE_EXTERNAL_CLASSES = " \n";
133 | private static final String IN_OUT_PAIR = " \n";
134 | private static final String TASK_DEF = " \n";
135 | private static final String PEDANTIC_ERROR_CHECKING = " \n";
136 | private static final String NAMING_SCHEME = " \n";
137 | private static final String NAMING_SAFETY = " \n";
138 |
139 | private static final String CLOSE_RENAME = " \n";
140 | private static final String CLOSE_YGUARD = " \n";
141 | private static final String CLOSE_TARGET = " \n";
142 | private static final String CLOSE_PROJECT = "\n";
143 |
144 | }
145 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/runner/JarTask.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.runner;
18 |
19 | import com.github.intelliguard.model.JarConfig;
20 | import com.github.intelliguard.util.ModuleUtils;
21 | import com.intellij.openapi.compiler.make.ManifestBuilder;
22 | import com.intellij.openapi.module.Module;
23 | import com.intellij.openapi.roots.OrderEnumerator;
24 | import com.intellij.openapi.vfs.VirtualFile;
25 | import com.intellij.util.PathsList;
26 | import org.jetbrains.annotations.NotNull;
27 | import org.jetbrains.annotations.Nullable;
28 |
29 | import java.io.*;
30 | import java.util.ArrayList;
31 | import java.util.List;
32 | import java.util.jar.JarEntry;
33 | import java.util.jar.JarOutputStream;
34 | import java.util.jar.Manifest;
35 |
36 | /**
37 | * Created by IntelliJ IDEA.
38 | * User: Ronnie
39 | * Date: 2009-nov-02
40 | * Time: 18:34:27
41 | */
42 | public class JarTask implements Runnable
43 | {
44 | private RunProgress runProgress;
45 | private Module module;
46 | private JarConfig jarConfig;
47 | private String mainClass;
48 | private File outFile;
49 |
50 | public JarTask(@NotNull final RunProgress runProgress, @NotNull final Module module, @NotNull final JarConfig jarConfig, @Nullable final String mainClass, @NotNull final File outFile)
51 | {
52 | this.runProgress = runProgress;
53 | this.module = module;
54 | this.jarConfig = jarConfig;
55 | this.mainClass = mainClass;
56 | this.outFile = outFile;
57 | }
58 |
59 | public void run()
60 | {
61 | Manifest manifest = new Manifest();
62 | manifest = createManifest(manifest, jarConfig, module);
63 | if (mainClass != null && mainClass.length() != 0)
64 | {
65 | manifest.getMainAttributes().putValue("Main-Class", mainClass);
66 | runProgress.markMessage("Setting Main-Class: " + mainClass);
67 | }
68 |
69 | final File classesDir = ModuleUtils.getModuleOutputDir(module);
70 |
71 | JarOutputStream jos = null;
72 |
73 | try
74 | {
75 | outFile.getParentFile().mkdirs();
76 | jos = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)), manifest);
77 |
78 | List jarEntries = jarConfig.getJarEntries();
79 | for (String jarEntry : jarEntries)
80 | {
81 | File entryFile = new File(jarEntry);
82 | if (entryFile.isFile())
83 | {
84 | jarFile(jos, entryFile.getAbsolutePath(), entryFile.getParentFile().getAbsolutePath());
85 | }
86 | else if (entryFile.isDirectory())
87 | {
88 | jarDirectory(jos, entryFile.getAbsolutePath(), entryFile.equals(classesDir) ? entryFile.getAbsolutePath() : entryFile.getParentFile().getAbsolutePath());
89 | }
90 | }
91 | }
92 | catch (IOException e)
93 | {
94 | runProgress.markError(e.getMessage());
95 | }
96 | finally
97 | {
98 | if (jos != null)
99 | {
100 | try
101 | {
102 | jos.close();
103 | }
104 | catch (IOException e)
105 | {
106 | runProgress.markError(e.getMessage());
107 | }
108 | }
109 | }
110 | }
111 |
112 | private void jarDirectory(@NotNull JarOutputStream jos, @NotNull String directoryName, @NotNull String baseDir)
113 | {
114 | File dirobject = new File(directoryName);
115 | if (dirobject.exists())
116 | {
117 | if (dirobject.isDirectory())
118 | {
119 | File[] fileList = dirobject.listFiles();
120 | for (File aFileList : fileList)
121 | {
122 | if (aFileList.isDirectory())
123 | {
124 | jarDirectory(jos, aFileList.getAbsolutePath(), baseDir);
125 | }
126 | else if (aFileList.isFile())
127 | {
128 | jarFile(jos, aFileList.getAbsolutePath(), baseDir);
129 | }
130 | }
131 | }
132 | }
133 | }
134 |
135 | private void jarFile(@NotNull JarOutputStream jos, @NotNull String filePath, @NotNull String baseDir)
136 | {
137 | try
138 | {
139 | FileInputStream fis = new FileInputStream(filePath);
140 | BufferedInputStream bis = new BufferedInputStream(fis);
141 | String entryName = filePath.substring(baseDir.length()).replace('\\', '/');
142 | while (entryName.startsWith("/")) entryName = entryName.substring(1);
143 |
144 | runProgress.markMessage("Adding " + entryName);
145 | JarEntry fileEntry = new JarEntry(entryName);
146 | jos.putNextEntry(fileEntry);
147 | byte[] data = new byte[1024];
148 | int byteCount;
149 | while ((byteCount = bis.read(data, 0, 1024)) > -1)
150 | {
151 | jos.write(data, 0, byteCount);
152 | }
153 | }
154 | catch (IOException e)
155 | {
156 | runProgress.markError(e.getMessage());
157 | }
158 | }
159 |
160 | @NotNull
161 | private Manifest createManifest(@Nullable Manifest manifest, @NotNull JarConfig jarConfig, @NotNull Module module)
162 | {
163 | if (manifest == null)
164 | {
165 | manifest = new Manifest();
166 | }
167 | ManifestBuilder.setGlobalAttributes(manifest.getMainAttributes());
168 | String libsPrefix = jarConfig.getLinkLibraries();
169 | if (libsPrefix != null)
170 | {
171 | PathsList dependenciesList = OrderEnumerator.orderEntries(module).withoutModuleSourceEntries().getPathsList();
172 | List virtualFileList = dependenciesList.getVirtualFiles();
173 | if (!virtualFileList.isEmpty())
174 | {
175 | List dependencyFileNames = new ArrayList();
176 | for (VirtualFile dependencyJar : virtualFileList)
177 | {
178 | final String dependencyName = dependencyJar.getName();
179 | runProgress.markMessage("Adding dependency " + dependencyName);
180 | dependencyFileNames.add(dependencyName);
181 | }
182 | StringBuilder sb = new StringBuilder();
183 | libsPrefix = libsPrefix.replace('\\', '/').trim();
184 | while (libsPrefix.startsWith("/")) libsPrefix = libsPrefix.substring(1);
185 | while (libsPrefix.endsWith("/")) libsPrefix = libsPrefix.substring(0, libsPrefix.length() - 1);
186 | for (String dependencyFileName : dependencyFileNames)
187 | {
188 | sb.append(" ");
189 | sb.append(libsPrefix);
190 | if (libsPrefix.length() > 0) sb.append("/");
191 | sb.append(dependencyFileName);
192 | }
193 | String classpath = sb.toString().trim();
194 | manifest.getMainAttributes().putValue("Class-Path", classpath);
195 | }
196 | }
197 | return manifest;
198 | }
199 | }
200 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/facet/GuardFacetConfiguration.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.facet;
18 |
19 | import com.intellij.facet.FacetConfiguration;
20 | import com.intellij.facet.ui.FacetEditorTab;
21 | import com.intellij.facet.ui.FacetEditorContext;
22 | import com.intellij.facet.ui.FacetValidatorsManager;
23 | import com.intellij.openapi.util.InvalidDataException;
24 | import com.intellij.openapi.util.WriteExternalException;
25 | import com.intellij.openapi.components.PersistentStateComponent;
26 | import com.intellij.psi.PsiElement;
27 | import com.intellij.psi.PsiMethod;
28 | import com.intellij.psi.PsiClass;
29 | import com.github.intelliguard.model.Keeper;
30 | import com.github.intelliguard.model.JarConfig;
31 | import com.github.intelliguard.util.PsiUtils;
32 | import org.jdom.Element;
33 |
34 | import java.util.Collection;
35 | import java.util.ArrayList;
36 |
37 | /**
38 | * Created by IntelliJ IDEA.
39 | * User: Ronnie
40 | * Date: 2009-okt-21
41 | * Time: 19:02:21
42 | */
43 | public class GuardFacetConfiguration implements FacetConfiguration, PersistentStateComponent
44 | {
45 | public String yGuardJar;
46 |
47 | public String inFile;
48 | public String outFile;
49 |
50 | // rename attributes
51 | public String mainclass = ""; // mainclass
52 | public boolean conservemanifest; // conservemanifest
53 | public boolean replaceClassNameStrings = true; // replaceClassNameStrings
54 |
55 | // rename property:
56 | public boolean errorChecking = true;
57 |
58 | // rename property:
59 | public String namingScheme = "small";
60 |
61 | // rename property:
62 | public String namingSafety = "compatible";
63 |
64 | // keep attributes
65 | public boolean sourcefile; // sourcefile
66 | public boolean linenumbertable; // linenumbertable
67 | public boolean localvariabletable; // localvariabletable
68 | public boolean localvariabletypetable; // localvariabletypetable
69 | public boolean runtimevisibleannotations = true; // runtimevisibleannotations
70 | public boolean runtimevisibleparameterannotations = true; // runtimevisibleparameterannotations
71 | public boolean runtimeinvisibleannotations = true; // runtimeinvisibleannotations
72 | public boolean runtimeinvisibleparameterannotations = true; // runtimeinvisibleparameterannotations
73 |
74 | public Collection keepers = new ArrayList();
75 |
76 | public JarConfig jarConfig = new JarConfig();
77 |
78 | public FacetEditorTab[] createEditorTabs(FacetEditorContext editorContext, FacetValidatorsManager validatorsManager)
79 | {
80 | return new FacetEditorTab[] {new GuardFacetEditorTab(this, editorContext, validatorsManager)}; //To change body of implemented methods use File | Settings | File Templates.
81 | }
82 |
83 | public void applyGlobalSettings(GuardFacetConfiguration state)
84 | {
85 | loadState(state);
86 | }
87 |
88 | public void readExternal(Element element) throws InvalidDataException
89 | {
90 | }
91 |
92 | public void writeExternal(Element element) throws WriteExternalException
93 | {
94 | }
95 |
96 | public GuardFacetConfiguration getState()
97 | {
98 | return this;
99 | }
100 |
101 | public void loadState(GuardFacetConfiguration state)
102 | {
103 | this.yGuardJar = state.yGuardJar;
104 | this.inFile = state.inFile;
105 | this.outFile = state.outFile;
106 | this.conservemanifest = state.conservemanifest;
107 | this.errorChecking = state.errorChecking;
108 | this.namingScheme = state.namingScheme;
109 | this.namingSafety = state.namingSafety;
110 | this.linenumbertable = state.linenumbertable;
111 | this.localvariabletable = state.localvariabletable;
112 | this.localvariabletypetable = state.localvariabletypetable;
113 | this.mainclass = state.mainclass;
114 | this.replaceClassNameStrings = state.replaceClassNameStrings;
115 | this.runtimeinvisibleannotations = state.runtimeinvisibleannotations;
116 | this.runtimeinvisibleparameterannotations = state.runtimeinvisibleparameterannotations;
117 | this.runtimevisibleannotations = state.runtimevisibleannotations;
118 | this.runtimevisibleparameterannotations = state.runtimevisibleparameterannotations;
119 | this.sourcefile = state.sourcefile;
120 | this.keepers = state.keepers;
121 | this.jarConfig= state.jarConfig;
122 | }
123 |
124 | public boolean equalsGlobalSettings(GuardFacetConfiguration that)
125 | {
126 | if (this == that) return true;
127 | if (that == null || getClass() != that.getClass()) return false;
128 |
129 | if (conservemanifest != that.conservemanifest) return false;
130 | if (errorChecking != that.errorChecking) return false;
131 | if (linenumbertable != that.linenumbertable) return false;
132 | if (localvariabletable != that.localvariabletable) return false;
133 | if (localvariabletypetable != that.localvariabletypetable) return false;
134 | if (replaceClassNameStrings != that.replaceClassNameStrings) return false;
135 | if (runtimeinvisibleannotations != that.runtimeinvisibleannotations) return false;
136 | if (runtimeinvisibleparameterannotations != that.runtimeinvisibleparameterannotations) return false;
137 | if (runtimevisibleannotations != that.runtimevisibleannotations) return false;
138 | if (runtimevisibleparameterannotations != that.runtimevisibleparameterannotations) return false;
139 | if (sourcefile != that.sourcefile) return false;
140 | if (mainclass != null ? !mainclass.equals(that.mainclass) : that.mainclass != null) return false;
141 | if (yGuardJar != null ? !yGuardJar.equals(that.yGuardJar) : that.yGuardJar != null) return false;
142 | if (namingScheme != null ? !namingScheme.equals(that.namingScheme) : that.namingScheme != null ) return false;
143 | if (namingSafety != null ? !namingSafety.equals(that.namingSafety) : that.namingSafety != null ) return false;
144 |
145 | return true;
146 | }
147 |
148 | public boolean isKeptByMainClass(PsiElement element)
149 | {
150 | if (mainclass.length() == 0)
151 | {
152 | return false;
153 | }
154 | if (element instanceof PsiClass)
155 | {
156 | return mainclass.equals(PsiUtils.getKeeperName(element));
157 | }
158 | if (element instanceof PsiMethod)
159 | {
160 | PsiMethod psiMethod = (PsiMethod) element;
161 | return mainclass.equals(PsiUtils.getKeeperName(psiMethod.getContainingClass()))
162 | && PsiUtils.isPublicStaticVoidMain(psiMethod);
163 | }
164 | return false;
165 | }
166 |
167 | public Keeper[] findConfiguredGuardKeepers(PsiElement psiElement)
168 | {
169 | // special treatment for constructors
170 | PsiMethod constructor = null;
171 | if (psiElement instanceof PsiMethod)
172 | {
173 | PsiMethod psiMethod = (PsiMethod) psiElement;
174 | if (psiMethod.isConstructor())
175 | {
176 | constructor = psiMethod;
177 | }
178 | }
179 |
180 | Collection found = new ArrayList();
181 | for (Keeper keeper : keepers)
182 | {
183 | if (constructor != null)
184 | {
185 | if (keeper.getType() == Keeper.Type.CLASS && keeper.getName() != null)
186 | {
187 | if (keeper.getName().equals(PsiUtils.getKeeperName(constructor.getContainingClass())))
188 | {
189 | found.add(keeper);
190 | }
191 | }
192 | }
193 | else if (keeper.satisfies(psiElement))
194 | {
195 | found.add(keeper);
196 | }
197 | }
198 | return found.toArray(new Keeper[found.size()]);
199 | }
200 | }
201 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/action/RunObfuscationAction.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.action;
18 |
19 | import com.intellij.openapi.actionSystem.AnActionEvent;
20 | import com.intellij.openapi.actionSystem.DataKeys;
21 | import com.intellij.openapi.actionSystem.Presentation;
22 | import com.intellij.openapi.vfs.VirtualFile;
23 | import com.intellij.openapi.module.Module;
24 | import com.intellij.openapi.compiler.*;
25 | import com.intellij.openapi.progress.ProgressManager;
26 | import com.intellij.openapi.ui.Messages;
27 | import com.intellij.facet.ui.ValidationResult;
28 | import com.github.intelliguard.facet.GuardFacet;
29 | import com.github.intelliguard.facet.GuardFacetConfiguration;
30 | import com.github.intelliguard.ui.FormDialogWrapper;
31 | import com.github.intelliguard.ui.JarOptionsForm;
32 | import com.github.intelliguard.util.UiUtils;
33 | import com.github.intelliguard.runner.JarTask;
34 | import com.github.intelliguard.runner.ObfuscateTask;
35 | import com.github.intelliguard.runner.RunProgress;
36 | import com.github.intelliguard.util.ModuleUtils;
37 | import com.github.intelliguard.util.ObfuscatorUtils;
38 | import com.github.intelliguard.GuardProjectComponent;
39 |
40 | import java.io.*;
41 |
42 | /**
43 | * Created by IntelliJ IDEA.
44 | * User: Ronnie
45 | * Date: 2009-okt-29
46 | * Time: 17:04:58
47 | */
48 | public class RunObfuscationAction extends AbstractGuardAction
49 | {
50 | @Override
51 | public void update(AnActionEvent e)
52 | {
53 | final Presentation presentation = e.getPresentation();
54 |
55 | final Module module = e.getData(DataKeys.MODULE);
56 | if (module == null)
57 | {
58 | presentation.setEnabled(false);
59 | return;
60 | }
61 |
62 | presentation.setText("Obfuscate module '" + module.getName() + "'");
63 |
64 | final GuardFacet guardFacet = GuardFacet.getInstance(module);
65 | presentation.setEnabled(guardFacet != null);
66 | }
67 |
68 | public void actionPerformed(AnActionEvent e)
69 | {
70 | final Module module = e.getData(DataKeys.MODULE);
71 | if (module == null)
72 | {
73 | System.out.println("RunObfuscationAction.actionPerformed: no Module");
74 | return;
75 | }
76 | final GuardFacet guardFacet = GuardFacet.getInstance(module);
77 | if (guardFacet == null)
78 | {
79 | System.out.println("RunObfuscationAction.actionPerformed: no GuardFacet");
80 | return;
81 | }
82 | VirtualFile baseDir = module.getProject().getBaseDir();
83 | if (baseDir == null)
84 | {
85 | System.out.println("RunObfuscationAction.actionPerformed: no baseDir");
86 | return;
87 | }
88 |
89 | if (guardFacet.getConfiguration().yGuardJar == null)
90 | {
91 | Messages.showErrorDialog(module.getProject(), "Missing yGuard archive\n\nPlease check Obfuscation facet settings.", "Obfuscation error");
92 | return;
93 | }
94 |
95 | final ValidationResult yGuardValidationResult = ObfuscatorUtils.checkYGuard(guardFacet.getConfiguration().yGuardJar);
96 | if (yGuardValidationResult != ValidationResult.OK)
97 | {
98 | Messages.showErrorDialog(module.getProject(), "Invalid yGuard archive: " + yGuardValidationResult.getErrorMessage() + "\n\nPlease check Obfuscation facet settings.", "Obfuscation error");
99 | return;
100 | }
101 |
102 | File outputDir = ModuleUtils.getModuleOutputDir(module);
103 | if (outputDir != null)
104 | {
105 | guardFacet.getConfiguration().jarConfig.addEntry(outputDir.getAbsolutePath());
106 | }
107 |
108 | JarOptionsForm jarOptionsForm = FormDialogWrapper.showJarOptionsForm(guardFacet);
109 |
110 | if (jarOptionsForm != null)
111 | {
112 | GuardFacetConfiguration configuration = guardFacet.getConfiguration();
113 | configuration.jarConfig.setLinkLibraries(jarOptionsForm.getLibrariesManifestPath());
114 | configuration.mainclass = jarOptionsForm.getMainClass();
115 | configuration.inFile = jarOptionsForm.getJarPath();
116 | configuration.outFile = jarOptionsForm.getObfuscatedJarPath();
117 |
118 | String errorMessage = null;
119 | if (configuration.inFile.length() == 0)
120 | {
121 | errorMessage = "Output jar path not specified";
122 | }
123 | else if (configuration.outFile.length() == 0)
124 | {
125 | errorMessage = "Obfuscation jar path not specified";
126 | }
127 | else if (configuration.inFile.equals(configuration.outFile))
128 | {
129 | errorMessage = "Output jar path and obfuscated jar path can not be the same";
130 | }
131 | if (errorMessage != null)
132 | {
133 | Messages.showErrorDialog(module.getProject(), errorMessage, "Obfuscation error");
134 | return;
135 | }
136 |
137 | final File inJar = new File(configuration.inFile);
138 | final File outJar = new File(configuration.outFile);
139 |
140 | final RunProgress runProgress = new RunProgress(module.getProject().getComponent(GuardProjectComponent.class).createProgressInfoReceiver());
141 | final Runnable jarTask = new JarTask(runProgress, module, configuration.jarConfig, configuration.mainclass, inJar);
142 | final Runnable obfuscateTask = new ObfuscateTask(runProgress, guardFacet);
143 |
144 | if (jarOptionsForm.getExecuteMake())
145 | {
146 | CompilerManager compilerManager = CompilerManager.getInstance(module.getProject());
147 | compilerManager.make(module.getProject(), new Module[] { module }, new CompileStatusNotification()
148 | {
149 | public void finished(boolean aborted, int errors, int warnings, CompileContext compileContext)
150 | {
151 | if (errors == 0 && !aborted)
152 | {
153 | ProgressManager.getInstance().runProcessWithProgressSynchronously(jarTask, "Building jar " + inJar.getName(), true, module.getProject());
154 | if (runProgress.lookingGood())
155 | {
156 | ProgressManager.getInstance().runProcessWithProgressSynchronously(obfuscateTask, "Obfuscating jar " + inJar.getName(), true, module.getProject());
157 |
158 | if (runProgress.lookingGood())
159 | {
160 | UiUtils.showInfoBallon(module.getProject(), "Obfuscated jar: " + outJar.getAbsolutePath());
161 | }
162 | else
163 | {
164 | UiUtils.showErrorBallon(module.getProject(), "Error obfuscating jar " + outJar.getAbsolutePath());
165 | }
166 | }
167 | else
168 | {
169 | UiUtils.showErrorBallon(module.getProject(), "Error building jar " + inJar.getAbsolutePath());
170 | }
171 | }
172 | else
173 | {
174 | runProgress.markError("Obfuscation aborted. Compilation errors: " + errors);
175 | UiUtils.showErrorBallon(module.getProject(), "Obfuscation aborted. Compilation errors: " + errors);
176 | }
177 | }
178 | });
179 | }
180 | else
181 | {
182 | ProgressManager.getInstance().runProcessWithProgressSynchronously(jarTask, "Building jar " + inJar.getName(), true, module.getProject());
183 | if (runProgress.lookingGood())
184 | {
185 | ProgressManager.getInstance().runProcessWithProgressSynchronously(obfuscateTask, "Obfuscating jar " + inJar.getName(), true, module.getProject());
186 |
187 | if (runProgress.lookingGood())
188 | {
189 | UiUtils.showInfoBallon(module.getProject(), "Obfuscated jar: " + outJar.getAbsolutePath());
190 | }
191 | else
192 | {
193 | UiUtils.showErrorBallon(module.getProject(), "Error obfuscating jar " + outJar.getAbsolutePath());
194 | }
195 | }
196 | else
197 | {
198 | UiUtils.showErrorBallon(module.getProject(), "Error building jar " + inJar.getAbsolutePath());
199 | }
200 | }
201 | }
202 | }
203 | }
204 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/inspection/PluginProblemsInspection.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.inspection;
18 |
19 | import org.jetbrains.annotations.Nls;
20 | import org.jetbrains.annotations.NotNull;
21 | import org.jetbrains.annotations.Nullable;
22 | import org.jdom.Element;
23 | import com.intellij.psi.PsiElementVisitor;
24 | import com.intellij.psi.JavaElementVisitor;
25 | import com.intellij.psi.PsiReferenceExpression;
26 | import com.intellij.psi.PsiClass;
27 | import com.intellij.codeInspection.ProblemsHolder;
28 | import com.intellij.ide.plugins.PluginBean;
29 | import com.intellij.ide.plugins.PluginManager;
30 | import com.intellij.openapi.components.ComponentConfig;
31 | import com.github.intelliguard.util.PsiUtils;
32 | import com.github.intelliguard.util.InspectionUtils;
33 | import com.github.intelliguard.facet.GuardFacetConfiguration;
34 | import com.github.intelliguard.model.Keeper;
35 |
36 | import java.util.*;
37 | import java.text.MessageFormat;
38 |
39 | /**
40 | * Created by IntelliJ IDEA.
41 | * User: Ronnie
42 | * Date: 2009-nov-14
43 | * Time: 23:34:12
44 | */
45 | public class PluginProblemsInspection extends GuardInspectionBase
46 | {
47 | private static final String STATIC_DESCRIPTION = "This inspection aids plugin developers and detects classes which " +
48 | "are declared in plugin.xml. These classes can be components, actions or extensions, and their names should " +
49 | "not be obfuscated in order for the plugin to load correctly.";
50 |
51 | @Nls
52 | @NotNull
53 | public String getDisplayName()
54 | {
55 | return "Plugin descriptor problems";
56 | }
57 |
58 | @NotNull
59 | public String getShortName()
60 | {
61 | return "PluginProblems";
62 | }
63 |
64 | @Override
65 | public String getStaticDescription()
66 | {
67 | return STATIC_DESCRIPTION;
68 | }
69 |
70 | @NotNull
71 | @Override
72 | public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly)
73 | {
74 | return new JavaElementVisitor()
75 | {
76 | @Override
77 | public void visitReferenceExpression(PsiReferenceExpression expression)
78 | {
79 | }
80 |
81 | @Override
82 | public void visitClass(PsiClass aClass)
83 | {
84 | final GuardFacetConfiguration facetConfiguration = getLocalConfiguration();
85 | final PluginBean pluginDescriptor = getLocalPluginDescriptor();
86 | if (facetConfiguration != null && pluginDescriptor != null)
87 | {
88 | final Keeper[] obfuscationKeepers = facetConfiguration.findConfiguredGuardKeepers(aClass);
89 | if (obfuscationKeepers.length == 0)
90 | {
91 | final String className = PsiUtils.getKeeperName(aClass);
92 |
93 | final Set applicationComponentClasses = new HashSet();
94 | extractComponentClassNames(applicationComponentClasses, pluginDescriptor.applicationComponents);
95 |
96 | final Set projectComponentClasses = new HashSet();
97 | extractComponentClassNames(projectComponentClasses, pluginDescriptor.projectComponents);
98 |
99 | final Set moduleComponentClasses = new HashSet();
100 | extractComponentClassNames(moduleComponentClasses, pluginDescriptor.moduleComponents);
101 |
102 | final Set actionClasses = new HashSet();
103 | extractElementClassNames(actionClasses, pluginDescriptor.actions);
104 |
105 | final Set extensionClasses = new HashSet();
106 | extractElementClassNames(extensionClasses, pluginDescriptor.extensions);
107 |
108 | final Set extensionPoints = new HashSet();
109 | extractElementClassNames(extensionPoints, pluginDescriptor.extensionPoints);
110 |
111 | /*
112 | System.out.println("ApplicationComponentClasses = " + applicationComponentClasses);
113 | System.out.println("ProjectComponentClasses = " + projectComponentClasses);
114 | System.out.println("ModuleComponentClasses = " + moduleComponentClasses);
115 | System.out.println("ActionClasses = " + actionClasses);
116 | System.out.println("ExtensionClasses = " + extensionClasses);
117 | System.out.println("ExtensionPoints = " + extensionPoints);
118 | */
119 |
120 | if (applicationComponentClasses.contains(className))
121 | {
122 | holder.registerProblem(InspectionUtils.getNameIdentifierElement(aClass),
123 | MessageFormat.format("Class {0} is listed as an ApplicationComponent in {1} and should not be obfuscated", className, PluginManager.PLUGIN_XML));
124 | }
125 | if (projectComponentClasses.contains(className))
126 | {
127 | holder.registerProblem(InspectionUtils.getNameIdentifierElement(aClass),
128 | MessageFormat.format("Class {0} is listed as a ProjectComponent in {1} and should not be obfuscated", className, PluginManager.PLUGIN_XML));
129 | }
130 | if (moduleComponentClasses.contains(className))
131 | {
132 | holder.registerProblem(InspectionUtils.getNameIdentifierElement(aClass),
133 | MessageFormat.format("Class {0} is listed as a ModuleComponent in {1} and should not be obfuscated", className, PluginManager.PLUGIN_XML));
134 | }
135 | if (actionClasses.contains(className))
136 | {
137 | holder.registerProblem(InspectionUtils.getNameIdentifierElement(aClass),
138 | MessageFormat.format("Class {0} is listed as an Action in {1} and should not be obfuscated", className, PluginManager.PLUGIN_XML));
139 | }
140 | if (extensionClasses.contains(className))
141 | {
142 | holder.registerProblem(InspectionUtils.getNameIdentifierElement(aClass),
143 | MessageFormat.format("Class {0} is listed as an Extension in {1} and should not be obfuscated", className, PluginManager.PLUGIN_XML));
144 | }
145 | if (extensionPoints.contains(className))
146 | {
147 | holder.registerProblem(InspectionUtils.getNameIdentifierElement(aClass),
148 | MessageFormat.format("Class {0} is listed as an ExtensionPoint in {1} and should not be obfuscated", className, PluginManager.PLUGIN_XML));
149 | }
150 | }
151 | }
152 |
153 | super.visitClass(aClass);
154 | }
155 | };
156 | }
157 |
158 | private void extractElementClassNames(@NotNull Collection classNames, @Nullable Element[] elements)
159 | {
160 | if (elements != null)
161 | {
162 | for (Element element : elements)
163 | {
164 | final List children = element.getChildren();
165 | if (children != null)
166 | {
167 | for (Object child : children)
168 | {
169 | if (child instanceof Element)
170 | {
171 | final Element childElement = (Element) child;
172 | addIfNotNullAttribute(classNames, childElement, "class");
173 | addIfNotNullAttribute(classNames, childElement, "interface");
174 | addIfNotNullAttribute(classNames, childElement, "implementation");
175 | addIfNotNullAttribute(classNames, childElement, "serviceInterface");
176 | addIfNotNullAttribute(classNames, childElement, "serviceImplementation");
177 | }
178 | }
179 | }
180 | }
181 | }
182 | }
183 |
184 | private void addIfNotNullAttribute(@NotNull Collection classNames, @NotNull Element element, String attributeName)
185 | {
186 | addIfNotNull(classNames, element.getAttributeValue(attributeName));
187 | }
188 |
189 | private void extractComponentClassNames(@NotNull Collection classes, @Nullable ComponentConfig[] components)
190 | {
191 | if (components != null)
192 | {
193 | for (ComponentConfig component : components)
194 | {
195 | addIfNotNull(classes, component.getInterfaceClass());
196 | addIfNotNull(classes, component.getImplementationClass());
197 | addIfNotNull(classes, component.getHeadlessImplementationClass());
198 | }
199 | }
200 | }
201 |
202 | private void addIfNotNull(@NotNull Collection names, @Nullable String name)
203 | {
204 | if (name != null)
205 | {
206 | names.add(name);
207 | }
208 | }
209 | }
210 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/inspection/GuardInspection.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.inspection;
18 |
19 | import com.intellij.codeInspection.ProblemsHolder;
20 | import com.intellij.codeInspection.LocalQuickFix;
21 | import com.intellij.codeInspection.ProblemHighlightType;
22 | import com.intellij.psi.*;
23 | import com.github.intelliguard.facet.GuardFacetConfiguration;
24 | import com.github.intelliguard.model.Keeper;
25 | import com.github.intelliguard.util.PsiUtils;
26 | import com.github.intelliguard.util.InspectionUtils;
27 | import com.github.intelliguard.fix.RemoveKeepFix;
28 | import com.github.intelliguard.fix.AddKeepFix;
29 | import org.jetbrains.annotations.NotNull;
30 | import org.jetbrains.annotations.Nls;
31 |
32 | import java.util.Collection;
33 | import java.util.ArrayList;
34 |
35 | /**
36 | * Created by IntelliJ IDEA.
37 | * User: Ronnie
38 | * Date: 2009-nov-02
39 | * Time: 19:04:46
40 | */
41 | public class GuardInspection extends GuardInspectionBase
42 | {
43 | private static final String STATIC_DESCRIPTION = "This inspection detects if a class, method or field will be " +
44 | "obfuscated according to the current configuration, and offers quick-fixes to either keep from obfuscation " +
45 | "or remove an existing keeper.";
46 |
47 | @Nls
48 | @NotNull
49 | public String getDisplayName()
50 | {
51 | return "Obfuscation options";
52 | }
53 |
54 | @NotNull
55 | public String getShortName()
56 | {
57 | return "GuardInspection";
58 | }
59 |
60 | @Override
61 | public String getStaticDescription()
62 | {
63 | return STATIC_DESCRIPTION;
64 | }
65 |
66 | @NotNull
67 | @Override
68 | public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly)
69 | {
70 | return new JavaElementVisitor()
71 | {
72 | public void visitReferenceExpression(PsiReferenceExpression expression)
73 | {
74 | }
75 |
76 | @Override
77 | public void visitClass(PsiClass aClass)
78 | {
79 | if (!isOnTheFly)
80 | {
81 | return;
82 | }
83 |
84 | GuardFacetConfiguration configuration = getLocalConfiguration();
85 | if (configuration != null)
86 | {
87 | if (configuration.isKeptByMainClass(aClass))
88 | {
89 | holder.registerProblem(InspectionUtils.getNameIdentifierElement(aClass), "Class is not obfuscated due to Main-Class", ProblemHighlightType.INFORMATION);
90 | }
91 | else
92 | {
93 | final Keeper[] configuredGuardKeepers = configuration.findConfiguredGuardKeepers(aClass);
94 | if (configuredGuardKeepers.length != 0)
95 | {
96 | holder.registerProblem(InspectionUtils.getNameIdentifierElement(aClass), "Class is not obfuscated", ProblemHighlightType.INFORMATION, createRemoveKeeperFixes(configuration, configuredGuardKeepers, aClass));
97 | }
98 | else
99 | {
100 | holder.registerProblem(InspectionUtils.getNameIdentifierElement(aClass), "Class is obfuscated", ProblemHighlightType.INFORMATION, createAddClassKeeperFixes(configuration, aClass));
101 | }
102 | }
103 | }
104 |
105 | super.visitClass(aClass);
106 | }
107 |
108 | @Override
109 | public void visitField(PsiField field)
110 | {
111 | if (!isOnTheFly)
112 | {
113 | return;
114 | }
115 |
116 | GuardFacetConfiguration configuration = getLocalConfiguration();
117 | if (configuration != null)
118 | {
119 | final Keeper[] configuredGuardKeepers = configuration.findConfiguredGuardKeepers(field);
120 | if (configuredGuardKeepers.length != 0)
121 | {
122 | holder.registerProblem(InspectionUtils.getNameIdentifierElement(field), "Field is not obfuscated", ProblemHighlightType.INFORMATION, createRemoveKeeperFixes(configuration, configuredGuardKeepers, field));
123 | }
124 | else
125 | {
126 | holder.registerProblem(InspectionUtils.getNameIdentifierElement(field), "Field is obfuscated", ProblemHighlightType.INFORMATION, createAddFieldKeeperFixes(configuration, field));
127 | }
128 | }
129 |
130 | super.visitField(field);
131 | }
132 |
133 | @Override
134 | public void visitMethod(PsiMethod method)
135 | {
136 | if (!isOnTheFly)
137 | {
138 | return;
139 | }
140 |
141 | GuardFacetConfiguration configuration = getLocalConfiguration();
142 | if (configuration != null && !InspectionUtils.isDefinedInLibrary(method))
143 | {
144 | if (configuration.isKeptByMainClass(method))
145 | {
146 | holder.registerProblem(InspectionUtils.getNameIdentifierElement(method), "Method is not obfuscated due to Main-Class", ProblemHighlightType.INFORMATION);
147 | }
148 | else
149 | {
150 | final Keeper[] configuredGuardKeepers = configuration.findConfiguredGuardKeepers(method);
151 | if (configuredGuardKeepers.length != 0)
152 | {
153 | holder.registerProblem(InspectionUtils.getNameIdentifierElement(method), "Method is not obfuscated", ProblemHighlightType.INFORMATION, createRemoveKeeperFixes(configuration, configuredGuardKeepers, method));
154 | }
155 | else
156 | {
157 | holder.registerProblem(InspectionUtils.getNameIdentifierElement(method), "Method is obfuscated", ProblemHighlightType.INFORMATION, createAddMethodKeeperFixes(configuration, method));
158 | }
159 | }
160 | }
161 |
162 | super.visitMethod(method);
163 | }
164 | };
165 | }
166 |
167 | private LocalQuickFix[] createAddClassKeeperFixes(final GuardFacetConfiguration configuration, final PsiClass aClass)
168 | {
169 | Collection fixes = new ArrayList();
170 |
171 | Keeper keeper = new Keeper();
172 | keeper.setType(Keeper.Type.CLASS);
173 | keeper.setName(PsiUtils.getKeeperName(aClass));
174 |
175 | fixes.add(new AddKeepFix(configuration, keeper, aClass));
176 |
177 | return fixes.toArray(new LocalQuickFix[fixes.size()]);
178 | }
179 |
180 | private LocalQuickFix[] createAddFieldKeeperFixes(final GuardFacetConfiguration configuration, final PsiField field)
181 | {
182 | Collection fixes = new ArrayList();
183 | String name = field.getName();
184 |
185 | Keeper keeper = new Keeper();
186 | keeper.setType(Keeper.Type.FIELD);
187 | keeper.setName(name);
188 | PsiClass containingClass = field.getContainingClass();
189 | if (containingClass != null)
190 | {
191 | keeper.setClazz(PsiUtils.getKeeperName(containingClass));
192 | fixes.add(new AddKeepFix(configuration, keeper, field));
193 | }
194 |
195 | keeper = new Keeper();
196 | keeper.setType(Keeper.Type.FIELD);
197 | keeper.setName(name);
198 | fixes.add(new AddKeepFix(configuration, keeper, field));
199 |
200 | return fixes.toArray(new LocalQuickFix[fixes.size()]);
201 | }
202 |
203 | private LocalQuickFix[] createAddMethodKeeperFixes(final GuardFacetConfiguration configuration, final PsiMethod method)
204 | {
205 | if (method.isConstructor())
206 | {
207 | return new LocalQuickFix[0];
208 | }
209 | final PsiMethod[] superMethods = method.findDeepestSuperMethods();
210 | if (superMethods.length != 0)
211 | {
212 | return new LocalQuickFix[0];
213 | }
214 |
215 | Collection fixes = new ArrayList();
216 | String signature = PsiUtils.getSignatureString(method);
217 |
218 | Keeper keeper = new Keeper();
219 | keeper.setType(Keeper.Type.METHOD);
220 | keeper.setName(signature);
221 | PsiClass containingClass = method.getContainingClass();
222 | if (containingClass != null)
223 | {
224 | keeper.setClazz(PsiUtils.getKeeperName(containingClass));
225 | fixes.add(new AddKeepFix(configuration, keeper, method));
226 | }
227 |
228 | keeper = new Keeper();
229 | keeper.setType(Keeper.Type.METHOD);
230 | keeper.setName(signature);
231 | fixes.add(new AddKeepFix(configuration, keeper, method));
232 |
233 | return fixes.toArray(new LocalQuickFix[fixes.size()]);
234 | }
235 |
236 | private LocalQuickFix[] createRemoveKeeperFixes(final GuardFacetConfiguration configuration, final Keeper[] keepers, final PsiElement element)
237 | {
238 | if (element instanceof PsiMethod)
239 | {
240 | PsiMethod psiMethod = (PsiMethod) element;
241 | if (psiMethod.isConstructor())
242 | {
243 | return new LocalQuickFix[0];
244 | }
245 | }
246 | Collection fixes = new ArrayList();
247 | for (Keeper keeper : keepers)
248 | {
249 | fixes.add(new RemoveKeepFix(configuration, keeper, element));
250 | }
251 | return fixes.toArray(new LocalQuickFix[fixes.size()]);
252 | }
253 | }
254 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/ui/JarOptionsForm.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 Ronnie Kolehmainen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.intelliguard.ui;
18 |
19 | import com.intellij.openapi.ui.TextFieldWithBrowseButton;
20 | import com.intellij.openapi.module.Module;
21 | import com.intellij.openapi.util.IconLoader;
22 | import com.intellij.openapi.fileTypes.FileTypeManager;
23 | import com.github.intelliguard.facet.GuardFacet;
24 | import com.github.intelliguard.facet.GuardFacetConfiguration;
25 | import com.github.intelliguard.model.JarConfig;
26 | import com.github.intelliguard.util.ModuleUtils;
27 |
28 | import javax.swing.*;
29 | import javax.swing.event.TreeSelectionListener;
30 | import javax.swing.event.TreeSelectionEvent;
31 | import javax.swing.tree.*;
32 | import java.awt.event.ActionListener;
33 | import java.awt.event.ActionEvent;
34 | import java.awt.*;
35 | import java.io.File;
36 |
37 | import org.jetbrains.annotations.NotNull;
38 |
39 | /**
40 | * Created by IntelliJ IDEA.
41 | * User: Ronnie
42 | * Date: 2009-okt-31
43 | * Time: 12:10:49
44 | */
45 | public class JarOptionsForm
46 | {
47 | private JCheckBox executeMakeCheckBox;
48 | private JButton addButton;
49 | private JButton removeButton;
50 | private JTree jarContent;
51 | private JCheckBox linkLibrariesInManifestCheckBox;
52 | private JTextField librariesRelativePath;
53 | private JPanel contentPane;
54 | private TextFieldWithBrowseButton jarPath;
55 | private TextFieldWithBrowseButton obfuscatedJarPath;
56 | private TextFieldWithBrowseButton mainClass;
57 | private static final Icon FOLDER_ICON = IconLoader.getIcon("/nodes/folder.png");
58 | private static final Icon MODULE_ICON = IconLoader.getIcon("/nodes/moduleGroup.png");
59 | private static final Icon JAR_ICON = IconLoader.getIcon("/fileTypes/archive.png");
60 |
61 | public JarOptionsForm(@NotNull GuardFacet guardFacet)
62 | {
63 | final Module module = guardFacet.getModule();
64 | final GuardFacetConfiguration facetConfiguration = guardFacet.getConfiguration();
65 |
66 | mainClass.getTextField().setText(facetConfiguration.mainclass == null ? "" : facetConfiguration.mainclass);
67 | mainClass.addActionListener(new MainClassChooser(module, mainClass));
68 |
69 | jarPath.getTextField().setText(facetConfiguration.inFile == null ? "" : facetConfiguration.inFile);
70 | jarPath.addActionListener(new ActionListener()
71 | {
72 | public void actionPerformed(ActionEvent e)
73 | {
74 | JFileChooser chooser = FileChooserFactory.createSaveJarChooser(jarPath.getText(), module.getModuleFilePath());
75 | int res = chooser.showSaveDialog(contentPane);
76 | if (res == JFileChooser.APPROVE_OPTION && chooser.getFileFilter().accept(chooser.getSelectedFile()))
77 | {
78 | jarPath.getTextField().setText(chooser.getSelectedFile().getAbsolutePath());
79 | }
80 | }
81 | });
82 |
83 | obfuscatedJarPath.getTextField().setText(facetConfiguration.outFile == null ? "" : facetConfiguration.outFile);
84 | obfuscatedJarPath.addActionListener(new ActionListener()
85 | {
86 | public void actionPerformed(ActionEvent e)
87 | {
88 | JFileChooser chooser = FileChooserFactory.createSaveJarChooser(obfuscatedJarPath.getText(), jarPath.getText(), module.getModuleFilePath());
89 | int res = chooser.showSaveDialog(contentPane);
90 | if (res == JFileChooser.APPROVE_OPTION && chooser.getFileFilter().accept(chooser.getSelectedFile()))
91 | {
92 | obfuscatedJarPath.getTextField().setText(chooser.getSelectedFile().getAbsolutePath());
93 | }
94 | }
95 | });
96 |
97 | linkLibrariesInManifestCheckBox.setSelected(facetConfiguration.jarConfig.getLinkLibraries() != null);
98 | librariesRelativePath.setText(facetConfiguration.jarConfig.getLinkLibraries() == null ? "/" : facetConfiguration.jarConfig.getLinkLibraries());
99 | librariesRelativePath.setEnabled(linkLibrariesInManifestCheckBox.isSelected());
100 | linkLibrariesInManifestCheckBox.addActionListener(new ActionListener()
101 | {
102 | public void actionPerformed(ActionEvent e)
103 | {
104 | librariesRelativePath.setEnabled(linkLibrariesInManifestCheckBox.isSelected());
105 | }
106 | });
107 |
108 | executeMakeCheckBox.setSelected(true); // TODO: read from settings
109 |
110 | final MyTreeModel myTreeModel = new MyTreeModel(guardFacet);
111 | jarContent.setModel(myTreeModel);
112 | jarContent.setCellRenderer(new MyCellRenderer(guardFacet));
113 |
114 | addButton.addActionListener(new ActionListener()
115 | {
116 | public void actionPerformed(ActionEvent e)
117 | {
118 | JFileChooser chooser = FileChooserFactory.createModuleFileChooser(module);
119 | int res = chooser.showOpenDialog(contentPane);
120 | if (res == JFileChooser.APPROVE_OPTION)
121 | {
122 | myTreeModel.addEntry(chooser.getSelectedFile().getAbsolutePath());
123 | }
124 | }
125 | });
126 | removeButton.addActionListener(new ActionListener()
127 | {
128 | public void actionPerformed(ActionEvent e)
129 | {
130 | TreePath selectionPath = jarContent.getSelectionPath();
131 | if (selectionPath.getParentPath() == null)
132 | {
133 | return;
134 | }
135 | Object path = selectionPath.getLastPathComponent();
136 | if (path != null)
137 | {
138 | myTreeModel.removeEntry(String.valueOf(path));
139 | }
140 | }
141 | });
142 |
143 | jarContent.addTreeSelectionListener(new TreeSelectionListener()
144 | {
145 | public void valueChanged(TreeSelectionEvent e)
146 | {
147 | TreePath selectionPath = e.getNewLeadSelectionPath();
148 | removeButton.setEnabled(selectionPath != null && selectionPath.getParentPath() != null);
149 | }
150 | });
151 | removeButton.setEnabled(false);
152 | }
153 |
154 | public JPanel getContentPane()
155 | {
156 | return contentPane;
157 | }
158 |
159 | public String getJarPath()
160 | {
161 | return jarPath.getText();
162 | }
163 |
164 | public String getObfuscatedJarPath()
165 | {
166 | return obfuscatedJarPath.getText();
167 | }
168 |
169 | public String getMainClass()
170 | {
171 | return mainClass.getText();
172 | }
173 |
174 | public String getLibrariesManifestPath()
175 | {
176 | return linkLibrariesInManifestCheckBox.isSelected()
177 | ? librariesRelativePath.getText()
178 | : null;
179 | }
180 |
181 | public boolean getExecuteMake()
182 | {
183 | return executeMakeCheckBox.isSelected();
184 | }
185 |
186 | private static class MyTreeModel extends DefaultTreeModel
187 | {
188 | private JarConfig myConfig;
189 |
190 | public MyTreeModel(GuardFacet guardFacet)
191 | {
192 | super(new MyTreeRoot());
193 |
194 | myConfig = guardFacet.getConfiguration().jarConfig;
195 | }
196 |
197 | public Object getChild(Object parent, int index)
198 | {
199 | if (parent instanceof MyTreeRoot)
200 | {
201 | if (index >= 0 && index < myConfig.getJarEntries().size())
202 | {
203 | return myConfig.getJarEntries().get(index);
204 | }
205 | }
206 | return null;
207 | }
208 |
209 | public int getChildCount(Object parent)
210 | {
211 | if (parent instanceof MyTreeRoot)
212 | {
213 | return myConfig.getJarEntries().size();
214 | }
215 | return 0;
216 | }
217 |
218 | public boolean isLeaf(Object node)
219 | {
220 | return !(node instanceof MyTreeRoot);
221 | }
222 |
223 | public int getIndexOfChild(Object parent, Object child)
224 | {
225 | if (parent instanceof MyTreeRoot)
226 | {
227 | for (int i = 0; i < myConfig.getJarEntries().size(); i++)
228 | {
229 | Object o = myConfig.getJarEntries().get(i);
230 | if (o.equals(child))
231 | {
232 | return i;
233 | }
234 | }
235 | }
236 | return -1;
237 | }
238 |
239 | public void removeEntry(String path)
240 | {
241 | myConfig.removeEntry(path);
242 | fireTreeStructureChanged(path, new String[] {path}, new int[0], new String[] {path});
243 | }
244 |
245 | public void addEntry(String path)
246 | {
247 | myConfig.addEntry(path);
248 | fireTreeStructureChanged(path, new String[] {path}, new int[0], new String[] {path});
249 | }
250 | }
251 |
252 | private static class MyCellRenderer extends DefaultTreeCellRenderer
253 | {
254 | private File moduleOutputDir;
255 |
256 | public MyCellRenderer(GuardFacet guardFacet)
257 | {
258 | moduleOutputDir = ModuleUtils.getModuleOutputDir(guardFacet.getModule());
259 | }
260 |
261 | public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus)
262 | {
263 | super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
264 |
265 | if (value instanceof MyTreeRoot)
266 | {
267 | setIcon(JAR_ICON);
268 | }
269 | else if (value instanceof String)
270 | {
271 | File file = new File(String.valueOf(value));
272 | String filename = file.getName();
273 | if (file.equals(moduleOutputDir))
274 | {
275 | setIcon(MODULE_ICON);
276 | setText("Module compile output");
277 | }
278 | else if (file.isDirectory())
279 | {
280 | setIcon(FOLDER_ICON);
281 | setText("Folder '" + filename + "'");
282 | }
283 | else
284 | {
285 | Icon icon = FileTypeManager.getInstance().getFileTypeByExtension(filename.substring(filename.lastIndexOf('.') + 1)).getIcon();
286 | setIcon(icon);
287 | setText("File '" + filename + "'");
288 | }
289 | }
290 |
291 | return this;
292 | }
293 | }
294 |
295 | private static class MyTreeRoot extends DefaultMutableTreeNode
296 | {
297 | @Override
298 | public boolean isLeaf()
299 | {
300 | return false;
301 | }
302 | }
303 | }
304 |
--------------------------------------------------------------------------------
/src/main/java/com/github/intelliguard/ui/JarOptionsForm.form:
--------------------------------------------------------------------------------
1 |
2 |
212 |
--------------------------------------------------------------------------------