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 support for casts #822

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ static Set<Dependency> tryCreateFromReferencedClassObject(ReferencedClassObject
referencedClassObject.getRawType(), referencedClassObject.getSourceCodeLocation());
}

static Set<Dependency> tryCreateFromTypeCast(TypeCast typeCast) {
return tryCreateDependency(
typeCast.getOwner(), "checks TypeCast",
typeCast.getRawType(), typeCast.getSourceCodeLocation());
}

static Set<Dependency> tryCreateFromAnnotation(JavaAnnotation<?> target) {
Origin origin = findSuitableOrigin(target, target.getAnnotatedElement());
return tryCreateDependency(origin, "is annotated with", target.getRawType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ public static InstanceofCheck createInstanceofCheck(JavaCodeUnit codeUnit, JavaC
return InstanceofCheck.from(codeUnit, target, lineNumber);
}

public static TypeCast createTypeCast(JavaCodeUnit codeUnit, JavaClass javaClass, int lineNumber) {
return TypeCast.from(codeUnit, javaClass, lineNumber);
}

public static <OWNER extends HasDescription> JavaTypeVariable<OWNER> createTypeVariable(String name, OWNER owner, JavaClass erasure) {
return new JavaTypeVariable<>(name, owner, erasure);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,11 @@ public Set<ReferencedClassObject> getReferencedClassObjects() {
return members.getReferencedClassObjects();
}

@PublicAPI(usage = ACCESS)
public Set<TypeCast> getypeCasts() {
return members.getTypeCast();
}

@Override
@PublicAPI(usage = ACCESS)
public JavaClass toErasure() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public Set<Dependency> get() {
result.addAll(annotationDependenciesFromSelf());
result.addAll(instanceofCheckDependenciesFromSelf());
result.addAll(referencedClassObjectDependenciesFromSelf());
result.addAll(typeCastDependenciesFromSelf());
result.addAll(typeParameterDependenciesFromSelf());
return result.build();
}
Expand Down Expand Up @@ -218,6 +219,14 @@ private Set<Dependency> referencedClassObjectDependenciesFromSelf() {
return result.build();
}

private Set<Dependency> typeCastDependenciesFromSelf() {
ImmutableSet.Builder<Dependency> result = ImmutableSet.builder();
for (TypeCast typeCast : javaClass.getypeCasts()) {
result.addAll(Dependency.tryCreateFromTypeCast(typeCast));
}
return result.build();
}

private Set<Dependency> typeParameterDependenciesFromSelf() {
ImmutableSet.Builder<Dependency> result = ImmutableSet.builder();
result.addAll(classTypeParameterDependenciesFromSelf());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ Set<ReferencedClassObject> getReferencedClassObjects() {
return result.build();
}

Set<TypeCast> getTypeCast() {
ImmutableSet.Builder<TypeCast> result = ImmutableSet.builder();
for (JavaCodeUnit codeUnit : codeUnits) {
result.addAll(codeUnit.getTypeCast());
}
return result.build();
}

Set<JavaFieldAccess> getFieldAccessesFromSelf() {
ImmutableSet.Builder<JavaFieldAccess> result = ImmutableSet.builder();
for (JavaCodeUnit codeUnit : codeUnits) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public abstract class JavaCodeUnit
private final List<JavaTypeVariable<JavaCodeUnit>> typeParameters;
private final Set<ReferencedClassObject> referencedClassObjects;
private final Set<InstanceofCheck> instanceofChecks;
private final Set<TypeCast> typeCast;

private Set<JavaFieldAccess> fieldAccesses = Collections.emptySet();
private Set<JavaMethodCall> methodCalls = Collections.emptySet();
Expand All @@ -76,6 +77,7 @@ public abstract class JavaCodeUnit
fullName = formatMethod(getOwner().getName(), getName(), namesOf(getRawParameterTypes()));
referencedClassObjects = ImmutableSet.copyOf(builder.getReferencedClassObjects(this));
instanceofChecks = ImmutableSet.copyOf(builder.getInstanceofChecks(this));
typeCast = ImmutableSet.copyOf(builder.getRawTypeCasts(this));
}

/**
Expand Down Expand Up @@ -201,6 +203,11 @@ public Set<InstanceofCheck> getInstanceofChecks() {
return instanceofChecks;
}

@PublicAPI(usage = ACCESS)
public Set<TypeCast> getTypeCast() {
return typeCast;
}

@PublicAPI(usage = ACCESS)
public Set<JavaCall<?>> getCallsFromSelf() {
return union(getMethodCallsFromSelf(), getConstructorCallsFromSelf());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2014-2022 TNG Technology Consulting GmbH
*
* 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.tngtech.archunit.core.domain;

import com.tngtech.archunit.PublicAPI;
import com.tngtech.archunit.core.domain.properties.HasOwner;
import com.tngtech.archunit.core.domain.properties.HasSourceCodeLocation;
import com.tngtech.archunit.core.domain.properties.HasType;

import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.tngtech.archunit.PublicAPI.Usage.ACCESS;

@PublicAPI(usage = ACCESS)
public final class TypeCast implements HasType, HasOwner<JavaCodeUnit>, HasSourceCodeLocation {

private final JavaCodeUnit owner;
private final JavaClass value;
private final SourceCodeLocation sourceCodeLocation;

private TypeCast(JavaCodeUnit owner, JavaClass value, int lineNumber) {
this.owner = checkNotNull(owner);
this.value = checkNotNull(value);
sourceCodeLocation = SourceCodeLocation.of(owner.getOwner(), lineNumber);
}

@Override
@PublicAPI(usage = ACCESS)
public JavaClass getRawType() {
return value;
}

@Override
@PublicAPI(usage = ACCESS)
public JavaType getType() {
return value;
}

@Override
@PublicAPI(usage = ACCESS)
public JavaCodeUnit getOwner() {
return owner;
}

@Override
public SourceCodeLocation getSourceCodeLocation() {
return sourceCodeLocation;
}

@Override
public String toString() {
return toStringHelper(this)
.add("owner", owner)
.add("target", value)
.toString();
}

static TypeCast from(JavaCodeUnit owner, JavaClass target, int lineNumber) {
return new TypeCast(owner, target, lineNumber);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.tngtech.archunit.core.domain.DomainObjectCreationContext;
import com.tngtech.archunit.core.domain.Formatters;
import com.tngtech.archunit.core.domain.InstanceofCheck;
import com.tngtech.archunit.core.domain.TypeCast;
import com.tngtech.archunit.core.domain.JavaAnnotation;
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.domain.JavaClassDescriptor;
Expand Down Expand Up @@ -74,14 +75,7 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Sets.union;
import static com.tngtech.archunit.core.domain.DomainObjectCreationContext.completeTypeVariable;
import static com.tngtech.archunit.core.domain.DomainObjectCreationContext.createGenericArrayType;
import static com.tngtech.archunit.core.domain.DomainObjectCreationContext.createInstanceofCheck;
import static com.tngtech.archunit.core.domain.DomainObjectCreationContext.createReferencedClassObject;
import static com.tngtech.archunit.core.domain.DomainObjectCreationContext.createSource;
import static com.tngtech.archunit.core.domain.DomainObjectCreationContext.createThrowsClause;
import static com.tngtech.archunit.core.domain.DomainObjectCreationContext.createTypeVariable;
import static com.tngtech.archunit.core.domain.DomainObjectCreationContext.createWildcardType;
import static com.tngtech.archunit.core.domain.DomainObjectCreationContext.*;
import static com.tngtech.archunit.core.domain.Formatters.ensureCanonicalArrayTypeName;
import static com.tngtech.archunit.core.domain.JavaConstructor.CONSTRUCTOR_NAME;
import static com.tngtech.archunit.core.domain.properties.HasName.Utils.namesOf;
Expand Down Expand Up @@ -248,6 +242,7 @@ public abstract static class JavaCodeUnitBuilder<OUTPUT, SELF extends JavaCodeUn
private List<JavaClassDescriptor> throwsDeclarations;
private final Set<RawReferencedClassObject> rawReferencedClassObjects = new HashSet<>();
private final List<RawInstanceofCheck> instanceOfChecks = new ArrayList<>();
private final List<RawTypeCast> rawTypeCasts = new ArrayList<>();

private JavaCodeUnitBuilder() {
}
Expand Down Expand Up @@ -289,6 +284,11 @@ SELF addInstanceOfCheck(RawInstanceofCheck rawInstanceOfChecks) {
return self();
}

SELF addRawTypeCast(RawTypeCast rawTypeCast) {
this.rawTypeCasts.add(rawTypeCast);
return self();
}

List<String> getParameterTypeNames() {
ImmutableList.Builder<String> result = ImmutableList.builder();
for (JavaClassDescriptor parameter : rawParameterTypes) {
Expand Down Expand Up @@ -359,6 +359,14 @@ public Set<InstanceofCheck> getInstanceofChecks(JavaCodeUnit codeUnit) {
return result.build();
}

public Set<TypeCast> getRawTypeCasts(JavaCodeUnit codeUnit) {
ImmutableSet.Builder<TypeCast> result = ImmutableSet.builder();
for (RawTypeCast rawTypeCast : this.rawTypeCasts) {
result.add(createTypeCast(codeUnit, get(rawTypeCast.getTarget().getFullyQualifiedClassName()), rawTypeCast.getLineNumber()));
}
return result.build();
}

private List<JavaClass> asJavaClasses(List<JavaClassDescriptor> descriptors) {
ImmutableList.Builder<JavaClass> result = ImmutableList.builder();
for (JavaClassDescriptor javaClassDescriptor : descriptors) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2014-2022 TNG Technology Consulting GmbH
*
* 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.tngtech.archunit.core.importer;

import com.tngtech.archunit.core.domain.JavaClassDescriptor;

import static com.google.common.base.MoreObjects.toStringHelper;

class RawTypeCast {

private final JavaClassDescriptor target;
private final int lineNumber;

private RawTypeCast(JavaClassDescriptor target, int lineNumber) {
this.target = target;
this.lineNumber = lineNumber;
}

static RawTypeCast from(JavaClassDescriptor target, int lineNumber) {
return new RawTypeCast(target, lineNumber);
}

JavaClassDescriptor getTarget() {
return target;
}

int getLineNumber() {
return lineNumber;
}

@Override
public String toString() {
return toStringHelper(this)
.add("target", target)
.add("lineNumber", lineNumber)
.toString();
}
}