private static boolean isActionEnabled(final AnActionEvent e) { Project project = e.getData(CommonDataKeys.PROJECT); if (project == null) return false; Editor editor = e.getData(CommonDataKeys.EDITOR); if (editor == null) return false; int offset = editor.getCaretModel().getOffset(); PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()); if (file == null) return false; PsiElement element = file.findElementAt(offset); if (element == null) return false; PsiClass psiClass = PsiTreeUtil.getParentOfType(element, PsiClass.class); if (psiClass == null) return false; if (PsiMethodUtil.findMainMethod(psiClass) != null) return false; if (FormClassIndex.findFormsBoundToClass(project, psiClass).isEmpty()) return false; return true; }
private static boolean isActionEnabled(final AnActionEvent e) { Project project = e.getData(PlatformDataKeys.PROJECT); if (project == null) return false; Editor editor = e.getData(PlatformDataKeys.EDITOR); if (editor == null) return false; int offset = editor.getCaretModel().getOffset(); PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()); if (file == null) return false; PsiElement element = file.findElementAt(offset); if (element == null) return false; PsiClass psiClass = PsiTreeUtil.getParentOfType(element, PsiClass.class); if (psiClass == null) return false; if (PsiMethodUtil.findMainMethod(psiClass) != null) return false; if (FormClassIndex.findFormsBoundToClass(psiClass).isEmpty()) return false; return true; }
private static boolean isActionEnabled(final AnActionEvent e) { Project project = e.getData(CommonDataKeys.PROJECT); if (project == null) return false; Editor editor = e.getData(PlatformDataKeys.EDITOR); if (editor == null) return false; int offset = editor.getCaretModel().getOffset(); PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()); if (file == null) return false; PsiElement element = file.findElementAt(offset); if (element == null) return false; PsiClass psiClass = PsiTreeUtil.getParentOfType(element, PsiClass.class); if (psiClass == null) return false; if (PsiMethodUtil.findMainMethod(psiClass) != null) return false; if (FormClassIndex.findFormsBoundToClass(psiClass).isEmpty()) return false; return true; }
@Nullable @Override public Info getInfo(final PsiElement e) { if(isIdentifier(e)) { PsiElement element = e.getParent(); if(element instanceof PsiClass && PsiMethodUtil.findMainInClass((PsiClass) element) != null || element instanceof PsiMethod && "main".equals(((PsiMethod) element).getName()) && PsiMethodUtil.isMainMethod((PsiMethod) element)) { final AnAction[] actions = ExecutorAction.getActions(0); return new Info(AllIcons.RunConfigurations.TestState.Run, element1 -> StringUtil.join(ContainerUtil.mapNotNull(actions, action -> getText(action, element1)), "\n"), actions); } } return null; }
@Nullable private static PsiMethod findMain(PsiElement element) { PsiMethod method; while((method = PsiTreeUtil.getParentOfType(element, PsiMethod.class)) != null) { if(PsiMethodUtil.isMainMethod(method)) { return method; } else { element = method.getParent(); } } return null; }
private void createUIComponents() { myMainClass = new LabeledComponent<>(); myMainClass.setComponent(new EditorTextFieldWithBrowseButton(myProject, true, (declaration, place) -> { if (declaration instanceof PsiClass) { final PsiClass aClass = (PsiClass) declaration; if (ConfigurationUtil.MAIN_CLASS.value(aClass) && PsiMethodUtil.findMainMethod(aClass) != null) { return JavaCodeFragment.VisibilityChecker.Visibility.VISIBLE; } } return JavaCodeFragment.VisibilityChecker.Visibility.NOT_VISIBLE; } )); }
public boolean isEntryPoint(@NotNull PsiElement element) { final Project project = element.getProject(); final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); if (element instanceof PsiMethod && isAddMainsEnabled() && PsiClassImplUtil.isMainOrPremainMethod((PsiMethod)element)) { return true; } if (element instanceof PsiClass) { PsiClass aClass = (PsiClass)element; final PsiClass applet = psiFacade.findClass("java.applet.Applet", GlobalSearchScope.allScope(project)); if (isAddAppletEnabled() && applet != null && aClass.isInheritor(applet, true)) { return true; } final PsiClass servlet = psiFacade.findClass("javax.servlet.Servlet", GlobalSearchScope.allScope(project)); if (isAddServletEnabled() && servlet != null && aClass.isInheritor(servlet, true)) { return true; } if (isAddMainsEnabled() && PsiMethodUtil.hasMainMethod(aClass)) return true; } if (element instanceof PsiModifierListOwner) { final EntryPointsManager entryPointsManager = EntryPointsManager.getInstance(project); if (entryPointsManager.isEntryPoint(element)) return true; } for (EntryPoint extension : myExtensions) { if (extension.isEntryPoint(element)) { return true; } } final ImplicitUsageProvider[] implicitUsageProviders = Extensions.getExtensions(ImplicitUsageProvider.EP_NAME); for (ImplicitUsageProvider provider : implicitUsageProviders) { if (provider.isImplicitUsage(element)) return true; } return false; }
protected boolean checkClass(@NotNull HeaderValuePart valuePart, @NotNull PsiClass aClass, @NotNull AnnotationHolder holder) { if (!PsiMethodUtil.hasMainMethod(aClass)) { holder.createErrorAnnotation(valuePart.getHighlightingRange(), ManifestBundle.message("header.main.class.invalid")); return true; } return false; }
@Override public void checkConfiguration() throws RuntimeConfigurationException { JavaParametersUtil.checkAlternativeJRE(this); final JavaRunConfigurationModule configurationModule = getConfigurationModule(); final PsiClass psiClass = configurationModule.checkModuleAndClassName(MAIN_CLASS_NAME, ExecutionBundle.message("no.main.class.specified.error.text")); if (!PsiMethodUtil.hasMainMethod(psiClass)) { throw new RuntimeConfigurationWarning(ExecutionBundle.message("main.method.not.found.in.class.error.message", MAIN_CLASS_NAME)); } ProgramParametersUtil.checkWorkingDirectoryExist(this, getProject(), configurationModule.getModule()); JavaRunConfigurationExtensionManager.checkConfigurationIsValid(this); }
private void createUIComponents() { myMainClass = new LabeledComponent<EditorTextFieldWithBrowseButton>(); myMainClass.setComponent(new EditorTextFieldWithBrowseButton(myProject, true, new JavaCodeFragment.VisibilityChecker() { @Override public Visibility isDeclarationVisible(PsiElement declaration, PsiElement place) { if (declaration instanceof PsiClass) { final PsiClass aClass = (PsiClass)declaration; if (ConfigurationUtil.MAIN_CLASS.value(aClass) && PsiMethodUtil.findMainMethod(aClass) != null || place.getParent() != null && myModuleSelector.findClass(((PsiClass)declaration).getQualifiedName()) != null) { return Visibility.VISIBLE; } } return Visibility.NOT_VISIBLE; } })); }
@Nullable @Override public Info getInfo(PsiElement e) { if (isIdentifier(e)) { PsiElement element = e.getParent(); if (element instanceof PsiClass && PsiMethodUtil.findMainInClass((PsiClass)element) != null) return new Info(ApplicationConfigurationType.getInstance().getIcon(), null, ExecutorAction.getActions(0)); if (element instanceof PsiMethod && "main".equals(((PsiMethod)element).getName()) && PsiMethodUtil.isMainMethod((PsiMethod)element)) return new Info(ApplicationConfigurationType.getInstance().getIcon(), null, ExecutorAction.getActions(0)); } return null; }
@Nullable private static PsiMethod findMain(PsiElement element) { PsiMethod method; while ((method = PsiTreeUtil.getParentOfType(element, PsiMethod.class)) != null) { if (PsiMethodUtil.isMainMethod(method)) return method; else element = method.getParent(); } return null; }
public boolean isAccepted(final PsiClass aClass) { return ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() { @Override public Boolean compute() { return PsiMethodUtil.MAIN_CLASS.value(aClass) && PsiMethodUtil.hasMainMethod(aClass); } }); }
@Override public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) { super.visitMethodCallExpression(expression); final PsiReferenceExpression methodExpression = expression.getMethodExpression(); @NonNls final String methodName = methodExpression.getReferenceName(); if (!"exit".equals(methodName) && !"halt".equals(methodName)) { return; } final PsiMethod containingMethod = PsiTreeUtil.getParentOfType(expression, PsiMethod.class, true, PsiClass.class, PsiLambdaExpression.class); if (ignoreInMainMethod && PsiMethodUtil.isMainMethod(containingMethod)) { return; } final PsiMethod method = expression.resolveMethod(); if (method == null) { return; } final PsiParameterList parameterList = method.getParameterList(); if (parameterList.getParametersCount() != 1) { return; } final PsiParameter[] parameters = parameterList.getParameters(); final PsiType parameterType = parameters[0].getType(); if (!parameterType.equals(PsiType.INT)) { return; } final PsiClass aClass = method.getContainingClass(); if (aClass == null) { return; } final String className = aClass.getQualifiedName(); if (!"java.lang.System".equals(className) && !"java.lang.Runtime".equals(className)) { return; } registerMethodCallError(expression, "System"); }
@Override public PsiMethod findMainInClass(PsiClass clazz) { final PsiMethod[] launches = clazz.findMethodsByName(LAUNCH_MAIN, true); for (PsiMethod launchMethod : launches) { if (PsiMethodUtil.isMainMethod(launchMethod)) { return launchMethod; } } return null; }
@Override protected boolean doSetupConfigFromContext( BlazeCommandRunConfiguration configuration, ConfigurationContext context, Ref<PsiElement> sourceElement) { PsiClass mainClass = getMainClass(context); if (mainClass == null) { return false; } // Try setting source element to a main method so ApplicationConfigurationProducer // can't override our configuration by producing a more specific one. PsiMethod mainMethod = PsiMethodUtil.findMainMethod(mainClass); if (mainMethod == null) { sourceElement.set(mainClass); } else { sourceElement.set(mainMethod); } TargetIdeInfo target = getTarget(context.getProject(), mainClass); if (target == null) { return false; } configuration.setTargetInfo(target.toTargetInfo()); BlazeCommandRunConfigurationCommonState handlerState = configuration.getHandlerStateIfType(BlazeCommandRunConfigurationCommonState.class); if (handlerState == null) { return false; } handlerState.getCommandState().setCommand(BlazeCommandName.RUN); configuration.setGeneratedName(); return true; }
/** * Creates UI Components */ private void createUIComponents() { myMainClass = new LabeledComponent<>(); myMainClass.setComponent(new EditorTextFieldWithBrowseButton(myProject, true, (declaration, place) -> { if (declaration instanceof PsiClass) { final PsiClass aClass = (PsiClass)declaration; if (ConfigurationUtil.MAIN_CLASS.value(aClass) && PsiMethodUtil.findMainMethod(aClass) != null || place.getParent() != null && myModuleSelector.findClass(((PsiClass)declaration).getQualifiedName()) != null) { return JavaCodeFragment.VisibilityChecker.Visibility.VISIBLE; } } return JavaCodeFragment.VisibilityChecker.Visibility.NOT_VISIBLE; })); }
@Nullable public static PsiMethod findMainMethod(@Nullable PsiElement element) { PsiMethod method; while((method = PsiTreeUtil.getParentOfType(element, PsiMethod.class)) != null) { if(PsiMethodUtil.isMainMethod(method)) { return method; } element = method.getParent(); } return null; }
public void checkConfiguration() throws RuntimeConfigurationException { JavaParametersUtil.checkAlternativeJRE(this); final JavaRunConfigurationModule configurationModule = getConfigurationModule(); final PsiClass psiClass = configurationModule.checkModuleAndClassName(MAIN_CLASS_NAME, ExecutionBundle.message("no.main.class.specified.error.text")); if (!PsiMethodUtil.hasMainMethod(psiClass)) { throw new RuntimeConfigurationWarning(ExecutionBundle.message("main.method.not.found.in.class.error.message", MAIN_CLASS_NAME)); } ProgramParametersUtil.checkWorkingDirectoryExist(this, getProject(), configurationModule.getModule()); JavaRunConfigurationExtensionManager.checkConfigurationIsValid(this); }
private void createUIComponents() { myMainClass = new LabeledComponent<EditorTextFieldWithBrowseButton>(); myMainClass.setComponent(new EditorTextFieldWithBrowseButton(myProject, true, new JavaCodeFragment.VisibilityChecker() { @Override public Visibility isDeclarationVisible(PsiElement declaration, PsiElement place) { if (declaration instanceof PsiClass) { final PsiClass aClass = (PsiClass)declaration; if (ConfigurationUtil.MAIN_CLASS.value(aClass) && PsiMethodUtil.findMainMethod(aClass) != null) { return Visibility.VISIBLE; } } return Visibility.NOT_VISIBLE; } })); }
@Override public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) { super.visitMethodCallExpression(expression); final PsiReferenceExpression methodExpression = expression.getMethodExpression(); @NonNls final String methodName = methodExpression.getReferenceName(); if (!"exit".equals(methodName) && !"halt".equals(methodName)) { return; } final PsiMethod containingMethod = PsiTreeUtil.getParentOfType(expression, PsiMethod.class, true, PsiClass.class); if (ignoreInMainMethod && PsiMethodUtil.isMainMethod(containingMethod)) { return; } final PsiMethod method = expression.resolveMethod(); if (method == null) { return; } final PsiParameterList parameterList = method.getParameterList(); if (parameterList.getParametersCount() != 1) { return; } final PsiParameter[] parameters = parameterList.getParameters(); final PsiType parameterType = parameters[0].getType(); if (!parameterType.equals(PsiType.INT)) { return; } final PsiClass aClass = method.getContainingClass(); if (aClass == null) { return; } final String className = aClass.getQualifiedName(); if (!"java.lang.System".equals(className) && !"java.lang.Runtime".equals(className)) { return; } registerMethodCallError(expression, "System"); }
@NotNull public static PsiElement[] buildEntryPoints(@NotNull Module module) { Query<PsiClass> search = AllClassesSearch.search(GlobalSearchScope.moduleScope(module), module.getProject()); final List<PsiElement> list = new ArrayList<PsiElement>(); search.forEach(psiClass -> { if(PsiMethodUtil.hasMainMethod(psiClass)) { list.add(new IkvmJavaClassAsDotNetTypeElement(psiClass)); } return true; }); return ContainerUtil.toArray(list, PsiElement.ARRAY_FACTORY); }
@Override public void checkConfiguration() throws RuntimeConfigurationException { JavaParametersUtil.checkAlternativeJRE(this); final JavaRunConfigurationModule configurationModule = getConfigurationModule(); final PsiClass psiClass = configurationModule.checkModuleAndClassName(MAIN_CLASS_NAME, ExecutionBundle.message("no.main.class.specified.error.text")); if(!PsiMethodUtil.hasMainMethod(psiClass)) { throw new RuntimeConfigurationWarning(ExecutionBundle.message("main.method.not.found.in.class.error.message", MAIN_CLASS_NAME)); } ProgramParametersUtil.checkWorkingDirectoryExist(this, getProject(), configurationModule.getModule()); JavaRunConfigurationExtensionManager.checkConfigurationIsValid(this); }
@Override public boolean isAccepted(final PsiClass aClass) { return ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() { @Override public Boolean compute() { return PsiMethodUtil.MAIN_CLASS.value(aClass) && PsiMethodUtil.hasMainMethod(aClass); } }); }
public static boolean canBeRunByGroovy(final PsiClass psiClass) { return psiClass instanceof GroovyScriptClass || isRunnable(psiClass) || psiClass instanceof GrTypeDefinition && PsiMethodUtil.hasMainMethod(psiClass); }
@Override protected boolean isContext(ConfigurationContext context) { return super.isContext(context) && !isTestScope && psiClass != null && PsiMethodUtil.hasMainMethod(psiClass); }
public boolean isEntryPoint(@NotNull PsiElement element) { final Project project = element.getProject(); final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); if (element instanceof PsiMethod && isAddMainsEnabled() && PsiClassImplUtil.isMainOrPremainMethod((PsiMethod)element)) { return true; } if (element instanceof PsiClass) { PsiClass aClass = (PsiClass)element; /* if (aClass.isAnnotationType()) { return true; } if (aClass.isEnum()) { return true; } */ final PsiClass applet = psiFacade.findClass("java.applet.Applet", GlobalSearchScope.allScope(project)); if (isAddAppletEnabled() && applet != null && aClass.isInheritor(applet, true)) { return true; } final PsiClass servlet = psiFacade.findClass("javax.servlet.Servlet", GlobalSearchScope.allScope(project)); if (isAddServletEnabled() && servlet != null && aClass.isInheritor(servlet, true)) { return true; } if (isAddMainsEnabled() && PsiMethodUtil.hasMainMethod(aClass)) return true; } if (element instanceof PsiModifierListOwner) { final EntryPointsManager entryPointsManager = EntryPointsManager.getInstance(project); if (entryPointsManager.isEntryPoint((PsiModifierListOwner)element)) return true; //if (AnnotationUtil // .checkAnnotatedUsingPatterns((PsiModifierListOwner)element, entryPointsManager.ADDITIONAL_ANNOTATIONS) || // AnnotationUtil // .checkAnnotatedUsingPatterns((PsiModifierListOwner)element, entryPointsManager.getAdditionalAnnotations())) { // return true; //} } for (EntryPoint extension : myExtensions) { if (extension.isEntryPoint(element)) { return true; } } final ImplicitUsageProvider[] implicitUsageProviders = Extensions.getExtensions(ImplicitUsageProvider.EP_NAME); for (ImplicitUsageProvider provider : implicitUsageProviders) { if (provider.isImplicitUsage(element)) return true; } return false; }
private boolean isMainClass(PsiClass cls) { return ApplicationManager.getApplication().runReadAction((Computable<Boolean>) () -> { return PsiMethodUtil.MAIN_CLASS.value(cls) && PsiMethodUtil.hasMainMethod(cls); }); }
public boolean isEntryPoint(@NotNull PsiElement element) { final Project project = element.getProject(); final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); if(element instanceof PsiMethod && isAddMainsEnabled() && PsiClassImplUtil.isMainOrPremainMethod((PsiMethod) element)) { return true; } if(element instanceof PsiClass) { PsiClass aClass = (PsiClass) element; final PsiClass applet = psiFacade.findClass("java.applet.Applet", GlobalSearchScope.allScope(project)); if(isAddAppletEnabled() && applet != null && aClass.isInheritor(applet, true)) { return true; } final PsiClass servlet = psiFacade.findClass("javax.servlet.Servlet", GlobalSearchScope.allScope(project)); if(isAddServletEnabled() && servlet != null && aClass.isInheritor(servlet, true)) { return true; } if(isAddMainsEnabled() && PsiMethodUtil.hasMainMethod(aClass)) { return true; } } if(element instanceof PsiModifierListOwner) { final EntryPointsManager entryPointsManager = EntryPointsManager.getInstance(project); if(entryPointsManager.isEntryPoint(element)) { return true; } } for(EntryPoint extension : myExtensions) { if(extension.isEntryPoint(element)) { return true; } } final ImplicitUsageProvider[] implicitUsageProviders = Extensions.getExtensions(ImplicitUsageProvider.EP_NAME); for(ImplicitUsageProvider provider : implicitUsageProviders) { if(provider.isImplicitUsage(element)) { return true; } } return false; }