Skip to content

Commit

Permalink
コンストラクタ対応
Browse files Browse the repository at this point in the history
refs #19
  • Loading branch information
love2hina-net committed Mar 8, 2022
1 parent dba4080 commit 2e7f043
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package net.love2hina.kotlin.sharon.data

import kotlin.collections.LinkedHashMap

internal data class ConstructorInfo(

/** 説明 */
override val description: StringBuilder = StringBuilder(),

/** パラメーター型 */
val typeParameters: HashMap<String, TypeParameterInfo> = LinkedHashMap(),

/** パラメーター */
val parameters: HashMap<String, ParameterInfo> = LinkedHashMap(),

/** 例外 */
val throws: HashMap<String, ThrowsInfo> = LinkedHashMap(),

/*
* see
* @since
* @deprecated
* @serialData
*/

): JavadocInfo {

override fun parseTag(name: String?, value: String): Boolean
= when (name) {
"param" -> {
val r = Regex("^(?:(?<name>\\w+)|<(?<type>\\w+)>)(?:\\s+(?<desc>.*))?$")
val m = r.find(value)

if (m != null) {
val groups = (m.groups as MatchNamedGroupCollection)

val paramName = groups["name"]?.value
val paramType = groups["type"]?.value
val paramDesc = groups["desc"]?.value ?: ""

if (paramName != null) {
val paramInfo = parameters.getOrPut(paramName) { ParameterInfo("", "", paramName) }

paramInfo.description = paramDesc
}
else if (paramType != null) {
val typeParameterInfo = typeParameters.getOrPut(paramType) { TypeParameterInfo(paramType) }

typeParameterInfo.description = paramDesc
}
}

true
}
"throws", "exception" -> {
val r = Regex("^(?<name>\\w+)(?:\\s+(?<desc>.*))?$")
val m = r.find(value)

if (m != null) {
val groups = (m.groups as MatchNamedGroupCollection)

val expType = groups["name"]!!.value
val expDesc = groups["desc"]?.value ?: ""

val throwsInfo = throws.getOrPut(expType) { ThrowsInfo(expType) }

throwsInfo.description = expDesc
}

true
}
else -> false
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,96 @@
package net.love2hina.kotlin.sharon.parser

import com.github.javaparser.ast.body.ConstructorDeclaration
import com.github.javaparser.ast.body.MethodDeclaration
import net.love2hina.kotlin.sharon.data.*
import net.love2hina.kotlin.sharon.*

/**
* コンストラクタ定義.
*/
internal fun Parser.Visitor.visitInConstructor(n: ConstructorDeclaration, arg: Void?) {
// コンストラクタ情報
val ctorInfo = ConstructorInfo()

// 実体解析
// タイプパラメーター
n.typeParameters.forEach {
val name = it.name.asString()
ctorInfo.typeParameters[name] = TypeParameterInfo(name)
}
// 明示的なthisパラメーター
n.receiverParameter.ifPresent {
val name = it.name.asString()
ctorInfo.parameters[name] = ParameterInfo("", it.type.asString(), name)
}
// パラメーター
n.parameters.forEach {
val name = it.name.asString()
ctorInfo.parameters[name] = ParameterInfo(
getModifier(it.modifiers),
it.type.asString(),
name
)
}
// 例外
n.thrownExceptions.forEach {
val type = it.asString()
ctorInfo.throws[type] = ThrowsInfo(type)
}

// コメントの解析
n.comment.parseJavadoc(ctorInfo)

// 出力開始
writer.writeStartElement("method")
writer.writeAttribute("type", "constructor")
writer.writeAttribute("modifier", getModifier(n.modifiers))
writer.writeAttribute("name", n.name.asString())

// 定義全体
writer.writeStartElement("definitions")
writer.writeStrings(n.getDeclarationAsString(true, true, true))
writer.writeEndElement()

// 説明の出力
writer.writeStartElement("description")
writer.writeStrings(ctorInfo.description.toString())
writer.writeEndElement()
// タイプパラメーターの出力
ctorInfo.typeParameters.forEach {
writer.writeStartElement("typeParameter")
writer.writeAttribute("type", it.value.type)
writer.writeStrings(it.value.description)
writer.writeEndElement()
}
// パラメーターの出力
ctorInfo.parameters.forEach {
writer.writeStartElement("parameter")
writer.writeAttribute("modifier", it.value.modifier)
writer.writeAttribute("type", it.value.type)
writer.writeAttribute("name", it.value.name)
writer.writeStrings(it.value.description)
writer.writeEndElement()
}
// 例外の出力
ctorInfo.throws.forEach {
writer.writeStartElement("throws")
writer.writeAttribute("type", it.value.type)
writer.writeStrings(it.value.description)
writer.writeEndElement()
}

// アノテーション
n.annotations.forEach { it.accept(this, arg) }

// ステートメント
writer.writeStartElement("code")
n.body.accept(this, arg)
writer.writeEndElement()

writer.writeEndElement()
}

/**
* 関数定義.
*/
Expand Down Expand Up @@ -46,6 +133,7 @@ internal fun Parser.Visitor.visitInFunction(n: MethodDeclaration, arg: Void?) {

// 出力開始
writer.writeStartElement("method")
writer.writeAttribute("type", "normal")
writer.writeAttribute("modifier", getModifier(n.modifiers))
writer.writeAttribute("name", n.name.asString())

Expand Down
35 changes: 3 additions & 32 deletions src/main/kotlin/net/love2hina/kotlin/sharon/parser/Parser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -333,39 +333,10 @@ internal class Parser(
}
}

/**
* コンストラクタ定義.
*/
override fun visit(n: ConstructorDeclaration?, arg: Void?) {
// TODO
// n.getBody().accept(this, arg);
// n.getModifiers().forEach(p -> p.accept(this, arg));
// n.getName().accept(this, arg);
// n.getParameters().forEach(p -> p.accept(this, arg));
// n.getReceiverParameter().ifPresent(l -> l.accept(this, arg));
// n.getThrownExceptions().forEach(p -> p.accept(this, arg));
// n.getTypeParameters().forEach(p -> p.accept(this, arg));
// n.getAnnotations().forEach(p -> p.accept(this, arg));
// n.getComment().ifPresent(l -> l.accept(this, arg));
}

override fun visit(n: ConstructorDeclaration?, arg: Void?) = visitInConstructor(n!!, arg)
override fun visit(n: MethodDeclaration?, arg: Void?) = visitInFunction(n!!, arg)

/**
* 明示的なthisパラメータ.
*/
override fun visit(n: ReceiverParameter?, arg: Void?) {
// 使われない
throw IllegalAccessException()
}

/**
* パラメータ.
*/
override fun visit(n: Parameter?, arg: Void?) {
// 使われない
throw IllegalAccessException()
}
override fun visit(n: ReceiverParameter?, arg: Void?) = error("使われない")
override fun visit(n: Parameter?, arg: Void?) = error("使われない")

/**
* ブロックステートメント.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.love2hina.kotlin.sharon;

import java.io.IOException;
import java.nio.file.Path;

/**
Expand Down Expand Up @@ -28,6 +29,18 @@ public enum MyEnum {

}

/**
* コンストラクタ.
*
* @param parent 親
* @exception IOException IO例外
*/
public ParseTestClassTarget(final String parent) throws IOException
{
/// # 初期化
/// 初期化処理を行う
}

/**
* メソッド.
*
Expand Down

0 comments on commit 2e7f043

Please sign in to comment.