Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
硅谷海盗
kotlin
提交
6f21de8e
K
kotlin
项目概览
硅谷海盗
/
kotlin
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
K
kotlin
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
6f21de8e
编写于
12月 06, 2011
作者:
S
Stepan Koltsov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
working on reading class data from .class files
上级
d38f1cf8
变更
11
隐藏空白更改
内联
并排
Showing
11 changed file
with
312 addition
and
6 deletion
+312
-6
compiler/backend/src/org/jetbrains/jet/codegen/GenerationState.java
...ackend/src/org/jetbrains/jet/codegen/GenerationState.java
+2
-1
compiler/backend/src/org/jetbrains/jet/compiler/CompileEnvironment.java
...nd/src/org/jetbrains/jet/compiler/CompileEnvironment.java
+1
-1
compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaDescriptorResolver.java
...tbrains/jet/lang/resolve/java/JavaDescriptorResolver.java
+31
-3
compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaPackageScope.java
...org/jetbrains/jet/lang/resolve/java/JavaPackageScope.java
+12
-1
compiler/frontend/src/org/jetbrains/jet/JetCoreEnvironment.java
...er/frontend/src/org/jetbrains/jet/JetCoreEnvironment.java
+2
-0
compiler/testData/readClass/Class.kt
compiler/testData/readClass/Class.kt
+3
-0
compiler/testData/readClass/ClassFun.kt
compiler/testData/readClass/ClassFun.kt
+5
-0
compiler/testData/readClass/FunParamNotNull.kt
compiler/testData/readClass/FunParamNotNull.kt
+3
-0
compiler/testData/readClass/FunParamNullable.kt
compiler/testData/readClass/FunParamNullable.kt
+3
-0
compiler/testData/readClass/NsFun.kt
compiler/testData/readClass/NsFun.kt
+3
-0
compiler/tests/org/jetbrains/jet/ReadClassDataTest.java
compiler/tests/org/jetbrains/jet/ReadClassDataTest.java
+247
-0
未找到文件。
compiler/backend/src/org/jetbrains/jet/codegen/GenerationState.java
浏览文件 @
6f21de8e
...
...
@@ -82,11 +82,12 @@ public class GenerationState {
return
factory
.
forNamespace
(
namespace
);
}
public
void
compile
(
JetFile
psiFile
)
{
public
BindingContext
compile
(
JetFile
psiFile
)
{
final
JetNamespace
namespace
=
psiFile
.
getRootNamespace
();
final
BindingContext
bindingContext
=
AnalyzerFacade
.
analyzeOneNamespaceWithJavaIntegration
(
namespace
,
JetControlFlowDataTraceFactory
.
EMPTY
);
AnalyzingUtils
.
throwExceptionOnErrors
(
bindingContext
);
compileCorrectNamespaces
(
bindingContext
,
Collections
.
singletonList
(
namespace
));
return
bindingContext
;
// NamespaceCodegen codegen = forNamespace(namespace);
// bindingContexts.push(bindingContext);
// typeMapper = new JetTypeMapper(standardLibrary, bindingContext);
...
...
compiler/backend/src/org/jetbrains/jet/compiler/CompileEnvironment.java
浏览文件 @
6f21de8e
...
...
@@ -356,7 +356,7 @@ public class CompileEnvironment {
}
}
p
rivate
static
void
writeToOutputDirectory
(
ClassFileFactory
factory
,
final
String
outputDir
)
{
p
ublic
static
void
writeToOutputDirectory
(
ClassFileFactory
factory
,
final
String
outputDir
)
{
List
<
String
>
files
=
factory
.
files
();
for
(
String
file
:
files
)
{
if
(!
skipFile
(
file
))
{
...
...
compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaDescriptorResolver.java
浏览文件 @
6f21de8e
...
...
@@ -332,7 +332,6 @@ public class JavaDescriptorResolver {
}
private
ValueParameterDescriptor
resolveParameterDescriptor
(
DeclarationDescriptor
containingDeclaration
,
int
i
,
PsiParameter
parameter
)
{
String
name
=
parameter
.
getName
();
PsiType
psiType
=
parameter
.
getType
();
JetType
varargElementType
;
...
...
@@ -343,14 +342,43 @@ public class JavaDescriptorResolver {
else
{
varargElementType
=
null
;
}
boolean
changeNullable
=
false
;
boolean
nullable
=
true
;
// TODO: must be very slow, make it lazy?
String
name
=
parameter
.
getName
()
!=
null
?
parameter
.
getName
()
:
"p"
+
i
;
for
(
PsiAnnotation
annotation
:
parameter
.
getModifierList
().
getAnnotations
())
{
// TODO: softcode annotation name
PsiNameValuePair
[]
attributes
=
annotation
.
getParameterList
().
getAttributes
();
attributes
.
toString
();
if
(
annotation
.
getQualifiedName
().
equals
(
"jet.typeinfo.JetParameter"
))
{
PsiLiteralExpression
nameExpression
=
(
PsiLiteralExpression
)
annotation
.
findAttributeValue
(
"name"
);
if
(
nameExpression
!=
null
)
{
name
=
(
String
)
nameExpression
.
getValue
();
}
PsiLiteralExpression
nullableExpression
=
(
PsiLiteralExpression
)
annotation
.
findAttributeValue
(
"nullable"
);
if
(
nullableExpression
!=
null
)
{
nullable
=
(
Boolean
)
nullableExpression
.
getValue
();
}
else
{
// default value of parameter
nullable
=
false
;
changeNullable
=
true
;
}
}
}
JetType
outType
=
semanticServices
.
getTypeTransformer
().
transformToType
(
psiType
);
return
new
ValueParameterDescriptorImpl
(
containingDeclaration
,
i
,
Collections
.<
AnnotationDescriptor
>
emptyList
(),
// TODO
name
==
null
?
"p"
+
i
:
name
,
name
,
null
,
// TODO : review
outType
,
changeNullable
?
TypeUtils
.
makeNullableAsSpecified
(
outType
,
nullable
)
:
outType
,
false
,
varargElementType
);
...
...
compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaPackageScope.java
浏览文件 @
6f21de8e
package
org.jetbrains.jet.lang.resolve.java
;
import
com.intellij.psi.PsiClass
;
import
org.jetbrains.annotations.NotNull
;
import
org.jetbrains.jet.lang.descriptors.*
;
import
org.jetbrains.jet.lang.resolve.scopes.JetScopeImpl
;
...
...
@@ -34,7 +35,17 @@ public class JavaPackageScope extends JetScopeImpl {
@NotNull
@Override
public
Set
<
FunctionDescriptor
>
getFunctions
(
@NotNull
String
name
)
{
return
Collections
.
emptySet
();
// TODO: what is GlobalSearchScope
PsiClass
psiClass
=
semanticServices
.
getDescriptorResolver
().
javaFacade
.
findClass
(
packagePrefix
+
"namespace"
);
if
(
psiClass
==
null
)
{
return
Collections
.
emptySet
();
}
if
(
containingDescriptor
==
null
)
{
return
Collections
.
emptySet
();
}
return
semanticServices
.
getDescriptorResolver
().
resolveFunctionGroup
(
containingDescriptor
,
psiClass
,
null
,
name
,
true
);
}
@NotNull
...
...
compiler/frontend/src/org/jetbrains/jet/JetCoreEnvironment.java
浏览文件 @
6f21de8e
package
org.jetbrains.jet
;
import
com.intellij.core.JavaCoreEnvironment
;
import
com.intellij.lang.java.JavaParserDefinition
;
import
com.intellij.openapi.Disposable
;
import
org.jetbrains.jet.lang.parsing.JetParserDefinition
;
import
org.jetbrains.jet.plugin.JetFileType
;
...
...
@@ -15,6 +16,7 @@ public class JetCoreEnvironment extends JavaCoreEnvironment {
registerFileType
(
JetFileType
.
INSTANCE
,
"kts"
);
registerFileType
(
JetFileType
.
INSTANCE
,
"ktm"
);
registerFileType
(
JetFileType
.
INSTANCE
,
"jet"
);
registerParserDefinition
(
new
JavaParserDefinition
());
registerParserDefinition
(
new
JetParserDefinition
());
}
}
compiler/testData/readClass/Class.kt
0 → 100644
浏览文件 @
6f21de8e
namespace
test
class
Ramification
compiler/testData/readClass/ClassFun.kt
0 → 100644
浏览文件 @
6f21de8e
namespace
test
class
River
{
fun
song
()
=
1
}
compiler/testData/readClass/FunParamNotNull.kt
0 → 100644
浏览文件 @
6f21de8e
namespace
test
fun
fff
(
a
:
String
)
=
1
compiler/testData/readClass/FunParamNullable.kt
0 → 100644
浏览文件 @
6f21de8e
namespace
test
fun
fff
(
a
:
String
?)
=
1
compiler/testData/readClass/NsFun.kt
0 → 100644
浏览文件 @
6f21de8e
namespace
test
fun
f
()
=
1
compiler/tests/org/jetbrains/jet/ReadClassDataTest.java
0 → 100644
浏览文件 @
6f21de8e
package
org.jetbrains.jet
;
import
com.intellij.lang.ASTFactory
;
import
com.intellij.lang.LanguageASTFactory
;
import
com.intellij.lang.java.JavaLanguage
;
import
com.intellij.openapi.Disposable
;
import
com.intellij.openapi.util.Disposer
;
import
com.intellij.openapi.util.io.FileUtil
;
import
com.intellij.openapi.vfs.CharsetToolkit
;
import
com.intellij.psi.PsiFileFactory
;
import
com.intellij.psi.impl.PsiFileFactoryImpl
;
import
com.intellij.psi.impl.source.tree.JavaASTFactory
;
import
com.intellij.testFramework.LightVirtualFile
;
import
com.intellij.testFramework.UsefulTestCase
;
import
junit.framework.Test
;
import
org.jetbrains.annotations.NotNull
;
import
org.jetbrains.jet.codegen.ClassBuilderFactory
;
import
org.jetbrains.jet.codegen.ClassFileFactory
;
import
org.jetbrains.jet.codegen.GenerationState
;
import
org.jetbrains.jet.compiler.CompileEnvironment
;
import
org.jetbrains.jet.lang.JetSemanticServices
;
import
org.jetbrains.jet.lang.descriptors.ClassDescriptor
;
import
org.jetbrains.jet.lang.descriptors.ClassifierDescriptor
;
import
org.jetbrains.jet.lang.descriptors.DeclarationDescriptor
;
import
org.jetbrains.jet.lang.descriptors.FunctionDescriptor
;
import
org.jetbrains.jet.lang.descriptors.NamespaceDescriptor
;
import
org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor
;
import
org.jetbrains.jet.lang.psi.JetFile
;
import
org.jetbrains.jet.lang.resolve.AnalyzingUtils
;
import
org.jetbrains.jet.lang.resolve.BindingContext
;
import
org.jetbrains.jet.lang.resolve.BindingTraceContext
;
import
org.jetbrains.jet.lang.resolve.java.JavaDescriptorResolver
;
import
org.jetbrains.jet.lang.resolve.java.JavaSemanticServices
;
import
org.jetbrains.jet.plugin.JetLanguage
;
import
org.junit.Assert
;
import
java.io.File
;
import
java.io.IOException
;
import
java.lang.reflect.Method
;
import
java.util.Arrays
;
import
java.util.Set
;
/**
* @author Stepan Koltsov
*/
public
class
ReadClassDataTest
extends
UsefulTestCase
{
protected
final
Disposable
myTestRootDisposable
=
new
Disposable
()
{
@Override
public
void
dispose
()
{
}
};
private
JetCoreEnvironment
jetCoreEnvironment
;
private
File
tmpdir
;
private
final
File
testFile
;
public
ReadClassDataTest
(
@NotNull
File
testFile
)
{
this
.
testFile
=
testFile
;
setName
(
testFile
.
getName
());
}
@Override
protected
void
setUp
()
throws
Exception
{
super
.
setUp
();
tmpdir
=
new
File
(
"tmp/"
+
this
.
getClass
().
getSimpleName
()
+
"."
+
this
.
getName
());
rmrf
(
tmpdir
);
mkdirs
(
tmpdir
);
}
@Override
public
void
tearDown
()
throws
Exception
{
Disposer
.
dispose
(
myTestRootDisposable
);
}
private
void
mkdirs
(
File
file
)
throws
IOException
{
if
(
file
.
isDirectory
())
{
return
;
}
if
(!
file
.
mkdirs
())
{
throw
new
IOException
();
}
}
private
void
rmrf
(
File
file
)
{
if
(
file
!=
null
)
{
File
[]
children
=
file
.
listFiles
();
if
(
children
!=
null
)
{
for
(
File
child
:
children
)
{
rmrf
(
child
);
}
}
file
.
delete
();
}
}
private
void
createMockCoreEnvironment
()
{
jetCoreEnvironment
=
new
JetCoreEnvironment
(
myTestRootDisposable
);
final
File
rtJar
=
new
File
(
JetTestCaseBuilder
.
getHomeDirectory
(),
"compiler/testData/mockJDK-1.7/jre/lib/rt.jar"
);
jetCoreEnvironment
.
addToClasspath
(
rtJar
);
jetCoreEnvironment
.
addToClasspath
(
new
File
(
JetTestCaseBuilder
.
getHomeDirectory
(),
"compiler/testData/mockJDK-1.7/jre/lib/annotations.jar"
));
}
@Override
public
void
runTest
()
throws
Exception
{
if
(
true
)
return
;
createMockCoreEnvironment
();
LanguageASTFactory
.
INSTANCE
.
addExplicitExtension
(
JavaLanguage
.
INSTANCE
,
new
JavaASTFactory
());
String
text
=
FileUtil
.
loadFile
(
testFile
);
LightVirtualFile
virtualFile
=
new
LightVirtualFile
(
"Hello.kt"
,
JetLanguage
.
INSTANCE
,
text
);
virtualFile
.
setCharset
(
CharsetToolkit
.
UTF8_CHARSET
);
JetFile
psiFile
=
(
JetFile
)
((
PsiFileFactoryImpl
)
PsiFileFactory
.
getInstance
(
jetCoreEnvironment
.
getProject
())).
trySetupPsiForFile
(
virtualFile
,
JetLanguage
.
INSTANCE
,
true
,
false
);
GenerationState
state
=
new
GenerationState
(
jetCoreEnvironment
.
getProject
(),
ClassBuilderFactory
.
BINARIES
);
AnalyzingUtils
.
checkForSyntacticErrors
(
psiFile
);
BindingContext
bindingContext
=
state
.
compile
(
psiFile
);
ClassFileFactory
classFileFactory
=
state
.
getFactory
();
CompileEnvironment
.
writeToOutputDirectory
(
classFileFactory
,
tmpdir
.
getPath
());
NamespaceDescriptor
namespaceFromSource
=
(
NamespaceDescriptor
)
bindingContext
.
get
(
BindingContext
.
DECLARATION_TO_DESCRIPTOR
,
psiFile
.
getRootNamespace
());
Assert
.
assertEquals
(
"test"
,
namespaceFromSource
.
getName
());
Disposer
.
dispose
(
myTestRootDisposable
);
createMockCoreEnvironment
();
jetCoreEnvironment
.
addToClasspath
(
tmpdir
);
JetSemanticServices
jetSemanticServices
=
JetSemanticServices
.
createSemanticServices
(
jetCoreEnvironment
.
getProject
());
JavaSemanticServices
semanticServices
=
new
JavaSemanticServices
(
jetCoreEnvironment
.
getProject
(),
jetSemanticServices
,
new
BindingTraceContext
());
JavaDescriptorResolver
javaDescriptorResolver
=
semanticServices
.
getDescriptorResolver
();
NamespaceDescriptor
namespaceFromClass
=
javaDescriptorResolver
.
resolveNamespace
(
"test"
);
compareNamespaces
(
namespaceFromSource
,
namespaceFromClass
);
}
private
void
compareNamespaces
(
@NotNull
NamespaceDescriptor
nsa
,
@NotNull
NamespaceDescriptor
nsb
)
{
Assert
.
assertEquals
(
nsa
.
getName
(),
nsb
.
getName
());
System
.
out
.
println
(
"namespace "
+
nsa
.
getName
());
for
(
DeclarationDescriptor
ad
:
nsa
.
getMemberScope
().
getAllDescriptors
())
{
if
(
ad
instanceof
ClassifierDescriptor
)
{
ClassifierDescriptor
bd
=
nsb
.
getMemberScope
().
getClassifier
(
ad
.
getName
());
compareClassifiers
((
ClassifierDescriptor
)
ad
,
bd
);
}
else
if
(
ad
instanceof
FunctionDescriptor
)
{
Set
<
FunctionDescriptor
>
functions
=
nsb
.
getMemberScope
().
getFunctions
(
ad
.
getName
());
Assert
.
assertTrue
(
functions
.
size
()
>=
1
);
Assert
.
assertTrue
(
"not implemented"
,
functions
.
size
()
==
1
);
FunctionDescriptor
bd
=
functions
.
iterator
().
next
();
compareFunctions
((
FunctionDescriptor
)
ad
,
bd
);
}
else
{
throw
new
AssertionError
(
"Unknown member: "
+
ad
);
}
}
}
private
void
compareClassifiers
(
@NotNull
ClassifierDescriptor
a
,
@NotNull
ClassifierDescriptor
b
)
{
Assert
.
assertEquals
(
a
.
getName
(),
b
.
getName
());
System
.
out
.
println
(
"classifier "
+
a
.
getName
());
if
(
a
instanceof
ClassDescriptor
||
b
instanceof
ClassDescriptor
)
{
compareClasses
((
ClassDescriptor
)
a
,
(
ClassDescriptor
)
b
);
}
}
private
void
compareClasses
(
@NotNull
ClassDescriptor
a
,
@NotNull
ClassDescriptor
b
)
{
System
.
out
.
println
(
"... is class"
);
}
private
void
compareFunctions
(
@NotNull
FunctionDescriptor
a
,
@NotNull
FunctionDescriptor
b
)
{
Assert
.
assertEquals
(
a
.
getName
(),
b
.
getName
());
Assert
.
assertEquals
(
a
.
getValueParameters
().
size
(),
b
.
getValueParameters
().
size
());
for
(
int
i
=
0
;
i
<
a
.
getValueParameters
().
size
();
++
i
)
{
compareAnything
(
ValueParameterDescriptor
.
class
,
a
.
getValueParameters
().
get
(
i
),
b
.
getValueParameters
().
get
(
i
));
}
System
.
out
.
println
(
"function "
+
a
.
getName
());
}
private
<
T
>
void
compareAnything
(
Class
<
T
>
clazz
,
T
a
,
T
b
)
{
System
.
out
.
println
(
"Comparing "
+
clazz
);
Assert
.
assertNotNull
(
a
);
Assert
.
assertNotNull
(
b
);
for
(
Method
method
:
clazz
.
getMethods
())
{
if
(!
isGetter
(
method
))
{
continue
;
}
if
(
method
.
getName
().
equals
(
"getContainingDeclaration"
))
{
continue
;
}
if
(
clazz
.
equals
(
ValueParameterDescriptor
.
class
))
{
if
(
method
.
getName
().
equals
(
"isRef"
)
||
method
.
getName
().
equals
(
"getOriginal"
))
{
continue
;
}
}
System
.
out
.
println
(
method
.
getName
());
Object
ap
=
invoke
(
method
,
a
);
Object
bp
=
invoke
(
method
,
b
);
Assert
.
assertEquals
(
ap
,
bp
);
}
}
private
static
boolean
isGetter
(
Method
method
)
{
if
(
method
.
getParameterTypes
().
length
>
0
)
{
return
false
;
}
if
(
method
.
getName
().
matches
(
"is.+"
))
{
return
method
.
getReturnType
().
equals
(
boolean
.
class
);
}
else
if
(
method
.
getName
().
matches
(
"get.+"
))
{
return
true
;
}
else
{
return
false
;
}
}
private
static
Object
invoke
(
Method
method
,
Object
thiz
,
Object
...
args
)
{
try
{
return
method
.
invoke
(
thiz
,
args
);
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
"failed to invoke "
+
method
+
": "
+
e
);
}
}
public
static
Test
suite
()
{
return
JetTestCaseBuilder
.
suiteForDirectory
(
JetTestCaseBuilder
.
getTestDataPathBase
(),
"/readClass"
,
true
,
new
JetTestCaseBuilder
.
NamedTestFactory
()
{
@NotNull
@Override
public
Test
createTest
(
@NotNull
String
dataPath
,
@NotNull
String
name
,
@NotNull
File
file
)
{
return
new
ReadClassDataTest
(
file
);
}
});
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录