Skip to content

Commit

Permalink
fix: NullPointerException on CtTypeReferenceImpl.isGenerics (#3276)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinWitt authored Mar 2, 2020
1 parent 21de813 commit 904fb1e
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public CtTypeParameter getDeclaration() {

if (parent instanceof CtExecutableReference) {
CtExecutableReference parentExec = (CtExecutableReference) parent;
if (!parentExec.getDeclaringType().equals(e)) {
if (Objects.nonNull(parentExec.getDeclaringType()) && !parentExec.getDeclaringType().equals(e)) {
CtElement parent2 = parentExec.getExecutableDeclaration();
if (parent2 instanceof CtMethod) {
e = parent2;
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/spoon/Issue3275Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package spoon;

import org.junit.Test;

import spoon.processing.AbstractProcessor;
import spoon.reflect.CtModel;
import spoon.reflect.reference.CtTypeReference;

/**
* Issue3275
*/
public class Issue3275Test {
// the following code reproduces the behavior from the issue
@Test
public void test() {
CtModel model = new FluentLauncher().inputResource("src/test/resources/issue3275/BOMCostPrice.java")
.noClasspath(true)
.processor(new AbstractProcessor<CtTypeReference<?>>() {
public void process(CtTypeReference<?> element) {
System.out.println(element.isGenerics());
}
})
.buildModel();
}
}
77 changes: 77 additions & 0 deletions src/test/resources/issue3275/BOMCostPrice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.eevolution.costing;

import java.util.Collection;
import java.util.HashMap;
import java.util.stream.Stream;

import com.google.common.base.Predicates;

import de.metas.costing.CostAmount;
import de.metas.costing.CostElementId;
import de.metas.product.ProductId;
import de.metas.util.GuavaCollectors;
import de.metas.util.lang.RepoIdAware;
import lombok.Builder;
import lombok.Getter;
import lombok.NonNull;
import lombok.Singular;
import lombok.ToString;

@ToString
public class BOMCostPrice {
public static BOMCostPrice empty(@NonNull final ProductId productId) {
return builder().productId(productId).build();
}

@Getter
private final ProductId productId;
private final HashMap<CostElementId, BOMCostElementPrice> pricesByElementId;

@Builder
private BOMCostPrice(@NonNull final ProductId productId,
@NonNull @Singular final Collection<BOMCostElementPrice> costElementPrices) {
this.productId = productId;
pricesByElementId = costElementPrices.stream()
.collect(GuavaCollectors.toHashMapByKey(BOMCostElementPrice::getCostElementId));
}

public Stream<CostElementId> streamCostElementIds() {
return pricesByElementId.keySet().stream();
}

public BOMCostElementPrice getCostElementPriceOrNull(@NonNull final CostElementId costElementId) {
return pricesByElementId.get(costElementId);
}

public void clearOwnCostPrice(@NonNull final CostElementId costElementId) {
final BOMCostElementPrice elementCostPrice = getCostElementPriceOrNull(costElementId);
if (elementCostPrice != null) {
elementCostPrice.clearOwnCostPrice();
}
}

public void setComponentsCostPrice(@NonNull final CostAmount costPrice,
@NonNull final CostElementId costElementId) {
pricesByElementId
.computeIfAbsent(costElementId, k -> BOMCostElementPrice.zero(costElementId, costPrice.getCurrencyId()))
.setComponentsCostPrice(costPrice);
}

public void clearComponentsCostPrice(@NonNull final CostElementId costElementId) {
final BOMCostElementPrice elementCostPrice = getCostElementPriceOrNull(costElementId);
if (elementCostPrice != null) {
elementCostPrice.clearComponentsCostPrice();
}
}

Collection<BOMCostElementPrice> getElementPrices() {
return pricesByElementId.values();
}

<T extends RepoIdAware> Stream<T> streamIds(@NonNull final Class<T> idType) {
return getElementPrices().stream()
.map(BOMCostElementPrice::getId)
.filter(Predicates.notNull())
.map(idType::cast);
}
}

0 comments on commit 904fb1e

Please sign in to comment.