converting to kotlin

This commit is contained in:
Dmitry Jemerov
2012-05-22 20:45:00 +02:00
committed by Pavel V. Talanov
parent 0375f3dc0f
commit c7e448ed0f
10 changed files with 112 additions and 271 deletions
@@ -1,40 +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.ast.types.Type;
import java.util.Set;
/**
* @author ignatov
*/
public class EnumConstant extends Field {
public EnumConstant(Identifier identifier, Set<String> modifiers, @NotNull Type type, Element params) {
super(identifier, modifiers, type.convertedToNotNull(), params, 0);
}
@NotNull
@Override
public String toKotlin() {
if (myInitializer.toKotlin().isEmpty()) {
return myIdentifier.toKotlin();
}
return myIdentifier.toKotlin() + SPACE + COLON + SPACE + myType.toKotlin() + "(" + myInitializer.toKotlin() + ")";
}
}
@@ -0,0 +1,20 @@
package org.jetbrains.jet.j2k.ast
import org.jetbrains.jet.j2k.ast.types.Type
import java.util.Set
public open class EnumConstant(identifier : Identifier,
modifiers : Set<String?>,
`type` : Type,
params : Element) : Field(identifier, modifiers, `type`.convertedToNotNull(), params, 0) {
public override fun toKotlin() : String {
if (initializer.toKotlin().isEmpty()) {
return identifier.toKotlin()
}
return identifier.toKotlin() + " : " + `type`.toKotlin() + "(" + initializer.toKotlin() + ")"
}
}
-99
View File
@@ -1,99 +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.ast.types.Type;
import org.jetbrains.jet.j2k.util.AstUtil;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import static org.jetbrains.jet.j2k.Converter.getDefaultInitializer;
/**
* @author ignatov
*/
public class Field extends Member {
final Identifier myIdentifier;
private final int myWritingAccesses;
final Type myType;
final Element myInitializer;
public Field(Identifier identifier, Set<String> modifiers, Type type, Element initializer, int writingAccesses) {
super(modifiers);
myIdentifier = identifier;
myWritingAccesses = writingAccesses;
myType = type;
myInitializer = initializer;
}
public Element getInitializer() {
return myInitializer;
}
public Identifier getIdentifier() {
return myIdentifier;
}
public Type getType() {
return myType;
}
@NotNull
String modifiersToKotlin() {
List<String> modifierList = new LinkedList<String>();
if (isAbstract()) {
modifierList.add(Modifier.ABSTRACT);
}
modifierList.add(accessModifier());
modifierList.add(isVal() ? "val" : "var");
if (modifierList.size() > 0) {
return AstUtil.join(modifierList, SPACE) + SPACE;
}
return EMPTY;
}
public boolean isVal() {
return myModifiers.contains(Modifier.FINAL);
}
@Override
public boolean isStatic() {
return myModifiers.contains(Modifier.STATIC);
}
@NotNull
@Override
public String toKotlin() {
final String declaration = modifiersToKotlin() + myIdentifier.toKotlin() + SPACE + COLON + SPACE + myType.toKotlin();
if (myInitializer.isEmpty()) {
return declaration + (isVal() && !isStatic() && myWritingAccesses == 1
? EMPTY
: SPACE + EQUAL + SPACE + getDefaultInitializer(this));
}
return declaration + SPACE + EQUAL + SPACE + myInitializer.toKotlin();
}
}
+51
View File
@@ -0,0 +1,51 @@
package org.jetbrains.jet.j2k.ast
import org.jetbrains.jet.j2k.ast.types.Type
import org.jetbrains.jet.j2k.util.AstUtil
import java.util.LinkedList
import java.util.List
import java.util.Set
import org.jetbrains.jet.j2k.Converter.getDefaultInitializer
public open class Field(val identifier : Identifier,
modifiers : Set<String?>,
val `type` : Type,
val initializer : Element,
val writingAccesses : Int) : Member(modifiers) {
open fun modifiersToKotlin() : String {
val modifierList : List<String> = arrayList()
if (isAbstract()) {
modifierList.add(Modifier.ABSTRACT)
}
modifierList.add(accessModifier())
modifierList.add(if (isVal()) "val" else "var")
if (modifierList.size() > 0)
{
return modifierList.makeString(" ") + " "
}
return ""
}
public open fun isVal() : Boolean {
return myModifiers.contains(Modifier.FINAL)
}
public override fun isStatic() : Boolean {
return myModifiers.contains(Modifier.STATIC)
}
public override fun toKotlin() : String {
val declaration : String? = modifiersToKotlin() + identifier.toKotlin() + " : " + `type`.toKotlin()
if (initializer.isEmpty()) {
return declaration + ((if (isVal() && !isStatic() && writingAccesses == 1)
""
else
" = " + getDefaultInitializer(this)))
}
return declaration + " = " + initializer.toKotlin()
}
}
@@ -1,39 +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.Set;
/**
* @author ignatov
*/
public class Initializer extends Member {
private final Block myBlock;
public Initializer(Block block, Set<String> modifiers) {
super(modifiers);
myBlock = block;
}
@NotNull
@Override
public String toKotlin() {
return myBlock.toKotlin();
}
}
@@ -0,0 +1,9 @@
package org.jetbrains.jet.j2k.ast
import java.util.Set
public open class Initializer(val block : Block, modifiers : Set<String?>) : Member(modifiers) {
public override fun toKotlin() : String {
return block.toKotlin()
}
}
@@ -1,37 +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.ast.types.Type;
/**
* @author ignatov
*/
public class TypeElement extends Element {
private final Type myType;
public TypeElement(Type type) {
myType = type;
}
@NotNull
@Override
public String toKotlin() {
return myType.toKotlin();
}
}
@@ -0,0 +1,9 @@
package org.jetbrains.jet.j2k.ast
import org.jetbrains.jet.j2k.ast.types.Type
public open class TypeElement(val `type` : Type) : Element() {
public override fun toKotlin() : String {
return `type`.toKotlin()
}
}
@@ -1,56 +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.ast.types.Type;
import java.util.List;
/**
* @author ignatov
*/
public class TypeParameter extends Element {
private final Identifier myName;
private final List<Type> myExtendsTypes;
public TypeParameter(Identifier name, List<Type> extendsTypes) {
myName = name;
myExtendsTypes = extendsTypes;
}
public boolean hasWhere() {
return myExtendsTypes.size() > 1;
}
@NotNull
public String getWhereToKotlin() {
if (hasWhere()) {
return myName.toKotlin() + SPACE + COLON + SPACE + myExtendsTypes.get(1).toKotlin();
}
return EMPTY;
}
@NotNull
@Override
public String toKotlin() {
if (myExtendsTypes.size() > 0) {
return myName.toKotlin() + SPACE + COLON + SPACE + myExtendsTypes.get(0).toKotlin();
}
return myName.toKotlin();
}
}
@@ -0,0 +1,23 @@
package org.jetbrains.jet.j2k.ast
import org.jetbrains.jet.j2k.ast.types.Type
import java.util.List
public open class TypeParameter(val name : Identifier, val extendsTypes : List<Type>) : Element() {
public open fun hasWhere() : Boolean = extendsTypes.size() > 1
public open fun getWhereToKotlin() : String {
if (hasWhere()) {
return name.toKotlin() + " : " + extendsTypes.get(1).toKotlin()
}
return ""
}
public override fun toKotlin() : String {
if (extendsTypes.size() > 0) {
return name.toKotlin() + " : " + extendsTypes [0].toKotlin()
}
return name.toKotlin()
}
}