Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add the ability to translate annotation literals #132

Merged
merged 3 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
org.eclipse.*
jdt.*
.log

test-rule-output/
58 changes: 58 additions & 0 deletions data/test_rule.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0"?>
<ruleset xmlns="http://windup.jboss.org/schema/jboss-ruleset" id="hsearch" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://windup.jboss.org/schema/jboss-ruleset http://windup.jboss.org/schema/jboss-ruleset/windup-jboss-ruleset.xsd">
<metadata>
<description>
This ruleset provides analysis for migration from Hibernate Search 4.x to Hibernate Search 5.x.
</description>
<dependencies>
<addon id="org.jboss.windup.rules,windup-rules-javaee,2.4.0.Final" />
<addon id="org.jboss.windup.rules,windup-rules-java,2.4.0.Final" />
</dependencies>
<sourceTechnology id="hibernate-search" versionRange="[4,5)" />
<sourceTechnology id="eap" versionRange="[6,7)" />
<targetTechnology id="hibernate-search" versionRange="[5,)" />
<targetTechnology id="eap" versionRange="[7,8)" />
<tag>hibernate-search</tag>
<tag>hibernate</tag>
</metadata>
<rules>
<rule id="hsearch-00116">
<when>
<or>
<javaclass references="org.hibernate.search.annotations.Field">
<location>ANNOTATION</location>
<annotation-literal name="index" pattern="Index.YES"/>
<annotation-type pattern="org.hibernate.search.annotations.NumericFields"/>
</javaclass>
<javaclass references="org.hibernate.search.annotations.Field">
<location>ANNOTATION</location>
<annotation-literal name="index" pattern="Index.YES"/>
<annotation-type pattern="org.hibernate.search.annotations.NumericField"/>
</javaclass>
<javaclass references="java.util.{date}">
<annotation-type pattern="org.hibernate.search.annotations.Field" />
<annotation-type pattern="org.hibernate.search.annotations.Fields"/>
</javaclass>
<javaclass references="java.lang.{wrapper}">
<annotation-type pattern="org.hibernate.search.annotations.Field" />
<annotation-type pattern="org.hibernate.search.annotations.Fields"/>
</javaclass>
</or>
</when>
<perform>
<hint title="Hibernate Search 5 - Changes in indexing numeric and date values" effort="1" category-id="optional">
<message>Test</message>
<link href="http://docs.jboss.org/hibernate/search/5.5/api/org/hibernate/search/bridge/builtin/IntegerBridge.html" title="Javadoc API for IntegerBridge" />
<tag>hibernate-search</tag>
</hint>
</perform>
<where param="wrapper">
<matches pattern="(Integer|Long|Float|Double)" />
</where>
<where param="date">
<matches pattern="(Calendar|Date)" />
</where>
</rule>
</rules>
</ruleset>
42 changes: 42 additions & 0 deletions data/test_rule.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
- category: optional
customVariables: []
description: Hibernate Search 5 - Changes in indexing numeric and date values
effort: 1
labels:
- konveyor.io/source=hibernate-search4
- konveyor.io/source=hibernate-search
- konveyor.io/source=eap6
- konveyor.io/source=eap
- konveyor.io/target=hibernate-search5+
- konveyor.io/target=hibernate-search
- konveyor.io/target=eap7
- konveyor.io/target=eap
- hibernate-search
- hibernate
links:
- title: Javadoc API for IntegerBridge
url: http://docs.jboss.org/hibernate/search/5.5/api/org/hibernate/search/bridge/builtin/IntegerBridge.html
message: Test
ruleID: hsearch-00116
when:
or:
- java.referenced:
annotated:
elements:
- name: index
value: Index.YES
pattern: org.hibernate.search.annotations.NumericFields
location: ANNOTATION
pattern: org.hibernate.search.annotations.Field
- java.referenced:
annotated:
elements:
- name: index
value: Index.YES
pattern: org.hibernate.search.annotations.NumericField
location: ANNOTATION
pattern: org.hibernate.search.annotations.Field
- java.referenced:
pattern: java.util.(Calendar|Date)
- java.referenced:
pattern: java.lang.(Integer|Long|Float|Double)
47 changes: 47 additions & 0 deletions e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"fmt"
"github.com/konveyor/windup-shim/pkg/conversion"
"github.com/konveyor/windup-shim/pkg/windup"
"log"
"os"
"path/filepath"
"testing"
)

func Test_convertRule(t *testing.T) {

rulesets := []windup.Ruleset{}
ruleTests := []windup.Ruletest{}

err := filepath.WalkDir("./data", walkXML("./data/test_ruleset.xml", &rulesets, &ruleTests, false))
if err != nil {
fmt.Println(err)
}
_, err = conversion.ConvertWindupRulesetsToAnalyzer(rulesets, "./data/test_ruleset.xml", "./test-rule-output", true, false)
if err != nil {
log.Fatal(err)
}

defer func() {
err := os.RemoveAll("./test-rule-output")
jmle marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
t.Fatalf("Failed to remove output directory %v", err)
}
}()

expected, err := os.ReadFile("./data/test_rule.yaml")
if err != nil {
t.Fatalf("Failed to read the original yaml: %v", err)
}

output, err := os.ReadFile("./test-rule-output/data/01-test_rule.yaml")
if err != nil {
t.Fatalf("Failed to read the output yaml: %v", err)
}

if string(expected) != string(output) {
t.Fatalf("YAML files do not match\nExpected: %v\nGot: %v", string(expected), string(output))
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/fabianvf/windup-rulesets-yaml
module github.com/konveyor/windup-shim

go 1.21

Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"path/filepath"
"strings"

"github.com/fabianvf/windup-rulesets-yaml/pkg/conversion"
"github.com/fabianvf/windup-rulesets-yaml/pkg/execution"
"github.com/fabianvf/windup-rulesets-yaml/pkg/windup"
"github.com/konveyor/windup-shim/pkg/conversion"
"github.com/konveyor/windup-shim/pkg/execution"
"github.com/konveyor/windup-shim/pkg/windup"
abrugaro marked this conversation as resolved.
Show resolved Hide resolved
)

func main() {
Expand Down
94 changes: 55 additions & 39 deletions pkg/conversion/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
"strings"
"unicode"

"github.com/fabianvf/windup-rulesets-yaml/pkg/windup"
"github.com/konveyor-ecosystem/kantra/pkg/testing"
"github.com/konveyor/analyzer-lsp/engine"
engineLabels "github.com/konveyor/analyzer-lsp/engine/labels"
"github.com/konveyor/analyzer-lsp/output/v1/konveyor"
"github.com/konveyor/windup-shim/pkg/windup"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -842,47 +842,14 @@ func convertWindupWhenToAnalyzer(windupWhen windup.When, where map[string]string
}
if jc.Location != nil {
for _, location := range jc.Location {
condition := map[string]interface{}{
"java.referenced": map[string]interface{}{
"location": location,
"pattern": pattern,
// TODO handle jc.Annotationtype
// TODO handle jc.Annotationlist
// TODO handle jc.Annotationliteral
// TODO handle jc.MatchesSource
// TODO handle jc.In
},
}
if jc.As != "" {
condition["as"] = jc.As
// TODO (shurley): Only set when something is going to use this as block
// condition["ignore"] = true
}
if jc.From != "" {
condition["from"] = jc.From
}

condition := convertWindupJavaClassToCondition(jc)
condition["java.referenced"].(map[string]interface{})["location"] = location
condition["java.referenced"].(map[string]interface{})["pattern"] = pattern
conditions = append(conditions, condition)
}
} else {
condition := map[string]interface{}{
"java.referenced": map[string]interface{}{
"pattern": pattern,
// TODO handle jc.Annotationtype
// TODO handle jc.Annotationlist
// TODO handle jc.Annotationliteral
// TODO handle jc.MatchesSource
// TODO handle jc.In
},
}
if jc.As != "" {
condition["as"] = jc.As
// TODO (shurley): Only set when something is going to use this as block
// condition["ignore"] = true
}
if jc.From != "" {
condition["from"] = jc.From
}
condition := convertWindupJavaClassToCondition(jc)
condition["java.referenced"].(map[string]interface{})["pattern"] = pattern
conditions = append(conditions, condition)
}
}
Expand Down Expand Up @@ -1445,3 +1412,52 @@ func deduplicateFromAs(when []map[string]interface{}) []map[string]interface{} {
}
return deduped
}

func convertWindupJavaClassToCondition(jc windup.Javaclass) map[string]interface{} {
// TODO handle jc.Annotationlist
// TODO handle jc.MatchesSource
// TODO handle jc.In
condition := map[string]interface{}{
"java.referenced": map[string]interface{}{},
}

if jc.Annotationliteral != nil {
condition["java.referenced"].(map[string]interface{})["annotated"] = map[string]interface{}{
"elements": []map[string]interface{}{},
}
for _, annotationLiteral := range jc.Annotationliteral {
if annotationLiteral.Name != "" {
annotatedContent := condition["java.referenced"].(map[string]interface{})["annotated"].(map[string]interface{})

elements := append(
annotatedContent["elements"].([]map[string]interface{}),
map[string]interface{}{
"name": annotationLiteral.Name,
"value": convertWindupRegex(annotationLiteral.Pattern),
},
)
annotatedContent["elements"] = elements
}
}

}

if jc.Annotationtype.Pattern != "" {
annotated := condition["java.referenced"].(map[string]interface{})["annotated"]
if annotated == nil {
annotated = map[string]interface{}{}
}
annotated.(map[string]interface{})["pattern"] = jc.Annotationtype.Pattern
}

if jc.As != "" {
condition["as"] = jc.As
// TODO (shurley): Only set when something is going to use this as block
// condition["ignore"] = true
}
if jc.From != "" {
condition["from"] = jc.From
}

return condition
}
4 changes: 2 additions & 2 deletions pkg/execution/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"regexp"
"strings"

"github.com/fabianvf/windup-rulesets-yaml/pkg/conversion"
"github.com/fabianvf/windup-rulesets-yaml/pkg/windup"
"github.com/konveyor/analyzer-lsp/output/v1/konveyor"
"github.com/konveyor/analyzer-lsp/provider"
"github.com/konveyor/windup-shim/pkg/conversion"
"github.com/konveyor/windup-shim/pkg/windup"
"gopkg.in/yaml.v2"
)

Expand Down
18 changes: 9 additions & 9 deletions pkg/windup/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,15 @@ type Iterationwhen struct {
}

type Javaclass struct {
Location []string `xml:"location,omitempty" yaml:"location,omitempty"`
Annotationtype Annotationtype `xml:"annotation-type,omitempty" yaml:"annotation-type,omitempty"`
Annotationlist Annotationlist `xml:"annotation-list,omitempty" yaml:"annotation-list,omitempty"`
Annotationliteral Annotationliteral `xml:"annotation-literal,omitempty" yaml:"annotation-literal,omitempty"`
References string `xml:"references,attr,omitempty" yaml:"references,omitempty"`
MatchesSource string `xml:"matchesSource,attr,omitempty" yaml:"matchesSource,omitempty"`
As string `xml:"as,attr,omitempty" yaml:"as,omitempty"`
From string `xml:"from,attr,omitempty" yaml:"from,omitempty"`
In string `xml:"in,attr,omitempty" yaml:"in,omitempty"`
Location []string `xml:"location,omitempty" yaml:"location,omitempty"`
Annotationtype Annotationtype `xml:"annotation-type,omitempty" yaml:"annotation-type,omitempty"`
Annotationlist Annotationlist `xml:"annotation-list,omitempty" yaml:"annotation-list,omitempty"`
Annotationliteral []Annotationliteral `xml:"annotation-literal,omitempty" yaml:"annotation-literal,omitempty"`
References string `xml:"references,attr,omitempty" yaml:"references,omitempty"`
MatchesSource string `xml:"matchesSource,attr,omitempty" yaml:"matchesSource,omitempty"`
As string `xml:"as,attr,omitempty" yaml:"as,omitempty"`
From string `xml:"from,attr,omitempty" yaml:"from,omitempty"`
In string `xml:"in,attr,omitempty" yaml:"in,omitempty"`
}

type Javaclassignore struct {
Expand Down
Loading