Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
硅谷海盗
kotlin
提交
bbf4fbc3
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,发现更多精彩内容 >>
提交
bbf4fbc3
编写于
4月 21, 2011
作者:
D
Dmitry Jemerov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
generate property accessors in class interface
上级
e0eb3ebd
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
82 addition
and
41 deletion
+82
-41
idea/src/org/jetbrains/jet/codegen/PropertyCodegen.java
idea/src/org/jetbrains/jet/codegen/PropertyCodegen.java
+75
-41
idea/tests/org/jetbrains/jet/codegen/PropertyGenTest.java
idea/tests/org/jetbrains/jet/codegen/PropertyGenTest.java
+7
-0
未找到文件。
idea/src/org/jetbrains/jet/codegen/PropertyCodegen.java
浏览文件 @
bbf4fbc3
...
...
@@ -34,7 +34,7 @@ public class PropertyCodegen {
}
public
void
genInInterface
(
JetProperty
p
)
{
gen
(
p
,
OwnerKind
.
INTERFACE
);
}
public
void
genInImplementation
(
JetProperty
p
)
{
...
...
@@ -46,55 +46,89 @@ public class PropertyCodegen {
}
public
void
gen
(
JetProperty
p
,
OwnerKind
kind
)
{
final
VariableDescriptor
descriptor
=
context
.
getVariableDescriptor
(
p
);
if
(!(
descriptor
instanceof
PropertyDescriptor
))
{
throw
new
UnsupportedOperationException
(
"expect a property to have a property descriptor"
);
}
final
PropertyDescriptor
propertyDescriptor
=
(
PropertyDescriptor
)
descriptor
;
if
(
kind
==
OwnerKind
.
NAMESPACE
||
kind
==
OwnerKind
.
IMPLEMENTATION
)
{
final
VariableDescriptor
descriptor
=
context
.
getVariableDescriptor
(
p
);
if
(!(
descriptor
instanceof
PropertyDescriptor
))
{
throw
new
UnsupportedOperationException
(
"expect a property to have a property descriptor"
);
generateBackingField
(
p
,
kind
,
propertyDescriptor
);
generateGetter
(
p
,
kind
,
propertyDescriptor
);
generateSetter
(
p
,
kind
,
propertyDescriptor
);
}
else
if
(
kind
==
OwnerKind
.
INTERFACE
)
{
final
JetPropertyAccessor
getter
=
p
.
getGetter
();
if
((
getter
!=
null
&&
!
getter
.
hasModifier
(
JetTokens
.
PRIVATE_KEYWORD
)
||
(
getter
==
null
&&
isExternallyAccessible
(
p
))))
{
v
.
visitMethod
(
Opcodes
.
ACC_ABSTRACT
|
Opcodes
.
ACC_PUBLIC
,
getterName
(
p
.
getName
()),
mapper
.
mapGetterSignature
(
propertyDescriptor
).
getDescriptor
(),
null
,
null
);
}
final
PropertyDescriptor
propertyDescriptor
=
(
PropertyDescriptor
)
descriptor
;
if
(
context
.
hasBackingField
(
propertyDescriptor
))
{
Object
value
=
null
;
final
JetExpression
initializer
=
p
.
getInitializer
();
if
(
initializer
!=
null
)
{
if
(
initializer
instanceof
JetConstantExpression
)
{
value
=
((
JetConstantExpression
)
initializer
).
getValue
();
}
}
int
modifiers
=
Opcodes
.
ACC_PRIVATE
;
if
(
kind
==
OwnerKind
.
NAMESPACE
)
{
modifiers
|=
Opcodes
.
ACC_STATIC
;
}
v
.
visitField
(
modifiers
,
p
.
getName
(),
mapper
.
mapType
(
descriptor
.
getOutType
()).
getDescriptor
(),
null
,
value
);
final
JetPropertyAccessor
setter
=
p
.
getSetter
();
if
((
setter
!=
null
&&
!
setter
.
hasModifier
(
JetTokens
.
PRIVATE_KEYWORD
)
||
(
setter
==
null
&&
isExternallyAccessible
(
p
)
&&
p
.
isVar
())))
{
v
.
visitMethod
(
Opcodes
.
ACC_ABSTRACT
|
Opcodes
.
ACC_PUBLIC
,
setterName
(
p
.
getName
()),
mapper
.
mapSetterSignature
(
propertyDescriptor
).
getDescriptor
(),
null
,
null
);
}
final
JetPropertyAccessor
getter
=
p
.
getGetter
();
if
(
getter
!=
null
)
{
if
(
getter
.
getBodyExpression
()
!=
null
)
{
functionCodegen
.
generateMethod
(
getter
,
kind
,
mapper
.
mapGetterSignature
(
propertyDescriptor
),
Collections
.<
ValueParameterDescriptor
>
emptyList
());
}
else
if
(!
getter
.
hasModifier
(
JetTokens
.
PRIVATE_KEYWORD
))
{
generateDefaultGetter
(
p
,
getter
,
kind
);
}
}
private
void
generateBackingField
(
JetProperty
p
,
OwnerKind
kind
,
PropertyDescriptor
propertyDescriptor
)
{
if
(
context
.
hasBackingField
(
propertyDescriptor
))
{
Object
value
=
null
;
final
JetExpression
initializer
=
p
.
getInitializer
();
if
(
initializer
!=
null
)
{
if
(
initializer
instanceof
JetConstantExpression
)
{
value
=
((
JetConstantExpression
)
initializer
).
getValue
();
}
}
else
if
(
p
.
hasModifier
(
JetTokens
.
PUBLIC_KEYWORD
))
{
generateDefaultGetter
(
p
,
p
,
kind
);
int
modifiers
=
Opcodes
.
ACC_PRIVATE
;
if
(
kind
==
OwnerKind
.
NAMESPACE
)
{
modifiers
|=
Opcodes
.
ACC_STATIC
;
}
final
JetPropertyAccessor
setter
=
p
.
getSetter
();
if
(
setter
!=
null
)
{
if
(
setter
.
getBodyExpression
()
!=
null
)
{
final
PropertySetterDescriptor
setterDescriptor
=
propertyDescriptor
.
getSetter
();
assert
setterDescriptor
!=
null
;
functionCodegen
.
generateMethod
(
setter
,
kind
,
mapper
.
mapSetterSignature
(
propertyDescriptor
),
setterDescriptor
.
getUnsubstitutedValueParameters
());
}
else
if
(!
p
.
hasModifier
(
JetTokens
.
PRIVATE_KEYWORD
))
{
generateDefaultSetter
(
p
,
setter
,
kind
);
}
v
.
visitField
(
modifiers
,
p
.
getName
(),
mapper
.
mapType
(
propertyDescriptor
.
getOutType
()).
getDescriptor
(),
null
,
value
);
}
}
private
void
generateGetter
(
JetProperty
p
,
OwnerKind
kind
,
PropertyDescriptor
propertyDescriptor
)
{
final
JetPropertyAccessor
getter
=
p
.
getGetter
();
if
(
getter
!=
null
)
{
if
(
getter
.
getBodyExpression
()
!=
null
)
{
functionCodegen
.
generateMethod
(
getter
,
kind
,
mapper
.
mapGetterSignature
(
propertyDescriptor
),
Collections
.<
ValueParameterDescriptor
>
emptyList
());
}
else
if
(
p
.
hasModifier
(
JetTokens
.
PUBLIC_KEYWORD
)
&&
p
.
isVar
(
))
{
generateDefault
Setter
(
p
,
p
,
kind
);
else
if
(
!
getter
.
hasModifier
(
JetTokens
.
PRIVATE_KEYWORD
))
{
generateDefault
Getter
(
p
,
getter
,
kind
);
}
}
else
if
(
isExternallyAccessible
(
p
))
{
generateDefaultGetter
(
p
,
p
,
kind
);
}
}
private
static
boolean
isExternallyAccessible
(
JetProperty
p
)
{
return
p
.
hasModifier
(
JetTokens
.
PUBLIC_KEYWORD
);
}
private
void
generateSetter
(
JetProperty
p
,
OwnerKind
kind
,
PropertyDescriptor
propertyDescriptor
)
{
final
JetPropertyAccessor
setter
=
p
.
getSetter
();
if
(
setter
!=
null
)
{
if
(
setter
.
getBodyExpression
()
!=
null
)
{
final
PropertySetterDescriptor
setterDescriptor
=
propertyDescriptor
.
getSetter
();
assert
setterDescriptor
!=
null
;
functionCodegen
.
generateMethod
(
setter
,
kind
,
mapper
.
mapSetterSignature
(
propertyDescriptor
),
setterDescriptor
.
getUnsubstitutedValueParameters
());
}
else
if
(!
p
.
hasModifier
(
JetTokens
.
PRIVATE_KEYWORD
))
{
generateDefaultSetter
(
p
,
setter
,
kind
);
}
}
else
if
(
isExternallyAccessible
(
p
)
&&
p
.
isVar
())
{
generateDefaultSetter
(
p
,
p
,
kind
);
}
}
private
void
generateDefaultGetter
(
JetProperty
p
,
JetDeclaration
declaration
,
OwnerKind
kind
)
{
...
...
idea/tests/org/jetbrains/jet/codegen/PropertyGenTest.java
浏览文件 @
bbf4fbc3
...
...
@@ -40,6 +40,13 @@ public class PropertyGenTest extends CodegenTestCase {
assertEquals
(
239
,
((
Integer
)
getter
.
invoke
(
instance
)).
intValue
());
}
public
void
testAccessorsInInterface
()
{
loadText
(
"class AccessorsInInterface { public var foo = 0; }"
);
final
Class
aClass
=
loadClass
(
"AccessorsInInterface"
,
generateClassesInFile
());
assertNotNull
(
findMethodByName
(
aClass
,
"getFoo"
));
assertNotNull
(
findMethodByName
(
aClass
,
"setFoo"
));
}
public
void
testPropertyInNamespace
()
throws
Exception
{
loadText
(
"private val x = 239"
);
final
Class
nsClass
=
generateNamespaceClass
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录