Skip to content
Open
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
5 changes: 5 additions & 0 deletions jcodemodel/src/main/java/com/helger/jcodemodel/JExpr.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.jspecify.annotations.Nullable;

import com.helger.annotation.Nonnegative;
import com.helger.jcodemodel.expressions.JInstanceOfVar;

/**
* Factory methods that generate various {@link IJExpression}s.
Expand Down Expand Up @@ -553,4 +554,8 @@ public static JOpTernary cond (@NonNull final IJExpression aCond,
{
return JOp.cond (aCond, aIfTrue, aIfFalse);
}

public static JInstanceOfVar instanceOf(@NonNull IJExpression expr, @NonNull AbstractJType type, @NonNull String name) {
return new JInstanceOfVar(expr, type, name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.helger.jcodemodel.expressions;

import org.jspecify.annotations.NonNull;

import com.helger.jcodemodel.AbstractJType;
import com.helger.jcodemodel.IJExpression;
import com.helger.jcodemodel.IJFormatter;
import com.helger.jcodemodel.JMods;
import com.helger.jcodemodel.JVar;

public class JInstanceOfVar implements IJExpression {

private final IJExpression m_oExpr;
private final JVar m_oVar;

public JInstanceOfVar(@NonNull final IJExpression expr,
@NonNull final AbstractJType type,
@NonNull final String name) {
m_oExpr = expr;
m_oVar = new JVar(JMods.forVar(0), type, name, null);
}

public IJExpression expr() {
return m_oExpr;
}

public JVar var() {
return m_oVar;
}

@Override
public void generate(@NonNull IJFormatter f) {
f.print('(').generable(m_oExpr).print("instanceof").var(m_oVar).print(')');
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.helger.jcodemodel.tests.instanceofvar;

import java.util.Collection;
import java.util.Map;
import javax.annotation.processing.Generated;

@Generated("com.helger.jcodemodel.JCodeModel")
public class ExampleInstanceOfVar {

public static int toInt(Object o) {
if (o == null) {
return 0;
}
if ((o instanceof String s)&&(!s.isBlank())) {
return s.strip().length();
}
if ((o instanceof Collection c)) {
return c.size();
}
if ((o instanceof Map m)) {
return m.size();
}
if ((o instanceof Number n)) {
return n.intValue();
}
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.helger.jcodemodel.tests.instanceofvar;

import java.util.Collection;
import java.util.Map;

import com.helger.jcodemodel.JExpr;
import com.helger.jcodemodel.JMod;
import com.helger.jcodemodel.JPackage;
import com.helger.jcodemodel.compile.annotation.TestJCM;
import com.helger.jcodemodel.exceptions.JCodeModelException;

@TestJCM
public class InstanceOfVarTestGen {

public void instanceOfVarGen(final JPackage root) throws JCodeModelException {
var cl = root._class("ExampleInstanceOfVar");
var jm = cl.method(JMod.PUBLIC | JMod.STATIC, root.owner().INT, "toInt");
var param = jm.param(Object.class, "o");

jm.body()._if(param.eqNull())._then()._return(JExpr.lit(0));

// string : strip.length
var instanceOf = JExpr.instanceOf(param, root.owner().ref(String.class), "s");
jm.body()._if(instanceOf.cand(instanceOf.var().invoke("isBlank").not()))
._then()._return(instanceOf.var().invoke("strip").invoke("length"));

// collection : size
instanceOf = JExpr.instanceOf(param, root.owner().ref(Collection.class), "c");
jm.body()._if(instanceOf)
._then()._return(instanceOf.var().invoke("size"));

// Map : size
instanceOf = JExpr.instanceOf(param, root.owner().ref(Map.class), "m");
jm.body()._if(instanceOf)
._then()._return(instanceOf.var().invoke("size"));

// Number : intValue
instanceOf = JExpr.instanceOf(param, root.owner().ref(Number.class), "n");
jm.body()._if(instanceOf)
._then()._return(instanceOf.var().invoke("intValue"));

jm.body()._return(JExpr.lit(0));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.helger.jcodemodel.tests.instanceofvar;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.Assert;
import org.junit.Test;

public class InstanceOfVarTest {

@Test
public void testToInt() {
Assert.assertEquals(0, ExampleInstanceOfVar.toInt(null));

// strings
Assert.assertEquals(0, ExampleInstanceOfVar.toInt(""));
Assert.assertEquals(0, ExampleInstanceOfVar.toInt("\t "));
Assert.assertEquals(3, ExampleInstanceOfVar.toInt("\tabc "));

// collection
Assert.assertEquals(0, ExampleInstanceOfVar.toInt(Set.of()));
Assert.assertEquals(1, ExampleInstanceOfVar.toInt(List.of("")));

// map
Assert.assertEquals(0, ExampleInstanceOfVar.toInt(Map.of()));
Assert.assertEquals(2, ExampleInstanceOfVar.toInt(Map.of(1, 1, 2, 4)));

// numbers
Assert.assertEquals(0, ExampleInstanceOfVar.toInt(0));
Assert.assertEquals(0, ExampleInstanceOfVar.toInt(0.0));
Assert.assertEquals(0, ExampleInstanceOfVar.toInt(new BigDecimal(0L)));
Assert.assertEquals(42, ExampleInstanceOfVar.toInt(42));
Assert.assertEquals(2, ExampleInstanceOfVar.toInt(2.1));

// Object

Assert.assertEquals(0, ExampleInstanceOfVar.toInt(new Object()));

}

}