Kotlin筆記

官網:

http://kotlinlang.org/html

http://kotlinlang.org/docs/reference/java

中文教程: http://kotlindoc.com/git

Gradle: http://gradle.org/github

 

KotlinMvc:  http://code.taobao.org/svn/MyKotlinMvc/spring

安裝:

1. Intellij Idea :  apache

2. Kotlin 編譯器。 https://github.com/JetBrains/kotlin

ubuntu

JVM最簡生存指南

http://www.importnew.com/10127.htmlapi

必讀, 是寫給 .Net 開發者 轉 Java 人員的。tomcat

 

Spring+Kotlin:

https://spring.io/blog/2016/02/15/developing-spring-boot-applications-with-kotlinapp

 

技巧:

1. 代碼混編。使用 Intellij Idea ,時,可使用 Java + Kotlin + Scala 混合編譯。 須要添加各自的包引用。

2. 把Java代碼 Copy 到 Kotlin 時,會有提示,讓系統自動進行轉換。

3. 函數的參數是不可修改的, 若是想修改, 能夠從新定義一個同名的參數,如: 

var startIndex = startIndex;

 

問題

1. 泛型

  Java聲明泛型時,在使用時,能夠不指定泛型, 如: List jm = new ArrayList<String>();   C#能夠這樣:

  public class JsonMsg<T> : JsonMsg{}

  但 Kotlin 不能使用同名泛型類。

2.  關於 Maven : 

  http://blog.csdn.net/ubuntu64fan/article/details/7462981 ,做者極力反對, 首推: Rake , 其次是 Ant

  http://westsky.blog.51cto.com/358372/1590573 ,推薦順序是: Gradle > Ant > Maven.

3.  Cloneable  

  Cloneable 接口的 clone 方法竟然是 protected,直接致使泛型參數是Cloneable對象沒法直接調用 clone 方法。

4 . 死循環調用:

open class MapModel2   : HashMap<String,String> ()  {
    companion object{}

    override   fun get(key: String): String {
        return super.get(key).toString()
    }
}
class abc : MapModel2() {
    companion object{
        @JvmStatic
        var instance:MapModel2? = null;
    }
    var Name= "";

    override   fun get(key: String): String {
        return super.get(key).toString()
    }
}
fun main(arg: Array<String>) {
    abc.instance = abc();
    abc.instance!!["Name"] = "OK";
    println(abc.instance!!["Name"]); 
}

 

必須逐級重寫 get 方法,不然就會: 在 abc 裏執行get , 在 MapModel2 裏執行get ,因爲沒有重寫,轉而執行 abc 裏的 get 繼而形成死循環。

該問題已提交到jetbrains ,並已獲得解決: https://youtrack.jetbrains.com/issue/KT-13116?replyTo=27-1522462 。 

5. jvm-target 1.6的問題

    Bug1:  

    Kotlin Jar包項目,使用 Artifacts Build,是不對的,會使用 1.6

  查看 jar包裏的 /META-INF/MANIFEST.MF 內容以下:

  

Manifest-Version: 1.0
Implementation-Title: HttpComponents Apache HttpClient
Implementation-Version: 4.5.3
Archiver-Version: Plexus Archiver
Built-By: oleg
Specification-Vendor: The Apache Software Foundation
Implementation-Vendor-Id: org.apache
Specification-Title: HttpComponents Apache HttpClient
url: http://hc.apache.org/httpcomponents-client
Implementation-Vendor: The Apache Software Foundation
X-Compile-Target-JDK: 1.6
Implementation-Build: tags/4.5.3-RC1/httpclient@r1779741; 2017-01-21 1
 6:58:35+0100
X-Compile-Source-JDK: 1.6
Created-By: Apache Maven 3.0.5
Build-Jdk: 1.7.0_75
Specification-Version: 4.5.3

 

Bug2:

  在 idea 的 Project Structure 裏,把 target platform 改成 1.6 ,是不成功的。 除非選中 使用項目設置(use project settings )再選擇 1.8 

查看項目的 iml 文件。

 <facet type="kotlin-language" name="Kotlin">
      <configuration version="2" platform="JVM 1.6" useProjectSettings="false">
        <compilerSettings />
        <compilerArguments>
          <option name="jvmTarget" value="1.6" />
          <option name="languageVersion" value="1.1" />
          <option name="apiVersion" value="1.1" />
          <option name="pluginClasspaths">
            <array />
          </option>
          <option name="coroutinesWarn" value="true" />
          <option name="pluginOptions">
            <array />
          </option>
        </compilerArguments>
      </configuration>
    </facet>

 

應該使用 Maven 的 Package 打包,若是出現: 1.8 1.6 的錯誤,應該添加  jvmtarget 以下:

<plugin>
                <artifactId>kotlin-maven-plugin</artifactId>
                <groupId>org.jetbrains.kotlin</groupId>
                <version>${kotlin.version}</version>
                <configuration>
                    <jvmTarget>1.8</jvmTarget>
                    <compilerPlugins>
                        <plugin>spring</plugin>
                    </compilerPlugins>
                </configuration>

                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.jetbrains.kotlin</groupId>
                        <artifactId>kotlin-maven-allopen</artifactId>
                        <version>${kotlin.version}</version>
                    </dependency>
                </dependencies>
            </plugin>

 

jar包項目和引用jar包的項目都要添加。

 

 編譯運行的差別

編譯: mvn clean kotlin:compile package

運行  jar -jar  -noverify  xx.jar

字節碼校驗器 , 具體校驗:
變量要在使用以前進行初始化。
方法調用與對象引用類型之間要匹配。
訪問私有數據和方法的規則沒有被違反。
對本地變量的訪問都在運行時堆棧內。
運行時堆棧沒有溢出。

 

java 可使用  -noverify

tomcat Java_opts 在  setenv.sh 裏,分兩種狀況:

1. startup.sh , 能夠用 -noverify

2. daemon.sh 必須用 -Xverify:none

 

-Xverify:none 兼容性最好.

 

快捷鍵:

使用 VS 設置。

Ctrl + Shift + C 拷貝當前文件的全路徑。

相關文章
相關標籤/搜索