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

Get modifiers of JavaParameter #1230

Open
priyanthabuddhika opened this issue Jan 11, 2024 · 1 comment
Open

Get modifiers of JavaParameter #1230

priyanthabuddhika opened this issue Jan 11, 2024 · 1 comment

Comments

@priyanthabuddhika
Copy link

priyanthabuddhika commented Jan 11, 2024

I am trying to create a custom arch unit rule to validate if method parameters have a final modifier. However, JavaParameter class does not seems to have a getModifiers method like JavaMethod class. Is this achievable with current ArchUnit API ?

I'm using ArchUnit 1.2.1 version.

This is a code sample of what I'm trying to achieve.

public void check(JavaMethod method, ConditionEvents events) {
            boolean allParametersAreFinal = true;

            for (JavaParameter parameter : method.getParameters()) {
                if (!parameter.getModifiers().contains(JavaModifier.FINAL)) {  // <---------- Something like this ?
                    allParametersAreFinal = false;
                    events.add(SimpleConditionEvent.violated(method,
                            String.format("Parameter %s of method %s is not final",
                                    parameter.getOwner().getFullName(), method.getSourceCodeLocation())));
                }
            }

            if (allParametersAreFinal) {
                events.add(SimpleConditionEvent.satisfied(method,
                        "Parameters of method " + method.getFullName() + " are final"));
            }
        }
    }
@hankem
Copy link
Member

hankem commented Jan 12, 2024

I fear that final method parameters might just exist in the Java language, but and in the byte code/JVM.
(I'm saying "might" because I don't know much about the JVM specification...)

I've just observed that for the simple example

class C {
   void method(int p1, final int p2) {
   }
}

javap does not show any difference between the parameters p1 and p2 in the byte code:

  void method(int, int);
    descriptor: (II)V
    flags: (0x0000)
    Code:
      stack=0, locals=3, args_size=3
         0: return

So the descriptor: (II)V might be everything that is available to ArchUnit. (I'm saying "might"...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants