001 /* 002 * Copyright 2010-2013 JetBrains s.r.o. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.jetbrains.jet.plugin; 018 019 import org.jetbrains.annotations.NotNull; 020 import org.jetbrains.annotations.Nullable; 021 import org.jetbrains.jet.lang.psi.*; 022 023 import java.util.Collection; 024 import java.util.List; 025 026 public class JetMainDetector { 027 private JetMainDetector() { 028 } 029 030 public static boolean hasMain(@NotNull List<JetDeclaration> declarations) { 031 return findMainFunction(declarations) != null; 032 } 033 034 public static boolean isMain(@NotNull JetNamedFunction function) { 035 if ("main".equals(function.getName())) { 036 List<JetParameter> parameters = function.getValueParameters(); 037 if (parameters.size() == 1) { 038 JetTypeReference reference = parameters.get(0).getTypeReference(); 039 if (reference != null && reference.getText().equals("Array<String>")) { // TODO correct check 040 return true; 041 } 042 } 043 } 044 return false; 045 } 046 047 @Nullable 048 public static JetNamedFunction getMainFunction(@NotNull Collection<JetFile> files) { 049 for (JetFile file : files) { 050 JetNamedFunction mainFunction = findMainFunction(file.getDeclarations()); 051 if (mainFunction != null) { 052 return mainFunction; 053 } 054 } 055 return null; 056 } 057 058 @Nullable 059 private static JetNamedFunction findMainFunction(@NotNull List<JetDeclaration> declarations) { 060 for (JetDeclaration declaration : declarations) { 061 if (declaration instanceof JetNamedFunction) { 062 JetNamedFunction candidateFunction = (JetNamedFunction) declaration; 063 if (isMain(candidateFunction)) { 064 return candidateFunction; 065 } 066 } 067 } 068 return null; 069 } 070 }