Skip to content

Commit

Permalink
Fix NPE #72
Browse files Browse the repository at this point in the history
- Add @checkfornull to CakePhpModule.forPhpModule and CakePhpModule.getCakePhpDirectory methods
- Check whether return value is null
  • Loading branch information
junichi11 committed Dec 8, 2013
1 parent 489d39a commit 24e8cf1
Show file tree
Hide file tree
Showing 21 changed files with 217 additions and 103 deletions.
6 changes: 5 additions & 1 deletion src/org/cakephp/netbeans/CakePhpFrameworkProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,12 @@ public boolean isInPhpModule(PhpModule phpModule) {
@Override
public File[] getConfigurationFiles(PhpModule phpModule) {
// return all php files from app/config
CakePhpModule cakeModule = CakePhpModule.forPhpModule(phpModule);
if (cakeModule == null) {
return new File[0];
}
List<File> configFiles = new LinkedList<File>();
FileObject config = CakePhpModule.forPhpModule(phpModule).getConfigDirectory(DIR_TYPE.APP);
FileObject config = cakeModule.getConfigDirectory(DIR_TYPE.APP);
assert config != null : "app/config or app/Config not found for CakePHP project " + phpModule.getDisplayName();
if (config.isFolder()) {
Enumeration<? extends FileObject> children = config.getChildren(true);
Expand Down
21 changes: 17 additions & 4 deletions src/org/cakephp/netbeans/CakePhpModuleExtender.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,10 @@ public Set<FileObject> extend(PhpModule phpModule) throws ExtendingException {
CakePreferences.setEnabled(phpModule, Boolean.TRUE);

CakePhpModule module = CakePhpModule.forPhpModule(phpModule);
FileObject config = module.getConfigFile();
FileObject config = null;
if (module != null) {
config = module.getConfigFile();
}

if (config != null) {
// change security string
Expand Down Expand Up @@ -250,11 +253,14 @@ private void createDBFile(PhpModule phpModule) {
})
private void createDatabaseFile(PhpModule phpModule) {
// create database.php file
FileObject configDirectory;
NewProjectConfigurationPanel p = getPanel();
NewProjectConfigurationDetailPanel detailPanel = NewProjectConfigurationDetailPanel.getDefault();
if (p.isDatabasePhp()) {
configDirectory = CakePhpModule.forPhpModule(phpModule).getConfigDirectory(CakePhpModule.DIR_TYPE.APP);
CakePhpModule cakeModule = CakePhpModule.forPhpModule(phpModule);
if (cakeModule == null) {
return;
}
FileObject configDirectory = cakeModule.getConfigDirectory(CakePhpModule.DIR_TYPE.APP);
if (configDirectory == null) {
LOGGER.log(Level.WARNING, Bundle.CakePhpModuleExtender_not_found_configdir());
return;
Expand Down Expand Up @@ -701,7 +707,11 @@ private void createComposerJson(FileObject targetDirectory) throws IOException {
* @param phpModule
*/
private void addComposerAutoload(PhpModule phpModule) {
FileObject configDirectory = CakePhpModule.forPhpModule(phpModule).getConfigDirectory(CakePhpModule.DIR_TYPE.APP);
CakePhpModule cakeModule = CakePhpModule.forPhpModule(phpModule);
if (cakeModule == null) {
return;
}
FileObject configDirectory = cakeModule.getConfigDirectory(CakePhpModule.DIR_TYPE.APP);
if (configDirectory == null) {
return;
}
Expand Down Expand Up @@ -747,6 +757,9 @@ private void addComposerAutoload(PhpModule phpModule) {
*/
private void changeCoreIncludePath(PhpModule phpModule, String appName) {
CakePhpModule cakeModule = CakePhpModule.forPhpModule(phpModule);
if (cakeModule == null) {
return;
}
FileObject webrootDirectory = cakeModule.getWebrootDirectory(DIR_TYPE.APP);
if (webrootDirectory == null) {
LOGGER.log(Level.WARNING, "Not found: webroot directory({0})", phpModule.getDisplayName());
Expand Down
33 changes: 22 additions & 11 deletions src/org/cakephp/netbeans/commands/CakeScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ public static CakeScript forComposer(PhpModule phpModule) throws InvalidPhpExecu

private static FileObject getPath(PhpModule phpModule) {
CakePhpModule module = CakePhpModule.forPhpModule(phpModule);
if (module == null) {
return null;
}
FileObject consoleDirectory = module.getConsoleDirectory(CakePhpModule.DIR_TYPE.APP);
if (consoleDirectory == null) {
LOGGER.log(Level.WARNING, "Not found " + SCRIPT_NAME);
Expand All @@ -188,6 +191,9 @@ public static String getOptionsPath() {

public void runCommand(PhpModule phpModule, List<String> parameters, Runnable postExecution) {
CakePhpModule cakeModule = CakePhpModule.forPhpModule(phpModule);
if (cakeModule == null) {
return;
}
FileObject app = cakeModule.getDirectory(CakePhpModule.DIR_TYPE.APP);
appParams.add("-app"); // NOI18N
appParams.add(app.getPath());
Expand Down Expand Up @@ -465,10 +471,13 @@ private List<FrameworkCommand> getFrameworkCommandsInternalConsole(PhpModule php
}
String[] shells = {CORE_SHELLS_DIRECTORY, VENDORS_SHELLS_DIRECTORY, cakeModule.getAppName() + "/" + VENDORS_SHELLS_DIRECTORY};

for (String shell : shells) {
FileObject shellFileObject = CakePhpModule.getCakePhpDirectory(phpModule).getFileObject(shell);
if (shellFileObject != null) {
shellDirs.add(shellFileObject);
FileObject cakePhpDirectory = CakePhpModule.getCakePhpDirectory(phpModule);
if (cakePhpDirectory != null) {
for (String shell : shells) {
FileObject shellFileObject = cakePhpDirectory.getFileObject(shell);
if (shellFileObject != null) {
shellDirs.add(shellFileObject);
}
}
}

Expand All @@ -494,14 +503,16 @@ private List<FrameworkCommand> getFrameworkCommandsInternalConsole(PhpModule php
private String getShellsPlace(PhpModule phpModule, FileObject shellDir) {
String place = ""; // NOI18N
CakePhpModule cakeModule = CakePhpModule.forPhpModule(phpModule);
String app = cakeModule.getAppName();
FileObject source = CakePhpModule.getCakePhpDirectory(phpModule);
if (source.getFileObject(CORE_SHELLS_DIRECTORY) == shellDir) {
place = "CORE"; // NOI18N
} else if (source.getFileObject(app + "/" + VENDORS_SHELLS_DIRECTORY) == shellDir) {
place = "APP VENDOR"; // NOI18N
} else if (source.getFileObject(VENDORS_SHELLS_DIRECTORY) == shellDir) {
place = "VENDOR"; // NOI18N
if (cakeModule != null && source != null) {
String app = cakeModule.getAppName();
if (source.getFileObject(CORE_SHELLS_DIRECTORY) == shellDir) {
place = "CORE"; // NOI18N
} else if (source.getFileObject(app + "/" + VENDORS_SHELLS_DIRECTORY) == shellDir) {
place = "APP VENDOR"; // NOI18N
} else if (source.getFileObject(VENDORS_SHELLS_DIRECTORY) == shellDir) {
place = "VENDOR"; // NOI18N
}
}
return place;
}
Expand Down
10 changes: 6 additions & 4 deletions src/org/cakephp/netbeans/editor/CakePhpEditorExtender.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ public List<PhpBaseElement> getElementsForCodeCompletion(FileObject fo) {

// get AppController
CakePhpModule cakeModule = CakePhpModule.forPhpModule(phpModule);
FileObject appController = cakeModule.getFile(DIR_TYPE.APP, CakePhpModule.FILE_TYPE.CONTROLLER, "App", null);
if (appController != null) {
for (PhpClass phpClass : parseFields(appController)) {
elements.add(new PhpVariable("$this", phpClass, fo, 0)); // NOI18N
if (cakeModule != null) {
FileObject appController = cakeModule.getFile(DIR_TYPE.APP, CakePhpModule.FILE_TYPE.CONTROLLER, "App", null);
if (appController != null) {
for (PhpClass phpClass : parseFields(appController)) {
elements.add(new PhpVariable("$this", phpClass, fo, 0)); // NOI18N
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,14 @@ private List<String> getAllCommonNames(Type type) {
if (fileType == FILE_TYPE.NONE) {
return Collections.emptyList();
}
List<String> list = new ArrayList<String>();

PhpModule phpModule = getPhpModule();
CakePhpModule module = CakePhpModule.forPhpModule(phpModule);
if (module == null) {
return Collections.emptyList();
}

List<String> list = new ArrayList<String>();
EditorSupport editorSupport = Lookup.getDefault().lookup(EditorSupport.class);
FileObject appDir = module.getDirectory(DIR_TYPE.APP, fileType, null);
list.addAll(getCommonNames(appDir, editorSupport, null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ private List<String> getAllValidations() {
List<String> validations = new ArrayList<String>();
PhpModule phpModule = getPhpModule();
CakePhpModule cakeModule = CakePhpModule.forPhpModule(phpModule);
if (cakeModule == null) {
return validations;
}
FileObject coreDirectory = cakeModule.getDirectory(DIR_TYPE.CORE);
if (coreDirectory == null) {
return validations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ private FileObject getTargetFile(Document doc, String methodName) {
*/
private FileObject getElementFile(Document doc) {
CakePhpModule module = CakePhpModule.forPhpModule(PhpModule.inferPhpModule());
if (module == null) {
return null;
}
FileObject elementFile = null;

// check plugin : Plugin.element
Expand Down
3 changes: 3 additions & 0 deletions src/org/cakephp/netbeans/module/CakePhp3ModuleImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ private String getNamespace(FileObject target) {
}
FileObject parent = target.getParent();
CakePhpModule cakeModule = CakePhpModule.forPhpModule(phpModule);
if (cakeModule == null) {
return namespace;
}
FileObject appDirectory = cakeModule.getDirectory(CakePhpModule.DIR_TYPE.APP);
FileObject coreDirectory = cakeModule.getDirectory(CakePhpModule.DIR_TYPE.CORE);
if (appDirectory == null || coreDirectory == null || parent == null) {
Expand Down
11 changes: 10 additions & 1 deletion src/org/cakephp/netbeans/module/CakePhpModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.cakephp.netbeans.preferences.CakePreferences;
import org.netbeans.api.annotations.common.CheckForNull;
import org.netbeans.modules.php.api.editor.PhpBaseElement;
import org.netbeans.modules.php.api.phpmodule.PhpModule;
import org.netbeans.modules.php.api.util.StringUtils;
Expand Down Expand Up @@ -292,7 +293,11 @@ public String getCurrentPluginName(FileObject currentFile) {
return impl.getCurrentPluginName(currentFile);
}

@CheckForNull
public static FileObject getCakePhpDirectory(PhpModule phpModule) {
if (phpModule == null) {
return null;
}
FileObject sourceDirectory = phpModule.getSourceDirectory();
if (sourceDirectory == null) {
return null;
Expand All @@ -301,7 +306,7 @@ public static FileObject getCakePhpDirectory(PhpModule phpModule) {
String cakePhpDirRelativePath = CakePreferences.getCakePhpDirPath(phpModule);
FileObject cakePhpDirectory;
if (!StringUtils.isEmpty(cakePhpDirRelativePath)) {
cakePhpDirectory = sourceDirectory.getFileObject(CakePreferences.getCakePhpDirPath(phpModule));
cakePhpDirectory = sourceDirectory.getFileObject(cakePhpDirRelativePath);
} else {
cakePhpDirectory = phpModule.getSourceDirectory();
}
Expand Down Expand Up @@ -435,10 +440,14 @@ public Set<String> getAllPluginNames() {
return impl.getAllPluginNames();
}

@CheckForNull
public static CakePhpModule forPhpModule(PhpModule phpModule) {
if (phpModule == null) {
phpModule = PhpModule.inferPhpModule();
}
if (phpModule == null) {
return null;
}
CakePhpModuleFactory factory = CakePhpModuleFactory.getInstance();
return factory.create(phpModule);
}
Expand Down
91 changes: 47 additions & 44 deletions src/org/cakephp/netbeans/ui/actions/CakePhpBaseMenuAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,6 @@
*/
package org.cakephp.netbeans.ui.actions;

import org.cakephp.netbeans.ui.actions.gotos.CakePhpGoToModelsAction;
import org.cakephp.netbeans.ui.actions.gotos.CakePhpGoToFixturesAction;
import org.cakephp.netbeans.ui.actions.gotos.CakePhpGoToSmartAction;
import org.cakephp.netbeans.ui.actions.gotos.CakePhpGoToBehaviorsAction;
import org.cakephp.netbeans.ui.actions.gotos.CakePhpGoToTestCasesAction;
import org.cakephp.netbeans.ui.actions.gotos.CakePhpGoToHelpersAction;
import org.cakephp.netbeans.ui.actions.gotos.CakePhpGoToViewsAction;
import org.cakephp.netbeans.ui.actions.gotos.CakePhpGoToComponentsAction;
import org.cakephp.netbeans.ui.actions.gotos.CakePhpGoToControllersAction;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
Expand All @@ -59,6 +50,15 @@
import javax.swing.JPopupMenu;
import javax.swing.text.StyledDocument;
import org.cakephp.netbeans.CakePhp;
import org.cakephp.netbeans.ui.actions.gotos.CakePhpGoToBehaviorsAction;
import org.cakephp.netbeans.ui.actions.gotos.CakePhpGoToComponentsAction;
import org.cakephp.netbeans.ui.actions.gotos.CakePhpGoToControllersAction;
import org.cakephp.netbeans.ui.actions.gotos.CakePhpGoToFixturesAction;
import org.cakephp.netbeans.ui.actions.gotos.CakePhpGoToHelpersAction;
import org.cakephp.netbeans.ui.actions.gotos.CakePhpGoToModelsAction;
import org.cakephp.netbeans.ui.actions.gotos.CakePhpGoToSmartAction;
import org.cakephp.netbeans.ui.actions.gotos.CakePhpGoToTestCasesAction;
import org.cakephp.netbeans.ui.actions.gotos.CakePhpGoToViewsAction;
import org.cakephp.netbeans.util.CakePhpUtils;
import org.cakephp.netbeans.util.CakeVersion;
import org.netbeans.modules.php.api.phpmodule.PhpModule;
Expand Down Expand Up @@ -225,48 +225,51 @@ private class CakeToolbarPresenter extends JButton {
private JPopupMenu popup;

CakeToolbarPresenter() {
this.setIcon(ImageUtilities.loadImageIcon(CakePhp.CAKE_ICON_16, true));
this.popup = new JPopupMenu();
PhpModule phpModule = PhpModule.inferPhpModule();
if (CakePhpUtils.isCakePHP(phpModule)) {
this.setIcon(ImageUtilities.loadImageIcon(CakePhp.CAKE_ICON_16, true));
this.popup = new JPopupMenu();

// add actions
// smart go to
popup.add(CakePhpGoToSmartAction.getInstance());
popup.add(CakePhpGoToTestCasesAction.getInstance());
popup.add(CakePhpGoToControllersAction.getInstance());
popup.add(CakePhpGoToViewsAction.getInstance());
popup.add(CakePhpGoToModelsAction.getInstance());
popup.add(CakePhpGoToComponentsAction.getInstance());
popup.add(CakePhpGoToHelpersAction.getInstance());
popup.add(CakePhpGoToBehaviorsAction.getInstance());
popup.add(CakePhpGoToFixturesAction.getInstance());
// add actions
// smart go to
popup.add(CakePhpGoToSmartAction.getInstance());
popup.add(CakePhpGoToTestCasesAction.getInstance());
popup.add(CakePhpGoToControllersAction.getInstance());
popup.add(CakePhpGoToViewsAction.getInstance());
popup.add(CakePhpGoToModelsAction.getInstance());
popup.add(CakePhpGoToComponentsAction.getInstance());
popup.add(CakePhpGoToHelpersAction.getInstance());
popup.add(CakePhpGoToBehaviorsAction.getInstance());
popup.add(CakePhpGoToFixturesAction.getInstance());

// format
popup.add(FormatPlusAction.getInstance());
// format
popup.add(FormatPlusAction.getInstance());

// create test
DataObject dataObject = getDataObject();
if (dataObject != null) {
popup.add(new RunBakeTestAction(dataObject));
}
// create test
DataObject dataObject = getDataObject();
if (dataObject != null) {
popup.add(new RunBakeTestAction(dataObject));
}

// run action
popup.add(RunActionAction.getInstance());
// run action
popup.add(RunActionAction.getInstance());

// fix namespace
if (CakeVersion.getInstance(PhpModule.inferPhpModule()).isCakePhp(3)) {
FileObject fileObject = getFileObject();
if (fileObject != null && !CakePhpUtils.isCtpFile(fileObject)) {
popup.add(new FixNamespaceAction());
// fix namespace
if (phpModule != null && CakeVersion.getInstance(phpModule).isCakePhp(3)) {
FileObject fileObject = getFileObject();
if (fileObject != null && !CakePhpUtils.isCtpFile(fileObject)) {
popup.add(new FixNamespaceAction());
}
}
}

// add listener
this.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CakeToolbarPresenter.this.popup.show(CakeToolbarPresenter.this, 0, CakeToolbarPresenter.this.getHeight());
}
});
// add listener
this.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CakeToolbarPresenter.this.popup.show(CakeToolbarPresenter.this, 0, CakeToolbarPresenter.this.getHeight());
}
});
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;
import org.cakephp.netbeans.preferences.CakePreferences;
import org.cakephp.netbeans.ui.actions.gotos.items.GoToItem;
import org.cakephp.netbeans.ui.GoToPopup;
import org.cakephp.netbeans.ui.actions.gotos.items.GoToViewItem;
import org.cakephp.netbeans.ui.PopupUtil;
import org.cakephp.netbeans.ui.actions.gotos.items.GoToItem;
import org.cakephp.netbeans.ui.actions.gotos.items.GoToViewItem;
import org.cakephp.netbeans.util.CakePhpUtils;
import org.cakephp.netbeans.util.CakeVersion;
import org.netbeans.api.editor.EditorRegistry;
Expand Down Expand Up @@ -110,6 +110,9 @@ public boolean goToView() {
// support for only app view file
// TODO support for theme?
PhpModule phpModule = PhpModule.forFileObject(controller);
if (phpModule == null) {
return false;
}
if (CakePreferences.getAutoCreateView(phpModule)) {
try {
view = CakePhpUtils.createView(controller, phpElement);
Expand Down
Loading

0 comments on commit 24e8cf1

Please sign in to comment.