Skip to content

Ruleset Java Code Matching Examples

sgilda edited this page Sep 3, 2015 · 14 revisions

Ruleset: Java Basic: Code Matching Examples

Example XML snippets

Import

DRAFT This page will contain rule snippets for Java code matching, AND the java code they are supposed to match. Try git grep '<when>' if you want to add.
package com.mycompany.app;

import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.def.ProcessDefinition;
...
<rule id="UNIQUE_RULE_ID">
    <when>
        <javaclass references="org.jbpm.graph.def.ActionHandler" as="default">
            <location>INHERITANCE</location>
        </javaclass>
    </when>
    <perform>
        <iteration>
            <hint message="Migrate to jBPM 5 org.drools.runtime.process.WorkItemHandler." effort="2">
            </hint>
        </iteration>
    </perform>
</rule>

(See rules-java-ee/src/main/resources/legacy/org.jboss.windup.rules.apps.legacy.java.JBossConfig.windup.xml)

Method call

import org.jbpm.graph.exe.ExecutionContext;
...
    public void myMethod( ExecutionContext exCtx){
        exCtx.setVariable("foo", "bar");
    }
...
<rule id="UNIQUE_RULE_ID">
    <when>
        <javaclass references="org.jbpm.graph.exe.ExecutionContext.setVariable" as="default">
            <location>METHOD</location>
        </javaclass>
    </when>
    <perform>
        <iteration>
            <hint effort="0">...</hint>
        </iteration>
    </perform>
</rule>

Annotation

import org.jboss.ejb3.annotation.Management;
...
@Service( name = "DeliveryManager" )
@Local( DeliveryManagerLocal.class )
@Management( DeliveryManagerManagement.class )
public class DeliveryManager implements DeliveryManagerManagement
{
    @Resource( mappedName = "Delivery/remote" )
    DeliveryRemote delivery;
<when>
    <javaclass references="org.jboss.ejb3.annotation.Management" as="default">
        <location>ANNOTATION</location>
    </javaclass>
</when>

Inheritance (Foo extends Bar)

public interface StockHome extends javax.ejb.EJBHome
{
    Stock create() throws java.rmi.RemoteException, javax.ejb.CreateException;
}
...
<when>
    <javaclass references="javax.ejb.EJBHome" as="default">
        <location>INHERITANCE</location>
    </javaclass>
</when>

Constructor call

JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://localhost:1099");
<when>
    <javaclass references="javax.management.remote.JMXServiceURL({*})" as="default">
        <location>CONSTRUCTOR_CALL</location>
    </javaclass>
</when>

Type (classes, interfaces, …​) references (anywhere, e.g. as method parameter)

...
<when>
    <javaclass references="java.awt.Frame.E_RESIZE_CURSOR" as="default">
        <location>TYPE</location>
    </javaclass>
</when>

Template:

<What is matched>

<Matched java code example>
...
<rule that matches that construct>
Clone this wiki locally