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

Support class versions #1418

Open
wants to merge 1 commit 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 @@ -77,6 +77,7 @@ public final class JavaClass
private final Optional<Source> source;
private final SourceCodeLocation sourceCodeLocation;
private final JavaClassDescriptor descriptor;
private final JavaClassVersion version;
private JavaPackage javaPackage;
private final boolean isInterface;
private final boolean isEnum;
Expand Down Expand Up @@ -133,6 +134,7 @@ public final class JavaClass
JavaClass(JavaClassBuilder builder) {
source = checkNotNull(builder.getSource());
descriptor = checkNotNull(builder.getDescriptor());
version = checkNotNull(builder.getVersion());
isInterface = builder.isInterface();
isEnum = builder.isEnum();
isAnnotation = builder.isAnnotation();
Expand Down Expand Up @@ -189,6 +191,13 @@ public String getSimpleName() {
return descriptor.getSimpleClassName();
}

/**
* @return The version of this {@link JavaClass}.
*/
public JavaClassVersion getJavaVersion() {
return version;
}

@PublicAPI(usage = ACCESS)
public JavaPackage getPackage() {
return javaPackage;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.tngtech.archunit.core.domain;

/**
* Represents the version of a JVM class file.
*/
public class JavaClassVersion {

private final int major;
private final int minor;

/**
* Create a new instance of {@link JavaClassVersion}.
*
* @param major The major version of the class file.
* @param minor The minor version of the class file.
*/
public JavaClassVersion(int major, int minor) {
this.major = major;
this.minor = minor;
}

/**
* Get the Java version corresponding to the major version of the class file.
*
* @return The Java version
*/
public int getJavaVersion() {
return major - 44;
}

/**
* Get the major version of the class file.
*
* @return The major version
*/
public int getBytecodeMajorVersion() {
return major;
}

/**
* Get the minor version of the class file.
*
* @return The minor version
*/
public int getBytecodeMinorVersion() {
return minor;
}

/**
* Create a new instance of {@link JavaClassVersion} from the version as provided by ASM.
*
* @return A representation of the class file version
*/
public static JavaClassVersion of(int asmVersion) {
int major = asmVersion & 0xFF;
int minor = asmVersion >> 16;

return new JavaClassVersion(major, minor);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
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.JavaClassVersion;
import com.tngtech.archunit.core.domain.JavaCodeUnit;
import com.tngtech.archunit.core.domain.JavaConstructor;
import com.tngtech.archunit.core.domain.JavaConstructorCall;
Expand Down Expand Up @@ -392,6 +393,7 @@ public static final class JavaClassBuilder {
private Optional<SourceDescriptor> sourceDescriptor = Optional.empty();
private Optional<String> sourceFileName = Optional.empty();
private JavaClassDescriptor descriptor;
private JavaClassVersion version;
private boolean isInterface;
private boolean isEnum;
private boolean isAnnotation;
Expand Down Expand Up @@ -423,6 +425,11 @@ JavaClassBuilder withDescriptor(JavaClassDescriptor descriptor) {
return this;
}

JavaClassBuilder withVersion(JavaClassVersion version) {
this.version = version;
return this;
}

JavaClassBuilder withInterface(boolean isInterface) {
this.isInterface = isInterface;
return this;
Expand Down Expand Up @@ -475,6 +482,10 @@ public JavaClassDescriptor getDescriptor() {
return descriptor;
}

public JavaClassVersion getVersion() {
return version;
}

public boolean isInterface() {
return isInterface;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
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.JavaClassVersion;
import com.tngtech.archunit.core.domain.JavaEnumConstant;
import com.tngtech.archunit.core.domain.JavaField;
import com.tngtech.archunit.core.domain.JavaModifier;
Expand Down Expand Up @@ -107,6 +108,8 @@ public void visit(int version, int access, String name, String signature, String
return;
}

JavaClassVersion classVersion = JavaClassVersion.of(version);

List<String> interfaceNames = createInterfaceNames(interfaces);
LOG.trace("Found interfaces {} on class '{}'", interfaceNames, name);
boolean opCodeForInterfaceIsPresent = (access & Opcodes.ACC_INTERFACE) != 0;
Expand All @@ -117,6 +120,7 @@ public void visit(int version, int access, String name, String signature, String
LOG.trace("Found superclass {} on class '{}'", superclassName.orElse(null), name);

javaClassBuilder = new DomainBuilders.JavaClassBuilder()
.withVersion(classVersion)
.withSourceDescriptor(sourceDescriptor)
.withDescriptor(descriptor)
.withInterface(opCodeForInterfaceIsPresent)
Expand Down