-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
21 changed files
with
773 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/feign/FeignTemplate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.itangcent.idea.plugin.api.export.feign | ||
|
||
object FeignTemplate { | ||
|
||
/** | ||
* Parse variables from a template fragment. | ||
* | ||
* @param fragment to parse | ||
* @return variables | ||
*/ | ||
fun parseVariables(fragment: String): List<String> { | ||
val variables = ArrayList<String>() | ||
val tokenizer = ChunkTokenizer(fragment) | ||
while (tokenizer.hasNext()) { | ||
val chunk = tokenizer.next() | ||
if (chunk.startsWith("{")) { | ||
variables.add(chunk.removePrefix("{").removeSuffix("}")) | ||
} | ||
} | ||
return variables | ||
} | ||
} | ||
|
||
/** | ||
* Splits a Uri into Chunks that exists inside and outside of an expression, delimited by curly | ||
* braces "{}". Nested expressions are treated as literals, for example "foo{bar{baz}}" will be | ||
* treated as "foo, {bar{baz}}". Inspired by Apache CXF Jax-RS. | ||
*/ | ||
private class ChunkTokenizer(template: String) { | ||
private val tokens: MutableList<String> = ArrayList() | ||
private var index = 0 | ||
operator fun hasNext(): Boolean { | ||
return tokens.size > index | ||
} | ||
|
||
operator fun next(): String { | ||
if (hasNext()) { | ||
return tokens[index++] | ||
} | ||
throw IllegalStateException("No More Elements") | ||
} | ||
|
||
init { | ||
var outside = true | ||
var level = 0 | ||
var lastIndex = 0 | ||
|
||
/* loop through the template, character by character */ | ||
var idx = 0 | ||
while (idx < template.length) { | ||
if (template[idx] == '{') { | ||
/* start of an expression */ | ||
if (outside) { | ||
/* outside of an expression */ | ||
if (lastIndex < idx) { | ||
/* this is the start of a new token */ | ||
tokens.add(template.substring(lastIndex, idx)) | ||
} | ||
lastIndex = idx | ||
|
||
/* | ||
* no longer outside of an expression, additional characters will be treated as in an | ||
* expression | ||
*/ | ||
outside = false | ||
} else { | ||
/* nested braces, increase our nesting level */ | ||
level++ | ||
} | ||
} else if (template[idx] == '}' && !outside) { | ||
/* the end of an expression */ | ||
if (level > 0) { | ||
/* | ||
* sometimes we see nested expressions, we only want the outer most expression | ||
* boundaries. | ||
*/ | ||
level-- | ||
} else { | ||
/* outermost boundary */ | ||
if (lastIndex < idx) { | ||
/* this is the end of an expression token */ | ||
tokens.add(template.substring(lastIndex, idx + 1)) | ||
} | ||
lastIndex = idx + 1 | ||
|
||
/* outside an expression */outside = true | ||
} | ||
} | ||
idx++ | ||
} | ||
if (lastIndex < idx) { | ||
/* grab the remaining chunk */ | ||
tokens.add(template.substring(lastIndex, idx)) | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...in/kotlin/com/itangcent/idea/plugin/api/export/feign/RequestLineRequestMappingResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.itangcent.idea.plugin.api.export.feign | ||
|
||
import com.google.inject.Inject | ||
import com.google.inject.Singleton | ||
import com.intellij.psi.PsiElement | ||
import com.itangcent.idea.condition.annotation.ConditionOnClass | ||
import com.itangcent.idea.plugin.api.export.spring.SpringRequestMappingResolver | ||
import com.itangcent.idea.plugin.condition.ConditionOnSetting | ||
import com.itangcent.intellij.jvm.AnnotationHelper | ||
import com.itangcent.intellij.logger.Logger | ||
import com.itangcent.intellij.psi.PsiClassUtils | ||
import com.itangcent.order.Order | ||
import com.itangcent.order.Ordered | ||
import java.util.regex.Matcher | ||
import java.util.regex.Pattern | ||
|
||
/** | ||
* Support @RequestLine | ||
*/ | ||
@Singleton | ||
@Order(Ordered.HIGHEST_PRECEDENCE) | ||
@ConditionOnClass(SpringFeignClassName.REQUEST_LINE_ANNOTATION) | ||
@ConditionOnSetting("feignEnable") | ||
class RequestLineRequestMappingResolver : SpringRequestMappingResolver { | ||
|
||
@Inject | ||
private lateinit var logger: Logger | ||
|
||
@Inject | ||
private lateinit var annotationHelper: AnnotationHelper | ||
|
||
/** | ||
* see [https://github.com/OpenFeign/feign/blob/f685f76002370413965f8aafea0ea96843b3c806/core/src/main/java/feign/Contract.java#L253-L270] | ||
* | ||
* @param psiElement annotated element(PsiMethod/PsiClass) | ||
* @return annotation attributes | ||
*/ | ||
override fun resolveRequestMapping(psiElement: PsiElement): Map<String, Any?>? { | ||
val requestLineValue = | ||
annotationHelper.findAttrAsString(psiElement, SpringFeignClassName.REQUEST_LINE_ANNOTATION) ?: return null | ||
|
||
if (requestLineValue.isEmpty()) { | ||
logger.error("RequestLine annotation was empty on method ${PsiClassUtils.fullNameOfMember(psiElement)}.") | ||
return null | ||
} | ||
val requestLineMatcher: Matcher = REQUEST_LINE_PATTERN.matcher(requestLineValue) | ||
if (!requestLineMatcher.find()) { | ||
logger.error( | ||
"RequestLine annotation didn't start with an HTTP verb on method ${ | ||
PsiClassUtils.fullNameOfMember( | ||
psiElement | ||
) | ||
}" | ||
) | ||
return null | ||
} | ||
return mapOf("method" to requestLineMatcher.group(1), | ||
"value" to requestLineMatcher.group(2)) | ||
} | ||
|
||
companion object { | ||
val REQUEST_LINE_PATTERN = Pattern.compile("^([A-Z]+)[ ]*(.*)$")!! | ||
} | ||
} |
Oops, something went wrong.