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.asJava;
018    
019    import com.intellij.openapi.application.ApplicationManager;
020    import com.intellij.openapi.diagnostic.Logger;
021    import com.intellij.openapi.progress.ProcessCanceledException;
022    import com.intellij.openapi.project.Project;
023    import com.intellij.openapi.util.Comparing;
024    import com.intellij.openapi.util.io.FileUtil;
025    import com.intellij.openapi.vfs.StandardFileSystems;
026    import com.intellij.openapi.vfs.VirtualFile;
027    import com.intellij.psi.PsiClass;
028    import com.intellij.psi.PsiElement;
029    import com.intellij.psi.PsiMethod;
030    import com.intellij.psi.impl.java.stubs.PsiClassStub;
031    import com.intellij.psi.search.GlobalSearchScope;
032    import com.intellij.psi.stubs.PsiFileStub;
033    import com.intellij.psi.stubs.StubElement;
034    import com.intellij.psi.util.PsiTreeUtil;
035    import com.intellij.util.PathUtil;
036    import com.intellij.util.SmartList;
037    import org.jetbrains.annotations.NotNull;
038    import org.jetbrains.annotations.Nullable;
039    import org.jetbrains.jet.lang.psi.*;
040    import org.jetbrains.jet.lang.resolve.java.JvmAbi;
041    import org.jetbrains.jet.lang.resolve.java.PackageClassUtils;
042    import org.jetbrains.jet.lang.resolve.java.jetAsJava.JetClsMethod;
043    import org.jetbrains.jet.lang.resolve.name.FqName;
044    import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
045    import org.jetbrains.jet.utils.ExceptionUtils;
046    import org.jetbrains.jet.utils.KotlinVfsUtil;
047    
048    import java.io.File;
049    import java.net.MalformedURLException;
050    import java.net.URL;
051    import java.util.*;
052    
053    public class LightClassUtil {
054        private static final Logger LOG = Logger.getInstance(LightClassUtil.class);
055    
056        public static final File BUILT_INS_SRC_DIR = new File("idea/builtinsSrc", KotlinBuiltIns.BUILT_INS_PACKAGE_NAME_STRING);
057    
058        /**
059         * Checks whether the given file is loaded from the location where Kotlin's built-in classes are defined.
060         * As of today, this is idea/builtinsSrc/jet directory and files such as Any.jet, Nothing.jet etc.
061         *
062         * Used to skip JetLightClass creation for built-ins, because built-in classes have no Java counterparts
063         */
064        public static boolean belongsToKotlinBuiltIns(@NotNull JetFile file) {
065            VirtualFile virtualFile = file.getVirtualFile();
066            if (virtualFile != null) {
067                VirtualFile parent = virtualFile.getParent();
068                if (parent != null) {
069                    try {
070                        String jetVfsPathUrl = KotlinVfsUtil.convertFromUrl(getBuiltInsDirUrl());
071                        String fileDirVfsUrl = parent.getUrl();
072                        if (jetVfsPathUrl.equals(fileDirVfsUrl)) {
073                            return true;
074                        }
075                    }
076                    catch (MalformedURLException e) {
077                        LOG.error(e);
078                    }
079                }
080            }
081    
082            // We deliberately return false on error: who knows what weird URLs we might come across out there
083            // it would be a pity if no light classes would be created in such cases
084            return false;
085        }
086    
087        @NotNull
088        public static URL getBuiltInsDirUrl() {
089            String builtInFilePath = "/" + KotlinBuiltIns.BUILT_INS_PACKAGE_NAME_STRING + "/Library.kt";
090    
091            URL url = KotlinBuiltIns.class.getResource(builtInFilePath);
092    
093            if (url == null) {
094                if (ApplicationManager.getApplication().isUnitTestMode()) {
095                    // HACK: Temp code. Get built-in files from the sources when running from test.
096                    try {
097                        return new URL(StandardFileSystems.FILE_PROTOCOL, "",
098                                       FileUtil.toSystemIndependentName(BUILT_INS_SRC_DIR.getAbsolutePath()));
099                    }
100                    catch (MalformedURLException e) {
101                        throw ExceptionUtils.rethrow(e);
102                    }
103                }
104    
105                throw new IllegalStateException("Built-ins file wasn't found at url: " + builtInFilePath);
106            }
107    
108            try {
109                return new URL(url.getProtocol(), url.getHost(), PathUtil.getParentPath(url.getFile()));
110            }
111            catch (MalformedURLException e) {
112                throw new AssertionError(e);
113            }
114        }
115    
116        @Nullable
117        /*package*/ static PsiClass findClass(@NotNull FqName fqn, @NotNull StubElement<?> stub) {
118            if (stub instanceof PsiClassStub && Comparing.equal(fqn.asString(), ((PsiClassStub) stub).getQualifiedName())) {
119                return (PsiClass) stub.getPsi();
120            }
121    
122            if (stub instanceof PsiClassStub || stub instanceof PsiFileStub) {
123                for (StubElement child : stub.getChildrenStubs()) {
124                    PsiClass answer = findClass(fqn, child);
125                    if (answer != null) return answer;
126                }
127            }
128    
129            return null;
130        }
131    
132        @Nullable
133        public static PsiClass getPsiClass(@Nullable JetClassOrObject classOrObject) {
134            if (classOrObject == null) return null;
135            return LightClassGenerationSupport.getInstance(classOrObject.getProject()).getPsiClass(classOrObject);
136        }
137    
138        @Nullable
139        public static PsiMethod getLightClassAccessorMethod(@NotNull JetPropertyAccessor accessor) {
140            return getPsiMethodWrapper(accessor);
141        }
142    
143        @NotNull
144        public static PropertyAccessorsPsiMethods getLightClassPropertyMethods(@NotNull JetProperty property) {
145            JetPropertyAccessor getter = property.getGetter();
146            JetPropertyAccessor setter = property.getSetter();
147    
148            PsiMethod getterWrapper = getter != null ? getLightClassAccessorMethod(getter) : null;
149            PsiMethod setterWrapper = setter != null ? getLightClassAccessorMethod(setter) : null;
150    
151            return extractPropertyAccessors(property, getterWrapper, setterWrapper);
152        }
153    
154        public static PropertyAccessorsPsiMethods getLightClassPropertyMethods(@NotNull JetParameter parameter) {
155            return extractPropertyAccessors(parameter, null, null);
156        }
157    
158        @Nullable
159        public static PsiMethod getLightClassMethod(@NotNull JetNamedFunction function) {
160            return getPsiMethodWrapper(function);
161        }
162    
163        @Nullable
164        private static PsiMethod getPsiMethodWrapper(@NotNull JetDeclaration declaration) {
165            List<PsiMethod> wrappers = getPsiMethodWrappers(declaration, false);
166            return !wrappers.isEmpty() ? wrappers.get(0) : null;
167        }
168    
169        @NotNull
170        private static List<PsiMethod> getPsiMethodWrappers(@NotNull JetDeclaration declaration, boolean collectAll) {
171            PsiClass psiClass = getWrappingClass(declaration);
172            if (psiClass == null) {
173                return Collections.emptyList();
174            }
175    
176            List<PsiMethod> methods = new SmartList<PsiMethod>();
177            for (PsiMethod method : psiClass.getMethods()) {
178                try {
179                    if (method instanceof JetClsMethod && ((JetClsMethod) method).getOrigin() == declaration) {
180                        methods.add(method);
181                        if (!collectAll) {
182                            return methods;
183                        }
184                    }
185                }
186                catch (ProcessCanceledException e) {
187                    throw e;
188                }
189                catch (Throwable e) {
190                    throw new IllegalStateException(
191                            "Error while wrapping declaration " + declaration.getName() +
192                            "Context\n:" +
193                            String.format("=== In file ===\n" +
194                                          "%s\n" +
195                                          "=== On element ===\n" +
196                                          "%s\n" +
197                                          "=== WrappedElement ===\n" +
198                                          "%s\n",
199                                          declaration.getContainingFile().getText(),
200                                          declaration.getText(),
201                                          method.toString()),
202                            e
203                    );
204                }
205            }
206    
207            return methods;
208        }
209    
210        @Nullable
211        private static PsiClass getWrappingClass(@NotNull JetDeclaration declaration) {
212            if (declaration instanceof JetParameter) {
213                JetClass constructorClass = JetPsiUtil.getClassIfParameterIsProperty((JetParameter) declaration);
214                if (constructorClass != null) {
215                    return getPsiClass(constructorClass);
216                }
217            }
218    
219            if (declaration instanceof JetPropertyAccessor) {
220                PsiElement propertyParent = declaration.getParent();
221                assert propertyParent instanceof JetProperty : "JetProperty is expected to be parent of accessor";
222    
223                declaration = (JetProperty) propertyParent;
224            }
225    
226            //noinspection unchecked
227            if (PsiTreeUtil.getParentOfType(declaration, JetFunction.class, JetProperty.class) != null) {
228                // Can't get wrappers for internal declarations. Their classes are not generated during calcStub
229                // with ClassBuilderMode.LIGHT_CLASSES mode, and this produces "Class not found exception" in getDelegate()
230                return null;
231            }
232    
233            PsiElement parent = declaration.getParent();
234    
235            if (parent instanceof JetFile) {
236                // top-level declaration
237                FqName fqName = getPackageClassNameForFile((JetFile) parent);
238                if (fqName != null) {
239                    Project project = declaration.getProject();
240    
241                    return JavaElementFinder.getInstance(project).findClass(fqName.asString(), GlobalSearchScope.allScope(project));
242                }
243            }
244            else if (parent instanceof JetClassBody) {
245                assert parent.getParent() instanceof JetClassOrObject;
246                return getPsiClass((JetClassOrObject) parent.getParent());
247            }
248    
249            return null;
250        }
251    
252        @Nullable
253        private static FqName getPackageClassNameForFile(@NotNull JetFile jetFile) {
254            String packageName = jetFile.getPackageName();
255            return packageName == null ? null : PackageClassUtils.getPackageClassFqName(new FqName(packageName));
256        }
257    
258        private static PropertyAccessorsPsiMethods extractPropertyAccessors(
259                @NotNull JetDeclaration jetDeclaration,
260                @Nullable PsiMethod specialGetter, @Nullable PsiMethod specialSetter
261        ) {
262            PsiMethod getterWrapper = specialGetter;
263            PsiMethod setterWrapper = specialSetter;
264    
265            if (getterWrapper == null || setterWrapper == null) {
266                // If some getter or setter isn't found yet try to get it from wrappers for general declaration
267    
268                List<PsiMethod> wrappers = getPsiMethodWrappers(jetDeclaration, true);
269                assert wrappers.size() <= 2 : "Maximum two wrappers are expected to be generated for declaration: " + jetDeclaration.getText();
270    
271                for (PsiMethod wrapper : wrappers) {
272                    if (wrapper.getName().startsWith(JvmAbi.SETTER_PREFIX)) {
273                        assert setterWrapper == null : String.format(
274                                "Setter accessor isn't expected to be reassigned (old: %s, new: %s)", setterWrapper, wrapper);
275    
276                        setterWrapper = wrapper;
277                    }
278                    else {
279                        assert getterWrapper == null : String.format(
280                                "Getter accessor isn't expected to be reassigned (old: %s, new: %s)", getterWrapper, wrapper);
281    
282                        getterWrapper = wrapper;
283                    }
284                }
285            }
286    
287            return new PropertyAccessorsPsiMethods(getterWrapper, setterWrapper);
288        }
289    
290        public static class PropertyAccessorsPsiMethods implements Iterable<PsiMethod> {
291            private final PsiMethod getter;
292            private final PsiMethod setter;
293            private final Collection<PsiMethod> accessors = new ArrayList<PsiMethod>(2);
294    
295            PropertyAccessorsPsiMethods(@Nullable PsiMethod getter, @Nullable PsiMethod setter) {
296                this.getter = getter;
297                if (getter != null) {
298                    accessors.add(getter);
299                }
300    
301                this.setter = setter;
302                if (setter != null) {
303                    accessors.add(setter);
304                }
305            }
306    
307            @Nullable
308            public PsiMethod getGetter() {
309                return getter;
310            }
311    
312            @Nullable
313            public PsiMethod getSetter() {
314                return setter;
315            }
316    
317            @NotNull
318            @Override
319            public Iterator<PsiMethod> iterator() {
320                return accessors.iterator();
321            }
322        }
323    
324        private LightClassUtil() {
325        }
326    }