javafx: controller method signature more precise; test

This commit is contained in:
Anna Kozlova
2013-01-30 14:00:32 +04:00
parent cf74807ae8
commit 4ef22db9c9
7 changed files with 120 additions and 5 deletions
@@ -79,6 +79,11 @@ public class JavaFxPropertyAttributeDescriptor implements XmlAttributeDescriptor
}
public static PsiClass getPropertyClass(PsiElement field) {
final PsiClassType classType = getPropertyClassType(field);
return classType != null ? classType.resolve() : null;
}
public static PsiClassType getPropertyClassType(PsiElement field) {
if (field instanceof PsiField) {
final PsiType type = ((PsiField)field).getType();
if (type instanceof PsiClassType) {
@@ -93,7 +98,7 @@ public class JavaFxPropertyAttributeDescriptor implements XmlAttributeDescriptor
if (superClassSubstitutor != null) {
final PsiType propertyType = superClassSubstitutor.substitute(objectProperty.getTypeParameters()[0]);
if (propertyType instanceof PsiClassType) {
return ((PsiClassType)propertyType).resolve();
return (PsiClassType)propertyType;
}
}
}
@@ -19,14 +19,19 @@ import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.codeInsight.daemon.QuickFixActionRegistrar;
import com.intellij.codeInsight.daemon.impl.quickfix.CreateMethodQuickFix;
import com.intellij.codeInsight.quickfix.UnresolvedReferenceQuickFixProvider;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.InheritanceUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlAttributeValue;
import com.intellij.util.ArrayUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.javaFX.fxml.JavaFxCommonClassNames;
import org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyAttributeDescriptor;
import java.util.ArrayList;
import java.util.List;
@@ -36,11 +41,13 @@ import java.util.List;
* Date: 1/16/13
*/
public class JavaFxEventHandlerReference extends PsiReferenceBase<XmlAttributeValue> {
private PsiMethod myEventHandler;
private final PsiClass myCurrentTagClass;
private final PsiMethod myEventHandler;
private final PsiClass myController;
public JavaFxEventHandlerReference(XmlAttributeValue element, final PsiMethod method, PsiClass controller) {
public JavaFxEventHandlerReference(XmlAttributeValue element, PsiClass currentTagClass, final PsiMethod method, PsiClass controller) {
super(element);
myCurrentTagClass = currentTagClass;
myEventHandler = method;
myController = controller;
}
@@ -98,7 +105,29 @@ public class JavaFxEventHandlerReference extends PsiReferenceBase<XmlAttributeVa
}
private static String getHandlerSignature(JavaFxEventHandlerReference ref) {
return "public void " + ref.getElement().getValue().substring(1) + "(" + JavaFxCommonClassNames.JAVAFX_EVENT + " e)";
final XmlAttributeValue element = ref.getElement();
String canonicalText = JavaFxCommonClassNames.JAVAFX_EVENT;
final XmlAttribute xmlAttribute = (XmlAttribute)element.getParent();
final Project project = element.getProject();
if (xmlAttribute != null) {
final PsiField handlerField = ref.myCurrentTagClass.findFieldByName(xmlAttribute.getName(), true);
if (handlerField != null) {
final PsiClassType classType = JavaFxPropertyAttributeDescriptor.getPropertyClassType(handlerField);
if (classType != null) {
final PsiClass eventHandlerClass = JavaPsiFacade.getInstance(project).findClass(JavaFxCommonClassNames.JAVAFX_EVENT_EVENT_HANDLER, GlobalSearchScope.allScope(project));
final PsiTypeParameter[] typeParameters = eventHandlerClass != null ? eventHandlerClass.getTypeParameters() : null;
if (typeParameters != null && typeParameters.length == 1) {
final PsiTypeParameter typeParameter = typeParameters[0];
final PsiSubstitutor substitutor = TypeConversionUtil.getSuperClassSubstitutor(eventHandlerClass, classType);
final PsiType eventType = substitutor.substitute(typeParameter);
if (eventType != null) {
canonicalText = eventType.getCanonicalText();
}
}
}
}
}
return "public void " + element.getValue().substring(1) + "(" + canonicalText + " e)";
}
@NotNull
@@ -67,6 +67,6 @@ class JavaFxEventHandlerReferenceProvider extends JavaFxControllerBasedReference
break;
}
}
return new PsiReference[]{new JavaFxEventHandlerReference(xmlAttributeValue, handlerMethod, controllerClass)};
return new PsiReference[]{new JavaFxEventHandlerReference(xmlAttributeValue, (PsiClass)currentTagClass, handlerMethod, controllerClass)};
}
}
@@ -0,0 +1 @@
public class CreateControllerMethod {}
@@ -0,0 +1,7 @@
import javafx.event.ActionEvent;
public class CreateControllerMethod {
public void bar(ActionEvent actionEvent) {
//To change body of created methods use File | Settings | File Templates.
}
}
@@ -0,0 +1,5 @@
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<GridPane xmlns:fx="http://javafx.com/fxml" fx:controller="CreateControllerMethod">
<Button onAction="#b<caret>ar"/>
</GridPane>
@@ -0,0 +1,68 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.plugins.javaFX.fxml;
import com.intellij.codeInsight.TargetElementUtilBase;
import com.intellij.codeInsight.daemon.DaemonAnalyzerTestCase;
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
import com.intellij.openapi.application.PluginPathManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.testFramework.PsiTestUtil;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class JavaFXQuickfixTest extends DaemonAnalyzerTestCase {
@Override
protected void setUpModule() {
super.setUpModule();
PsiTestUtil.addLibrary(getModule(), "javafx", PluginPathManager.getPluginHomePath("javaFX") + "/testData", "jfxrt.jar");
}
public void testCreateControllerMethod() throws Exception {
doTest("Create Method 'void bar(ActionEvent)'", true);
}
private void doTest(final String actionName) throws Exception {
doTest(actionName, false);
}
private void doTest(final String actionName, boolean changeEditor) throws Exception {
configureByFiles(null, getTestName(true) + ".fxml", getTestName(false) + ".java");
final List<HighlightInfo> infos = doHighlighting();
findAndInvokeIntentionAction(infos, actionName, getEditor(), getFile());
if (changeEditor) {
final PsiElement targetMethod = TargetElementUtilBase.findTargetElement(getEditor(), TargetElementUtilBase.REFERENCED_ELEMENT_ACCEPTED);
assertNotNull(targetMethod);
final VirtualFile file = targetMethod.getContainingFile().getVirtualFile();
assertNotNull(file);
final Editor editor = FileEditorManager.getInstance(getProject()).openTextEditor(new OpenFileDescriptor(getProject(), file), true);
assertNotNull(editor);
setActiveEditor(editor);
}
checkResultByFile(getTestName(false) + "_after.java");
}
@NotNull
@Override
protected String getTestDataPath() {
return PluginPathManager.getPluginHomePath("javaFX") + "/testData/quickfix/";
}
}