Function and Constructor converted; no longer emit 'open' modifier for private functions
This commit is contained in:
committed by
Pavel V. Talanov
parent
80e678e2ec
commit
4a07263908
@@ -250,7 +250,7 @@ public class Converter {
|
||||
// and modify secondaries
|
||||
if (m.getKind() == INode.Kind.CONSTRUCTOR) {
|
||||
Function f = (Function) m;
|
||||
if (!((Constructor) f).isPrimary()) {
|
||||
if (!((Constructor) f).getIsPrimary()) {
|
||||
for (Field fo : finalOrWithEmptyInitializer) {
|
||||
String init = getDefaultInitializer(fo);
|
||||
initializers.put(fo.getIdentifier().toKotlin(), init);
|
||||
|
||||
@@ -44,9 +44,9 @@ public class Class extends Member {
|
||||
|
||||
public Class(Converter converter, Identifier name, Set<String> modifiers, List<Element> typeParameters, List<Type> extendsTypes,
|
||||
List<Expression> baseClassParams, List<Type> implementsTypes, List<Member> members) {
|
||||
super(modifiers);
|
||||
myName = name;
|
||||
myBaseClassParams = baseClassParams;
|
||||
myModifiers = modifiers;
|
||||
myTypeParameters = typeParameters;
|
||||
myExtendsTypes = extendsTypes;
|
||||
myImplementsTypes = implementsTypes;
|
||||
@@ -73,7 +73,7 @@ public class Class extends Member {
|
||||
private Constructor getPrimaryConstructor() {
|
||||
for (Member m : myMembers)
|
||||
if (m.getKind() == Kind.CONSTRUCTOR) {
|
||||
if (((Constructor) m).isPrimary()) {
|
||||
if (((Constructor) m).getIsPrimary()) {
|
||||
return (Constructor) m;
|
||||
}
|
||||
}
|
||||
@@ -131,7 +131,7 @@ public class Class extends Member {
|
||||
List<Function> secondaryConstructorsAsStaticInitFunction() {
|
||||
final LinkedList<Function> result = new LinkedList<Function>();
|
||||
for (Member m : myMembers)
|
||||
if (m.getKind() == Kind.CONSTRUCTOR && !((Constructor) m).isPrimary()) {
|
||||
if (m.getKind() == Kind.CONSTRUCTOR && !((Constructor) m).getIsPrimary()) {
|
||||
Function f = (Function) m;
|
||||
Set<String> modifiers = new HashSet<String>(m.myModifiers);
|
||||
modifiers.add(Modifier.STATIC);
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.j2k.ast;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author ignatov
|
||||
*/
|
||||
public class Constructor extends Function {
|
||||
private final boolean myIsPrimary;
|
||||
|
||||
public Constructor(Identifier identifier, Set<String> modifiers, Type type, List<Element> typeParameters, Element params, Block block, boolean isPrimary) {
|
||||
super(identifier, modifiers, type, typeParameters, params, block);
|
||||
myIsPrimary = isPrimary;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String primarySignatureToKotlin() {
|
||||
return "(" + myParams.toKotlin() + ")";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String primaryBodyToKotlin() {
|
||||
return myBlock.toKotlin();
|
||||
}
|
||||
|
||||
public boolean isPrimary() {
|
||||
return myIsPrimary;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Kind getKind() {
|
||||
return Kind.CONSTRUCTOR;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package org.jetbrains.jet.j2k.ast
|
||||
|
||||
import java.util.List
|
||||
import java.util.Set
|
||||
import org.jetbrains.jet.j2k.ast.INode.Kind
|
||||
|
||||
public open class Constructor(identifier : Identifier,
|
||||
modifiers : Set<String?>,
|
||||
`type` : Type,
|
||||
typeParameters : List<Element>,
|
||||
params : Element,
|
||||
block : Block,
|
||||
val isPrimary : Boolean) : Function(identifier, modifiers, `type`, typeParameters, params, block) {
|
||||
|
||||
public open fun primarySignatureToKotlin() : String {
|
||||
return "(" + params.toKotlin() + ")"
|
||||
}
|
||||
|
||||
public open fun primaryBodyToKotlin() : String {
|
||||
return block!!.toKotlin()
|
||||
}
|
||||
|
||||
public override fun getKind() : INode.Kind {
|
||||
return INode.Kind.CONSTRUCTOR
|
||||
}
|
||||
}
|
||||
@@ -35,9 +35,9 @@ public class Field extends Member {
|
||||
final Element myInitializer;
|
||||
|
||||
public Field(Identifier identifier, Set<String> modifiers, Type type, Element initializer, int writingAccesses) {
|
||||
super(modifiers);
|
||||
myIdentifier = identifier;
|
||||
myWritingAccesses = writingAccesses;
|
||||
myModifiers = modifiers;
|
||||
myType = type;
|
||||
myInitializer = initializer;
|
||||
}
|
||||
|
||||
@@ -1,127 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.j2k.ast;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.j2k.util.AstUtil;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author ignatov
|
||||
*/
|
||||
public class Function extends Member {
|
||||
private final Identifier myName;
|
||||
private final Type myType;
|
||||
private final List<Element> myTypeParameters;
|
||||
final Element myParams;
|
||||
|
||||
// TODO: maybe remove it?
|
||||
public void setBlock(Block block) {
|
||||
myBlock = block;
|
||||
}
|
||||
|
||||
Block myBlock;
|
||||
|
||||
public Function(Identifier name, Set<String> modifiers, Type type, List<Element> typeParameters, Element params, Block block) {
|
||||
myName = name;
|
||||
myModifiers = modifiers;
|
||||
myType = type;
|
||||
myTypeParameters = typeParameters;
|
||||
myParams = params;
|
||||
myBlock = block;
|
||||
}
|
||||
|
||||
public List<Element> getTypeParameters() {
|
||||
return myTypeParameters;
|
||||
}
|
||||
|
||||
public Element getParams() {
|
||||
return myParams;
|
||||
}
|
||||
|
||||
public Block getBlock() {
|
||||
return myBlock;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String typeParametersToKotlin() {
|
||||
return myTypeParameters.size() > 0 ? "<" + AstUtil.joinNodes(myTypeParameters, COMMA_WITH_SPACE) + ">" : EMPTY;
|
||||
}
|
||||
|
||||
private boolean hasWhere() {
|
||||
for (Element t : myTypeParameters)
|
||||
if (t instanceof TypeParameter && ((TypeParameter) t).hasWhere()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private String typeParameterWhereToKotlin() {
|
||||
if (hasWhere()) {
|
||||
List<String> wheres = new LinkedList<String>();
|
||||
for (Element t : myTypeParameters)
|
||||
if (t instanceof TypeParameter) {
|
||||
wheres.add(((TypeParameter) t).getWhereToKotlin());
|
||||
}
|
||||
return SPACE + "where" + SPACE + AstUtil.join(wheres, COMMA_WITH_SPACE) + SPACE;
|
||||
}
|
||||
return EMPTY;
|
||||
}
|
||||
|
||||
String modifiersToKotlin() {
|
||||
List<String> modifierList = new LinkedList<String>();
|
||||
|
||||
String accessModifier = accessModifier();
|
||||
if (!accessModifier.isEmpty()) {
|
||||
modifierList.add(accessModifier);
|
||||
}
|
||||
|
||||
if (isAbstract()) {
|
||||
modifierList.add(Modifier.ABSTRACT);
|
||||
}
|
||||
|
||||
if (myModifiers.contains(Modifier.OVERRIDE)) {
|
||||
modifierList.add(Modifier.OVERRIDE);
|
||||
}
|
||||
|
||||
if (!myModifiers.contains(Modifier.ABSTRACT) && !myModifiers.contains(Modifier.OVERRIDE) && !myModifiers.contains(Modifier.FINAL)) {
|
||||
modifierList.add(Modifier.OPEN);
|
||||
}
|
||||
|
||||
if (myModifiers.contains(Modifier.NOT_OPEN)) {
|
||||
modifierList.remove(Modifier.OPEN);
|
||||
}
|
||||
|
||||
if (modifierList.size() > 0) {
|
||||
return AstUtil.join(modifierList, SPACE) + SPACE;
|
||||
}
|
||||
|
||||
return EMPTY;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String toKotlin() {
|
||||
return modifiersToKotlin() + "fun" + SPACE + myName.toKotlin() + typeParametersToKotlin() + "(" + myParams.toKotlin() + ")" + SPACE + COLON +
|
||||
SPACE + myType.toKotlin() + SPACE +
|
||||
typeParameterWhereToKotlin() +
|
||||
myBlock.toKotlin();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package org.jetbrains.jet.j2k.ast
|
||||
|
||||
import org.jetbrains.jet.j2k.util.AstUtil
|
||||
import java.util.LinkedList
|
||||
import java.util.List
|
||||
import java.util.Set
|
||||
|
||||
public open class Function(val name : Identifier,
|
||||
modifiers : Set<String?>,
|
||||
val `type` : Type,
|
||||
val typeParameters : List<Element>,
|
||||
val params : Element,
|
||||
var block : Block?) : Member(modifiers) {
|
||||
private fun typeParametersToKotlin() : String {
|
||||
return (if (typeParameters.size() > 0)
|
||||
"<" + typeParameters.map { it.toKotlin() }.makeString(", ") + ">"
|
||||
else
|
||||
"")
|
||||
}
|
||||
|
||||
private fun hasWhere() : Boolean = typeParameters.any { it is TypeParameter && it.hasWhere() }
|
||||
|
||||
private fun typeParameterWhereToKotlin() : String {
|
||||
if (hasWhere())
|
||||
{
|
||||
val wheres = typeParameters.filter { it is TypeParameter }.map { ((it as TypeParameter).getWhereToKotlin() )}
|
||||
return " where " + wheres.makeString(", ") + " "
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
open fun modifiersToKotlin() : String {
|
||||
val modifierList: List<String> = arrayList()
|
||||
val accessModifier : String = accessModifier()
|
||||
if (!accessModifier.isEmpty())
|
||||
{
|
||||
modifierList.add(accessModifier)
|
||||
}
|
||||
|
||||
if (isAbstract())
|
||||
{
|
||||
modifierList.add(Modifier.ABSTRACT)
|
||||
}
|
||||
|
||||
if (myModifiers.contains(Modifier.OVERRIDE))
|
||||
{
|
||||
modifierList.add(Modifier.OVERRIDE)
|
||||
}
|
||||
|
||||
if (!myModifiers.contains(Modifier.ABSTRACT) &&
|
||||
!myModifiers.contains(Modifier.OVERRIDE) &&
|
||||
!myModifiers.contains(Modifier.FINAL) &&
|
||||
!myModifiers.contains(Modifier.PRIVATE))
|
||||
{
|
||||
modifierList.add(Modifier.OPEN)
|
||||
}
|
||||
|
||||
if (myModifiers.contains(Modifier.NOT_OPEN))
|
||||
{
|
||||
modifierList.remove(Modifier.OPEN)
|
||||
}
|
||||
|
||||
if (modifierList.size() > 0)
|
||||
{
|
||||
return modifierList.makeString(" ") + " "
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
public override fun toKotlin() : String {
|
||||
return modifiersToKotlin() + "fun " + name.toKotlin() + typeParametersToKotlin() + "(" + params.toKotlin() + ") : " + `type`.toKotlin() + " "+ typeParameterWhereToKotlin() + block?.toKotlin()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -27,8 +27,8 @@ public class Initializer extends Member {
|
||||
private final Block myBlock;
|
||||
|
||||
public Initializer(Block block, Set<String> modifiers) {
|
||||
super(modifiers);
|
||||
myBlock = block;
|
||||
myModifiers = modifiers;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -24,7 +24,11 @@ import java.util.Set;
|
||||
* @author ignatov
|
||||
*/
|
||||
public abstract class Member extends Node implements IMember {
|
||||
Set<String> myModifiers;
|
||||
@NotNull protected Set<String> myModifiers;
|
||||
|
||||
protected Member(Set<String> modifiers) {
|
||||
myModifiers = modifiers;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
String accessModifier() {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
public open class MyClass() {
|
||||
private open fun init(arg1 : Int, arg2 : Int, arg3 : Int) : Unit {
|
||||
private fun init(arg1 : Int, arg2 : Int, arg3 : Int) : Unit {
|
||||
}
|
||||
}
|
||||
@@ -8,9 +8,9 @@ return _firstName
|
||||
public open fun getLastName() : String? {
|
||||
return _lastName
|
||||
}
|
||||
private open fun doSmthBefore() : Unit {
|
||||
private fun doSmthBefore() : Unit {
|
||||
}
|
||||
private open fun doSmthAfter() : Unit {
|
||||
private fun doSmthAfter() : Unit {
|
||||
}
|
||||
{
|
||||
doSmthBefore()
|
||||
|
||||
Reference in New Issue
Block a user