Skip to content

Commit

Permalink
Improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
matschieu committed Oct 23, 2023
1 parent a6f6a8b commit 75d3597
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ public final class AnnotationUtils {
private AnnotationUtils() { }

public static final Annotation toAnnotation(Class<? extends Annotation> annotationType) {
return new Annotation() {
@Override
public Class<? extends Annotation> annotationType() {
return annotationType;
}
};
return () -> annotationType;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,29 @@

public class NumberProducer {

private static final java.util.Random RANDOM = new java.util.Random();

// A producer can be a field
// It must be a default-access, public, protected or private, field of a managed bean class.
// A producer field may be either static or non-static.
@Produces
@Null
private Integer nullInteger = null;
private final Integer nullInteger = null;

// A producer method must be a default-access, public, protected or private, non-abstract method of a managed bean class
// A producer method may be either static or non-static
// It can return null but it must have scope @Dependent else an IllegalProductException is thrown by the container.
@Produces
@Random
public Integer getRandomInteger() {
return Integer.valueOf((new java.util.Random()).nextInt());
return Integer.valueOf(RANDOM.nextInt());
}

// A producer can also return a primitive type
@Produces
@Random
public static double getRandomInt() {
return 1.0 * (new java.util.Random()).nextInt();
return 1.0 * RANDOM.nextInt();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public class IntIncrementInterceptor {
public Object interceptOrder(final InvocationContext ctx) throws Exception {
System.out.println(this.getClass().getSimpleName() + ": Intercept " + ctx.getMethod().getName());
final Object output = ctx.proceed();
if (output instanceof Integer) {
return ((Integer) output).intValue() + 1;
if (output instanceof final Integer integer) {
return integer.intValue() + 1;
}
return output;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@Interceptor
public class ValidationInterceptor {

private void checkObject(final Object obj) throws Exception {
private void checkObject(final Object obj) throws IllegalArgumentException, IllegalAccessException, NullElementException {
if ((obj == null) || (obj instanceof String)) {
return;
}
Expand All @@ -27,7 +27,7 @@ private void checkObject(final Object obj) throws Exception {
}
}

private void checkFieldAnnotation(final String fieldName, final Annotation[] annotations, final Object value) throws Exception {
private void checkFieldAnnotation(final String fieldName, final Annotation[] annotations, final Object value) throws NullElementException {
for (final Annotation annotation : annotations) {
if (annotation.annotationType() == NotNullElement.class && value == null) {
throw new NullElementException(fieldName + " is null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import jakarta.inject.Inject;

@EnableWeld
public class MyServiceTest {
class MyServiceTest {

@WeldSetup
public WeldInitiator weld = WeldInitiator.from(MyService.class, MyServiceImpl.class).build();
Expand All @@ -18,7 +18,7 @@ public class MyServiceTest {
private MyService myService;

@Test
public void testMyService() {
void testMyService() {
Assertions.assertEquals(2, this.myService.getInt());
Assertions.assertEquals(Integer.valueOf(2), this.myService.getInteger());
Assertions.assertEquals("{[MyString]}", this.myService.getString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import jakarta.inject.Inject;

@EnableWeld
public class ResourceServiceTest {
class ResourceServiceTest {

@WeldSetup
public WeldInitiator weld = WeldInitiator.from(ResourceService.class, ResourceServiceImpl.class).build();
Expand All @@ -20,7 +20,7 @@ public class ResourceServiceTest {
private ResourceService resourceService;

@Test
public void testMyServiceCreateResource1() {
void testMyServiceCreateResource1() {
try {
this.resourceService.createResource(null, "", "");
fail();
Expand All @@ -41,7 +41,7 @@ public void testMyServiceCreateResource1() {
}

@Test
public void testMyServiceCreateResource2() {
void testMyServiceCreateResource2() {
try {
this.resourceService.createResource(new Resource(null, "", ""));
fail();
Expand All @@ -62,7 +62,7 @@ public void testMyServiceCreateResource2() {
}

@Test
public void testMyServiceGetResource() {
void testMyServiceGetResource() {
Resource resource;

try {
Expand All @@ -82,7 +82,7 @@ public void testMyServiceGetResource() {
}

@Test
public void testMyServiceDeleteResource() {
void testMyServiceDeleteResource() {
try {
this.resourceService.deleteResource(null);
fail();
Expand Down

0 comments on commit 75d3597

Please sign in to comment.