Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
Add support for casts
TNG#710

Signed-off-by: Kaammill  <>
  • Loading branch information
Kaammill committed Mar 3, 2022
1 parent 8f56568 commit bf97930
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 36 deletions.
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> getypeCast() {
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.getypeCast()) {
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,82 @@
/*
* 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 int lineNumber;
private final SourceCodeLocation sourceCodeLocation;

private TypeCast(JavaCodeUnit owner, JavaClass value, int lineNumber) {
this.owner = checkNotNull(owner);
this.value = checkNotNull(value);
this.lineNumber = lineNumber;
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;
}

@PublicAPI(usage = ACCESS)
public int getLineNumber() {
return lineNumber;
}

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

@Override
public String toString() {
return toStringHelper(this)
.add("owner", owner)
.add("target", value)
.add("lineNumber", lineNumber)
.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 @@ -34,54 +34,20 @@
import com.tngtech.archunit.base.Function;
import com.tngtech.archunit.base.HasDescription;
import com.tngtech.archunit.base.Optional;
import com.tngtech.archunit.core.domain.AccessTarget;
import com.tngtech.archunit.core.domain.*;
import com.tngtech.archunit.core.domain.AccessTarget.CodeUnitAccessTarget;
import com.tngtech.archunit.core.domain.AccessTarget.ConstructorCallTarget;
import com.tngtech.archunit.core.domain.AccessTarget.ConstructorReferenceTarget;
import com.tngtech.archunit.core.domain.AccessTarget.FieldAccessTarget;
import com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget;
import com.tngtech.archunit.core.domain.AccessTarget.MethodReferenceTarget;
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.JavaAnnotation;
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.domain.JavaClassDescriptor;
import com.tngtech.archunit.core.domain.JavaCodeUnit;
import com.tngtech.archunit.core.domain.JavaConstructor;
import com.tngtech.archunit.core.domain.JavaConstructorCall;
import com.tngtech.archunit.core.domain.JavaConstructorReference;
import com.tngtech.archunit.core.domain.JavaEnumConstant;
import com.tngtech.archunit.core.domain.JavaField;
import com.tngtech.archunit.core.domain.JavaFieldAccess;
import com.tngtech.archunit.core.domain.JavaFieldAccess.AccessType;
import com.tngtech.archunit.core.domain.JavaMember;
import com.tngtech.archunit.core.domain.JavaMethod;
import com.tngtech.archunit.core.domain.JavaMethodCall;
import com.tngtech.archunit.core.domain.JavaMethodReference;
import com.tngtech.archunit.core.domain.JavaModifier;
import com.tngtech.archunit.core.domain.JavaParameter;
import com.tngtech.archunit.core.domain.JavaParameterizedType;
import com.tngtech.archunit.core.domain.JavaStaticInitializer;
import com.tngtech.archunit.core.domain.JavaType;
import com.tngtech.archunit.core.domain.JavaTypeVariable;
import com.tngtech.archunit.core.domain.JavaWildcardType;
import com.tngtech.archunit.core.domain.ReferencedClassObject;
import com.tngtech.archunit.core.domain.Source;
import com.tngtech.archunit.core.domain.ThrowsClause;
import com.tngtech.archunit.core.domain.properties.HasTypeParameters;

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 +214,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 +256,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 +331,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();
}
}

0 comments on commit bf97930

Please sign in to comment.