Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -254,19 +254,24 @@ public class ClosureCodegen extends ObjectOrClosureCodegen {
|
|||||||
iv.invokespecial(funClass, "<init>", "(Ljet/typeinfo/TypeInfo;)V");
|
iv.invokespecial(funClass, "<init>", "(Ljet/typeinfo/TypeInfo;)V");
|
||||||
|
|
||||||
i = 1;
|
i = 1;
|
||||||
|
int k = 0;
|
||||||
for (Type type : argTypes) {
|
for (Type type : argTypes) {
|
||||||
StackValue.local(0, JetTypeMapper.TYPE_OBJECT).put(JetTypeMapper.TYPE_OBJECT, iv);
|
StackValue.local(0, JetTypeMapper.TYPE_OBJECT).put(JetTypeMapper.TYPE_OBJECT, iv);
|
||||||
StackValue.local(i, type).put(type, iv);
|
StackValue.local(i, type).put(type, iv);
|
||||||
final String fieldName;
|
final String fieldName;
|
||||||
if (captureThis && i == 1) {
|
if (captureThis && i == 1) {
|
||||||
fieldName = "this$0";
|
fieldName = "this$0";
|
||||||
|
i++;
|
||||||
|
k++;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (captureReceiver != null && (captureThis && i == 2 || !captureThis && i == 1)) {
|
if (captureReceiver != null && (captureThis && i == 2 || !captureThis && i == 1)) {
|
||||||
fieldName = "receiver$0";
|
fieldName = "receiver$0";
|
||||||
|
i++;
|
||||||
|
k++;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
fieldName = "$" + (i);
|
fieldName = "$" + (i-k);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -200,6 +200,8 @@ public class JetTypeMapper {
|
|||||||
}
|
}
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
if(name.contains("/"))
|
||||||
|
return name;
|
||||||
if (container != null) {
|
if (container != null) {
|
||||||
if (container instanceof ModuleDescriptor) {
|
if (container instanceof ModuleDescriptor) {
|
||||||
return name;
|
return name;
|
||||||
|
|||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
/*
|
||||||
|
* @author max
|
||||||
|
*/
|
||||||
|
package org.jetbrains.jet.lang.resolve.java;
|
||||||
|
|
||||||
|
import com.intellij.psi.PsiClass;
|
||||||
|
|
||||||
|
public interface JetJavaMirrorMarker extends PsiClass {
|
||||||
|
}
|
||||||
@@ -15,36 +15,47 @@ fun thread(f: fun ()) {
|
|||||||
thread.start()
|
thread.start()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun <T> Int.latch(op: fun CountDownLatch.() : T) : T {
|
||||||
|
val cdl = CountDownLatch(this)
|
||||||
|
val res = cdl.op()
|
||||||
|
cdl.await()
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Int.times(action: fun Int.()) {
|
||||||
|
for(i in 0..this-1)
|
||||||
|
action()
|
||||||
|
}
|
||||||
|
|
||||||
fun main(args: Array<String>) {
|
fun main(args: Array<String>) {
|
||||||
val processors = Runtime.getRuntime().sure().availableProcessors()
|
val processors = Runtime.getRuntime().sure().availableProcessors()
|
||||||
var threadNum = 1
|
var threadNum = 1
|
||||||
while(threadNum <= 1024) {
|
while(threadNum <= 1024) {
|
||||||
val counter = AtomicInteger()
|
val counter = AtomicInteger()
|
||||||
val cdl = CountDownLatch(threadNum)
|
|
||||||
val lock = ReentrantLock()
|
|
||||||
|
|
||||||
val start = System.currentTimeMillis()
|
val start = System.currentTimeMillis()
|
||||||
for(i in 0..threadNum-1) {
|
threadNum.latch{
|
||||||
thread {
|
val lock = ReentrantLock()
|
||||||
while(true) {
|
threadNum.times {
|
||||||
lock.lock()
|
thread {
|
||||||
try {
|
while(true) {
|
||||||
if (counter.get() == 100000000) {
|
lock.lock()
|
||||||
cdl.countDown();
|
try {
|
||||||
break;
|
if (counter.get() == 100000000) {
|
||||||
} else {
|
countDown();
|
||||||
counter.incrementAndGet();
|
break;
|
||||||
|
} else {
|
||||||
|
counter.incrementAndGet();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
lock.unlock()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
finally {
|
|
||||||
lock.unlock()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cdl.await()
|
|
||||||
|
|
||||||
System.out?.println(threadNum.toString() + " " + (System.currentTimeMillis() - start));
|
System.out?.println(threadNum.toString() + " " + (System.currentTimeMillis() - start));
|
||||||
|
|
||||||
if(threadNum < 2 * processors)
|
if(threadNum < 2 * processors)
|
||||||
|
|||||||
@@ -3,55 +3,39 @@
|
|||||||
*/
|
*/
|
||||||
package org.jetbrains.jet.plugin.java;
|
package org.jetbrains.jet.plugin.java;
|
||||||
|
|
||||||
import com.google.common.base.Predicate;
|
|
||||||
import com.google.common.collect.Collections2;
|
|
||||||
import com.intellij.openapi.compiler.ex.CompilerPathsEx;
|
import com.intellij.openapi.compiler.ex.CompilerPathsEx;
|
||||||
import com.intellij.openapi.diagnostic.Logger;
|
|
||||||
import com.intellij.openapi.fileTypes.FileType;
|
import com.intellij.openapi.fileTypes.FileType;
|
||||||
import com.intellij.openapi.fileTypes.FileTypeManager;
|
import com.intellij.openapi.fileTypes.FileTypeManager;
|
||||||
import com.intellij.openapi.project.Project;
|
import com.intellij.openapi.project.Project;
|
||||||
import com.intellij.openapi.roots.ProjectRootManager;
|
import com.intellij.openapi.roots.ProjectRootManager;
|
||||||
import com.intellij.openapi.util.Comparing;
|
import com.intellij.openapi.util.text.StringUtil;
|
||||||
import com.intellij.openapi.util.Key;
|
|
||||||
import com.intellij.openapi.vfs.VirtualFile;
|
import com.intellij.openapi.vfs.VirtualFile;
|
||||||
import com.intellij.psi.*;
|
import com.intellij.psi.*;
|
||||||
import com.intellij.psi.impl.PsiManagerImpl;
|
|
||||||
import com.intellij.psi.impl.compiled.ClsFileImpl;
|
|
||||||
import com.intellij.psi.impl.file.PsiPackageImpl;
|
import com.intellij.psi.impl.file.PsiPackageImpl;
|
||||||
import com.intellij.psi.impl.java.stubs.PsiClassStub;
|
|
||||||
import com.intellij.psi.impl.java.stubs.PsiJavaFileStub;
|
|
||||||
import com.intellij.psi.impl.java.stubs.impl.PsiJavaFileStubImpl;
|
|
||||||
import com.intellij.psi.search.GlobalSearchScope;
|
import com.intellij.psi.search.GlobalSearchScope;
|
||||||
import com.intellij.psi.stubs.PsiClassHolderFileStub;
|
|
||||||
import com.intellij.psi.stubs.StubElement;
|
|
||||||
import com.intellij.util.SmartList;
|
import com.intellij.util.SmartList;
|
||||||
import com.intellij.util.containers.Stack;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.jet.codegen.ClassBuilder;
|
|
||||||
import org.jetbrains.jet.codegen.ClassBuilderFactory;
|
|
||||||
import org.jetbrains.jet.codegen.CodegenUtil;
|
import org.jetbrains.jet.codegen.CodegenUtil;
|
||||||
import org.jetbrains.jet.codegen.GenerationState;
|
|
||||||
import org.jetbrains.jet.lang.psi.JetClassOrObject;
|
import org.jetbrains.jet.lang.psi.JetClassOrObject;
|
||||||
import org.jetbrains.jet.lang.psi.JetDeclaration;
|
import org.jetbrains.jet.lang.psi.JetDeclaration;
|
||||||
import org.jetbrains.jet.lang.psi.JetFile;
|
import org.jetbrains.jet.lang.psi.JetFile;
|
||||||
import org.jetbrains.jet.lang.psi.JetNamespace;
|
import org.jetbrains.jet.lang.psi.JetNamespace;
|
||||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
|
||||||
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacade;
|
|
||||||
import org.jetbrains.jet.plugin.JetFileType;
|
import org.jetbrains.jet.plugin.JetFileType;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
public class JavaElementFinder extends PsiElementFinder {
|
public class JavaElementFinder extends PsiElementFinder {
|
||||||
private static final Logger LOG = Logger.getInstance("#org.jetbrains.jet.plugin.java.JavaElementFinder");
|
|
||||||
|
|
||||||
private final Project project;
|
private final Project project;
|
||||||
|
private final PsiManager psiManager;
|
||||||
|
|
||||||
public JavaElementFinder(Project project) {
|
public JavaElementFinder(Project project) {
|
||||||
this.project = project;
|
this.project = project;
|
||||||
|
psiManager = PsiManager.getInstance(project);
|
||||||
}
|
}
|
||||||
|
|
||||||
private final static Key<PsiJavaFileStub> JAVA_API_STUB = Key.create("JAVA_API_STUB");
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PsiClass findClass(@NotNull String qualifiedName, @NotNull GlobalSearchScope scope) {
|
public PsiClass findClass(@NotNull String qualifiedName, @NotNull GlobalSearchScope scope) {
|
||||||
final PsiClass[] allClasses = findClasses(qualifiedName, scope);
|
final PsiClass[] allClasses = findClasses(qualifiedName, scope);
|
||||||
@@ -65,14 +49,22 @@ public class JavaElementFinder extends PsiElementFinder {
|
|||||||
if (qualifiedName.startsWith("java.")) return PsiClass.EMPTY_ARRAY;
|
if (qualifiedName.startsWith("java.")) return PsiClass.EMPTY_ARRAY;
|
||||||
|
|
||||||
List<PsiClass> answer = new SmartList<PsiClass>();
|
List<PsiClass> answer = new SmartList<PsiClass>();
|
||||||
final List<PsiFile> filesInScope = ensureUpToDate(scope);
|
final List<JetFile> filesInScope = collectProjectFiles(project, scope);
|
||||||
for (PsiFile file : filesInScope) {
|
for (JetFile file : filesInScope) {
|
||||||
final PsiJavaFileStub stub = file.getUserData(JAVA_API_STUB);
|
JetNamespace rootNamespace = file.getRootNamespace();
|
||||||
if (stub == null) continue;
|
final String packageName = CodegenUtil.getFQName(rootNamespace);
|
||||||
|
|
||||||
final String packageName = stub.getPackageName();
|
|
||||||
if (packageName != null && qualifiedName.startsWith(packageName)) {
|
if (packageName != null && qualifiedName.startsWith(packageName)) {
|
||||||
collectClasses(answer, qualifiedName, stub);
|
if (qualifiedName.equals(fqn(packageName, "namespace"))) {
|
||||||
|
answer.add(new JetLightClass(psiManager, file, "namespace"));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (JetDeclaration declaration : rootNamespace.getDeclarations()) {
|
||||||
|
if (declaration instanceof JetClassOrObject) {
|
||||||
|
if (qualifiedName.equals(fqn(packageName, declaration.getName()))) {
|
||||||
|
answer.add(new JetLightClass(psiManager, file, declaration.getName()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return answer.toArray(new PsiClass[answer.size()]);
|
return answer.toArray(new PsiClass[answer.size()]);
|
||||||
@@ -83,15 +75,13 @@ public class JavaElementFinder extends PsiElementFinder {
|
|||||||
Set<String> answer = new HashSet<String>();
|
Set<String> answer = new HashSet<String>();
|
||||||
|
|
||||||
String packageFQN = psiPackage.getQualifiedName();
|
String packageFQN = psiPackage.getQualifiedName();
|
||||||
for (PsiFile psiFile : collectProjectFiles(project, GlobalSearchScope.allScope(project))) {
|
for (JetFile psiFile : collectProjectFiles(project, GlobalSearchScope.allScope(project))) {
|
||||||
if (psiFile instanceof JetFile) {
|
final JetNamespace rootNamespace = psiFile.getRootNamespace();
|
||||||
final JetNamespace rootNamespace = ((JetFile) psiFile).getRootNamespace();
|
if (packageFQN.equals(CodegenUtil.getFQName(rootNamespace))) {
|
||||||
if (packageFQN.equals(CodegenUtil.getFQName(rootNamespace))) {
|
answer.add("namespace");
|
||||||
answer.add("namespace");
|
for (JetDeclaration declaration : rootNamespace.getDeclarations()) {
|
||||||
for (JetDeclaration declaration : rootNamespace.getDeclarations()) {
|
if (declaration instanceof JetClassOrObject) {
|
||||||
if (declaration instanceof JetClassOrObject) {
|
answer.add(declaration.getName());
|
||||||
answer.add(declaration.getName());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -100,15 +90,18 @@ public class JavaElementFinder extends PsiElementFinder {
|
|||||||
return answer;
|
return answer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String fqn(String packageName, String className) {
|
||||||
|
if (StringUtil.isEmpty(packageName)) return className;
|
||||||
|
return packageName + "." + className;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PsiPackage findPackage(@NotNull String qualifiedName) {
|
public PsiPackage findPackage(@NotNull String qualifiedName) {
|
||||||
final List<PsiFile> psiFiles = collectProjectFiles(project, GlobalSearchScope.allScope(project));
|
final List<JetFile> psiFiles = collectProjectFiles(project, GlobalSearchScope.allScope(project));
|
||||||
|
|
||||||
for (PsiFile psiFile : psiFiles) {
|
for (JetFile psiFile : psiFiles) {
|
||||||
if (psiFile instanceof JetFile) {
|
if (qualifiedName.equals(CodegenUtil.getFQName(psiFile.getRootNamespace()))) {
|
||||||
if (qualifiedName.equals(CodegenUtil.getFQName(((JetFile) psiFile).getRootNamespace()))) {
|
return new PsiPackageImpl(psiFile.getManager(), qualifiedName);
|
||||||
return new PsiPackageImpl(psiFile.getManager(), qualifiedName);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,107 +112,26 @@ public class JavaElementFinder extends PsiElementFinder {
|
|||||||
@Override
|
@Override
|
||||||
public PsiClass[] getClasses(@NotNull PsiPackage psiPackage, @NotNull GlobalSearchScope scope) {
|
public PsiClass[] getClasses(@NotNull PsiPackage psiPackage, @NotNull GlobalSearchScope scope) {
|
||||||
List<PsiClass> answer = new SmartList<PsiClass>();
|
List<PsiClass> answer = new SmartList<PsiClass>();
|
||||||
final List<PsiFile> filesInScope = ensureUpToDate(scope);
|
final List<JetFile> filesInScope = collectProjectFiles(project, scope);
|
||||||
final String qualifiedName = psiPackage.getQualifiedName();
|
String packageFQN = psiPackage.getQualifiedName();
|
||||||
for (PsiFile file : filesInScope) {
|
for (JetFile file : filesInScope) {
|
||||||
final PsiJavaFileStub stub = file.getUserData(JAVA_API_STUB);
|
final JetNamespace rootNamespace = file.getRootNamespace();
|
||||||
if (stub == null) continue;
|
if (packageFQN.equals(CodegenUtil.getFQName(rootNamespace))) {
|
||||||
|
answer.add(new JetLightClass(psiManager, file, "namespace"));
|
||||||
if (Comparing.equal(qualifiedName, stub.getPackageName())) {
|
for (JetDeclaration declaration : rootNamespace.getDeclarations()) {
|
||||||
for (StubElement child : stub.getChildrenStubs()) {
|
if (declaration instanceof JetClassOrObject) {
|
||||||
if (child instanceof PsiClassStub) {
|
answer.add(new JetLightClass(psiManager, file, declaration.getName()));
|
||||||
answer.add((PsiClass) child.getPsi());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return answer.toArray(new PsiClass[answer.size()]);
|
return answer.toArray(new PsiClass[answer.size()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void collectClasses(List<PsiClass> answer, String qualifiedName, StubElement<?> stub) {
|
|
||||||
if (stub instanceof PsiClassStub && qualifiedName.equals(((PsiClassStub) stub).getQualifiedName())) {
|
|
||||||
answer.add((PsiClass) stub.getPsi());
|
|
||||||
}
|
|
||||||
|
|
||||||
for (StubElement child : stub.getChildrenStubs()) {
|
private static List<JetFile> collectProjectFiles(final Project project, @NotNull final GlobalSearchScope scope) {
|
||||||
collectClasses(answer, qualifiedName, child);
|
final List<JetFile> answer = new ArrayList<JetFile>();
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<PsiFile> ensureUpToDate(GlobalSearchScope scope) {
|
|
||||||
final Stack<StubElement> stubStack = new Stack<StubElement>();
|
|
||||||
|
|
||||||
final ClassBuilderFactory builderFactory = new ClassBuilderFactory() {
|
|
||||||
@Override
|
|
||||||
public ClassBuilder newClassBuilder() {
|
|
||||||
return new StubClassBuilder(stubStack);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String asText(ClassBuilder builder) {
|
|
||||||
throw new UnsupportedOperationException("asText is not implemented"); // TODO
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public byte[] asBytes(ClassBuilder builder) {
|
|
||||||
throw new UnsupportedOperationException("asBytes is not implemented"); // TODO
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
final GenerationState state = new GenerationState(project, builderFactory) {
|
|
||||||
@Override
|
|
||||||
protected void generateNamespace(JetNamespace namespace) {
|
|
||||||
final PsiJavaFileStubImpl fileStub = new PsiJavaFileStubImpl(CodegenUtil.getFQName(namespace), true);
|
|
||||||
PsiManager manager = PsiManager.getInstance(project);
|
|
||||||
stubStack.push(fileStub);
|
|
||||||
|
|
||||||
final PsiFile file = namespace.getContainingFile();
|
|
||||||
file.putUserData(JAVA_API_STUB, fileStub);
|
|
||||||
fileStub.setPsiFactory(new ClsWrapperStubPsiFactory());
|
|
||||||
final ClsFileImpl fakeFile = new ClsFileImpl((PsiManagerImpl) manager, new ClassFileViewProvider(manager, file.getVirtualFile())) {
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public PsiClassHolderFileStub getStub() {
|
|
||||||
return fileStub;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
fakeFile.setPhysical(false);
|
|
||||||
fileStub.setPsi(fakeFile);
|
|
||||||
|
|
||||||
try {
|
|
||||||
super.generateNamespace(namespace);
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
final StubElement pop = stubStack.pop();
|
|
||||||
if (pop != fileStub) {
|
|
||||||
LOG.error("Unbalanced stack operations");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
final List<PsiFile> psiFiles = collectProjectFiles(project, scope);
|
|
||||||
|
|
||||||
Collection<PsiFile> dirty = Collections2.filter(psiFiles, new Predicate<PsiFile>() {
|
|
||||||
@Override
|
|
||||||
public boolean apply(PsiFile psiFile) {
|
|
||||||
return psiFile.getUserData(JAVA_API_STUB) == null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
if (dirty.size() > 0) {
|
|
||||||
final BindingContext context = AnalyzerFacade.shallowAnalyzeFiles(dirty);
|
|
||||||
state.compileCorrectNamespaces(context, AnalyzerFacade.collectRootNamespaces(dirty));
|
|
||||||
state.getFactory().files();
|
|
||||||
}
|
|
||||||
|
|
||||||
return psiFiles;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static List<PsiFile> collectProjectFiles(final Project project, @NotNull final GlobalSearchScope scope) {
|
|
||||||
final List<PsiFile> answer = new ArrayList<PsiFile>();
|
|
||||||
|
|
||||||
|
|
||||||
final FileTypeManager fileTypeManager = FileTypeManager.getInstance();
|
final FileTypeManager fileTypeManager = FileTypeManager.getInstance();
|
||||||
@@ -235,7 +147,7 @@ public class JavaElementFinder extends PsiElementFinder {
|
|||||||
if (scope.accept(file)) {
|
if (scope.accept(file)) {
|
||||||
final PsiFile psiFile = psiManager.findFile(file);
|
final PsiFile psiFile = psiManager.findFile(file);
|
||||||
if (psiFile instanceof JetFile) {
|
if (psiFile instanceof JetFile) {
|
||||||
answer.add(psiFile);
|
answer.add((JetFile) psiFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,152 @@
|
|||||||
|
/*
|
||||||
|
* @author max
|
||||||
|
*/
|
||||||
|
package org.jetbrains.jet.plugin.java;
|
||||||
|
|
||||||
|
import com.intellij.openapi.diagnostic.Logger;
|
||||||
|
import com.intellij.openapi.project.Project;
|
||||||
|
import com.intellij.openapi.util.Key;
|
||||||
|
import com.intellij.psi.*;
|
||||||
|
import com.intellij.psi.impl.PsiManagerImpl;
|
||||||
|
import com.intellij.psi.impl.compiled.ClsFileImpl;
|
||||||
|
import com.intellij.psi.impl.java.stubs.PsiClassStub;
|
||||||
|
import com.intellij.psi.impl.java.stubs.PsiJavaFileStub;
|
||||||
|
import com.intellij.psi.impl.java.stubs.impl.PsiJavaFileStubImpl;
|
||||||
|
import com.intellij.psi.impl.light.AbstractLightClass;
|
||||||
|
import com.intellij.psi.stubs.PsiClassHolderFileStub;
|
||||||
|
import com.intellij.psi.stubs.StubElement;
|
||||||
|
import com.intellij.util.containers.Stack;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.jet.codegen.ClassBuilder;
|
||||||
|
import org.jetbrains.jet.codegen.ClassBuilderFactory;
|
||||||
|
import org.jetbrains.jet.codegen.CodegenUtil;
|
||||||
|
import org.jetbrains.jet.codegen.GenerationState;
|
||||||
|
import org.jetbrains.jet.lang.psi.JetFile;
|
||||||
|
import org.jetbrains.jet.lang.psi.JetNamespace;
|
||||||
|
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||||
|
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacade;
|
||||||
|
import org.jetbrains.jet.lang.resolve.java.JetJavaMirrorMarker;
|
||||||
|
import org.jetbrains.jet.plugin.JetLanguage;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class JetLightClass extends AbstractLightClass implements JetJavaMirrorMarker {
|
||||||
|
private static final Logger LOG = Logger.getInstance("#org.jetbrains.jet.plugin.java.JetLightClass");
|
||||||
|
private final static Key<PsiJavaFileStub> JAVA_API_STUB = Key.create("JAVA_API_STUB");
|
||||||
|
|
||||||
|
private final JetFile file;
|
||||||
|
private final String className;
|
||||||
|
private PsiClass delegate;
|
||||||
|
|
||||||
|
public JetLightClass(PsiManager manager, JetFile file, String className) {
|
||||||
|
super(manager, JetLanguage.INSTANCE);
|
||||||
|
this.file = file;
|
||||||
|
this.className = className;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return className;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PsiElement copy() {
|
||||||
|
return new JetLightClass(getManager(), file, className);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PsiClass getDelegate() {
|
||||||
|
if (delegate == null) {
|
||||||
|
delegate = findClass(className, getStub());
|
||||||
|
}
|
||||||
|
return delegate;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static PsiClass findClass(String name, StubElement<?> stub) {
|
||||||
|
if (stub instanceof PsiClassStub && name.equals(((PsiClassStub) stub).getName())) {
|
||||||
|
return (PsiClass) stub.getPsi();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (StubElement child : stub.getChildrenStubs()) {
|
||||||
|
PsiClass answer = findClass(name, child);
|
||||||
|
if (answer != null) return answer;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private PsiJavaFileStub getStub() {
|
||||||
|
PsiJavaFileStub answer = file.getUserData(JAVA_API_STUB);
|
||||||
|
if (answer == null) {
|
||||||
|
answer = calcStub();
|
||||||
|
file.putUserData(JAVA_API_STUB, answer);
|
||||||
|
}
|
||||||
|
|
||||||
|
return answer;
|
||||||
|
}
|
||||||
|
|
||||||
|
private PsiJavaFileStub calcStub() {
|
||||||
|
final PsiJavaFileStubImpl answer = new PsiJavaFileStubImpl(CodegenUtil.getFQName(file.getRootNamespace()), true);
|
||||||
|
final Project project = getProject();
|
||||||
|
|
||||||
|
final Stack<StubElement> stubStack = new Stack<StubElement>();
|
||||||
|
|
||||||
|
final ClassBuilderFactory builderFactory = new ClassBuilderFactory() {
|
||||||
|
@Override
|
||||||
|
public ClassBuilder newClassBuilder() {
|
||||||
|
return new StubClassBuilder(stubStack);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String asText(ClassBuilder builder) {
|
||||||
|
throw new UnsupportedOperationException("asText is not implemented"); // TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] asBytes(ClassBuilder builder) {
|
||||||
|
throw new UnsupportedOperationException("asBytes is not implemented"); // TODO
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
final GenerationState state = new GenerationState(project, builderFactory) {
|
||||||
|
@Override
|
||||||
|
protected void generateNamespace(JetNamespace namespace) {
|
||||||
|
PsiManager manager = PsiManager.getInstance(project);
|
||||||
|
stubStack.push(answer);
|
||||||
|
|
||||||
|
answer.setPsiFactory(new ClsWrapperStubPsiFactory());
|
||||||
|
final ClsFileImpl fakeFile = new ClsFileImpl((PsiManagerImpl) manager, new ClassFileViewProvider(manager, file.getVirtualFile())) {
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public PsiClassHolderFileStub getStub() {
|
||||||
|
return answer;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fakeFile.setPhysical(false);
|
||||||
|
answer.setPsi(fakeFile);
|
||||||
|
|
||||||
|
try {
|
||||||
|
super.generateNamespace(namespace);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
final StubElement pop = stubStack.pop();
|
||||||
|
if (pop != answer) {
|
||||||
|
LOG.error("Unbalanced stack operations");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
List<PsiFile> files = Collections.<PsiFile>singletonList(file);
|
||||||
|
final BindingContext context = AnalyzerFacade.shallowAnalyzeFiles(files);
|
||||||
|
state.compileCorrectNamespaces(context, AnalyzerFacade.collectRootNamespaces(files));
|
||||||
|
state.getFactory().files();
|
||||||
|
|
||||||
|
return answer;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user