-
Notifications
You must be signed in to change notification settings - Fork 306
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't have to store the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok i will not store 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 |
---|---|---|
|
@@ -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.*; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry for it, did not set intellij auto format, |
||
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; | ||
|
@@ -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() { | ||
} | ||
|
@@ -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) { | ||
|
@@ -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) { | ||
|
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(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
getTypeCast
s
? (And same inJavaMembers
.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, so the idea is to move typeCast inside JavaMembers class? or just call getTypeCasts inside it like for example getReferencedClassObjects method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method is currently called
getypeCast
. As it returns aSet
ofTypeCast
objects, I thought that a pluralized namegetTypeCasts
might be more appropriate.The delegation to
JavaMembers
is probably fine, but I'd also rename the method there.