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

[apex] [7.0-rc1] Exception while initializing Apexlink (Index 34812 out of bounds for length 34812) #4453

Closed
nwcm opened this issue Apr 4, 2023 · 7 comments · Fixed by #4610
Labels
a:bug PMD crashes or fails to analyse a file.
Milestone

Comments

@nwcm
Copy link
Contributor

nwcm commented Apr 4, 2023

Affects PMD Version: 7.0.0-rc1

Make sure, to test with the latest PMD version.

Description:

Unable to utilize rule UnusedMethod

Exception Stacktrace:

[main] DEBUG net.sourceforge.pmd.lang.apex.multifile.ApexMultifileAnalysis - Parsed 709 classes in 929ms
[main] ERROR net.sourceforge.pmd.lang.apex.multifile.ApexMultifileAnalysis - Exception while initializing Apexlink (Index 34812 out of bounds for length 34812)
java.lang.ArrayIndexOutOfBoundsException: Index 34812 out of bounds for length 34812
        at com.nawforce.common.parsers.UTF8Decode$.getCharOffsetFrom(UTF8Decode.scala:38)
        at com.nawforce.runtime.parsers.ByteArraySourceData.subdata(SourceData.scala:66)
        at com.nawforce.runtime.parsers.ByteArraySourceData.subdata(SourceData.scala:61)
        at com.nawforce.runtime.parsers.Source.extractSource(Source.scala:57)
        at com.nawforce.runtime.parsers.CodeParser.extractSource(CodeParser.scala:76)
        at com.nawforce.common.cst.Block$.constructLazy(Statements.scala:108)
        at com.nawforce.common.cst.ApexMethodDeclaration$.$anonfun$construct$17(BodyDeclarations.scala:273)
        at scala.Option.map(Option.scala:242)
        at com.nawforce.common.cst.ApexMethodDeclaration$.construct(BodyDeclarations.scala:273)
        at com.nawforce.common.cst.ClassBodyDeclaration$.$anonfun$construct$1(BodyDeclarations.scala:106)
        at scala.Option.map(Option.scala:242)
        at com.nawforce.common.cst.ClassBodyDeclaration$.construct(BodyDeclarations.scala:99)
        at com.nawforce.common.cst.ClassDeclaration$.$anonfun$construct$9(TypeDeclarations.scala:120)
        at scala.Option.map(Option.scala:242)
        at com.nawforce.common.cst.ClassDeclaration$.$anonfun$construct$8(TypeDeclarations.scala:119)
        at scala.Option.orElse(Option.scala:477)
        at com.nawforce.common.cst.ClassDeclaration$.$anonfun$construct$6(TypeDeclarations.scala:119)
        at scala.collection.immutable.List.flatMap(List.scala:293)
        at scala.collection.immutable.List.flatMap(List.scala:79)
        at com.nawforce.common.cst.ClassDeclaration$.construct(TypeDeclarations.scala:114)
        at com.nawforce.common.cst.ClassDeclaration$.constructInner(TypeDeclarations.scala:95)
        at com.nawforce.common.cst.ClassBodyDeclaration$.$anonfun$construct$13(BodyDeclarations.scala:178)
        at scala.Option.map(Option.scala:242)
        at com.nawforce.common.cst.ClassBodyDeclaration$.$anonfun$construct$12(BodyDeclarations.scala:171)
        at scala.Option.orElse(Option.scala:477)
        at com.nawforce.common.cst.ClassBodyDeclaration$.construct(BodyDeclarations.scala:170)
        at com.nawforce.common.cst.ClassDeclaration$.$anonfun$construct$9(TypeDeclarations.scala:120)
        at scala.Option.map(Option.scala:242)
        at com.nawforce.common.cst.ClassDeclaration$.$anonfun$construct$8(TypeDeclarations.scala:119)
        at scala.Option.orElse(Option.scala:477)
        at com.nawforce.common.cst.ClassDeclaration$.$anonfun$construct$6(TypeDeclarations.scala:119)
        at scala.collection.immutable.List.flatMap(List.scala:293)
        at scala.collection.immutable.List.flatMap(List.scala:79)
        at com.nawforce.common.cst.ClassDeclaration$.construct(TypeDeclarations.scala:114)
        at com.nawforce.common.types.apex.FullDeclaration$.$anonfun$construct$1(FullDeclaration.scala:275)
        at scala.Option.map(Option.scala:242)
        at com.nawforce.common.types.apex.FullDeclaration$.construct(FullDeclaration.scala:268)
        at com.nawforce.common.cst.CompilationUnit$.$anonfun$construct$1(TypeDeclarations.scala:47)
        at scala.util.DynamicVariable.withValue(DynamicVariable.scala:59)
        at com.nawforce.common.cst.CompilationUnit$.construct(TypeDeclarations.scala:48)
        at com.nawforce.common.types.apex.FullDeclaration$.create(FullDeclaration.scala:253)
        at com.nawforce.common.org.PackageDeploy.$anonfun$loadClasses$12(PackageDeploy.scala:146)
        at com.nawforce.common.org.PackageDeploy.$anonfun$loadClasses$11(PackageDeploy.scala:146)
        at scala.collection.immutable.List.flatMap(List.scala:293)
        at scala.collection.immutable.List.flatMap(List.scala:79)
        at com.nawforce.common.org.PackageDeploy.$anonfun$loadClasses$10(PackageDeploy.scala:143)
        at com.nawforce.common.org.PackageDeploy.loadClasses(PackageDeploy.scala:139)
        at com.nawforce.common.org.PackageDeploy.deployFromWorkspace(PackageDeploy.scala:69)
        at com.nawforce.common.org.PackageDeploy.deployFromWorkspace$(PackageDeploy.scala:51)
        at com.nawforce.common.org.PackageImpl.deployFromWorkspace(PackageImpl.scala:48)
        at com.nawforce.common.org.PackageImpl.<init>(PackageImpl.scala:68)
        at com.nawforce.common.org.OrgImpl.$anonfun$addPackage$1(OrgImpl.scala:207)
        at scala.util.DynamicVariable.withValue(DynamicVariable.scala:59)
        at com.nawforce.common.org.OrgImpl.addPackage(OrgImpl.scala:206)
        at com.nawforce.common.org.OrgImpl.newSFDXPackageInternal(OrgImpl.scala:158)
        at com.nawforce.common.org.OrgImpl.newSFDXPackage(OrgImpl.scala:148)
        at net.sourceforge.pmd.lang.apex.multifile.ApexMultifileAnalysis.<init>(ApexMultifileAnalysis.java:68)
        at net.sourceforge.pmd.lang.apex.ApexLanguageProcessor.<init>(ApexLanguageProcessor.java:21)
        at net.sourceforge.pmd.lang.apex.ApexLanguageModule.createProcessor(ApexLanguageModule.java:43)
        at net.sourceforge.pmd.lang.LanguageProcessorRegistry.create(LanguageProcessorRegistry.java:131)
        at net.sourceforge.pmd.PmdAnalysis.performAnalysisImpl(PmdAnalysis.java:352)
        at net.sourceforge.pmd.PmdAnalysis.performAnalysisImpl(PmdAnalysis.java:317)
        at net.sourceforge.pmd.PmdAnalysis.performAnalysis(PmdAnalysis.java:297)
        at net.sourceforge.pmd.PmdAnalysis.runAndReturnStats(PmdAnalysis.java:515)
        at net.sourceforge.pmd.cli.commands.internal.PmdCommand.execute(PmdCommand.java:354)
        at net.sourceforge.pmd.cli.commands.internal.AbstractPmdSubcommand.call(AbstractPmdSubcommand.java:35)
        at net.sourceforge.pmd.cli.commands.internal.AbstractPmdSubcommand.call(AbstractPmdSubcommand.java:20)
        at picocli.CommandLine.executeUserObject(CommandLine.java:2041)
        at picocli.CommandLine.access$1500(CommandLine.java:148)
        at picocli.CommandLine$RunLast.executeUserObjectOfLastSubcommandWithSameParent(CommandLine.java:2461)
        at picocli.CommandLine$RunLast.handle(CommandLine.java:2453)
        at picocli.CommandLine$RunLast.handle(CommandLine.java:2415)
        at picocli.CommandLine$AbstractParseResultHandler.execute(CommandLine.java:2273)
        at picocli.CommandLine$RunLast.execute(CommandLine.java:2417)
        at picocli.CommandLine.execute(CommandLine.java:2170)
        at net.sourceforge.pmd.cli.PmdCli.main(PmdCli.java:18)
[main] ERROR net.sourceforge.pmd.lang.apex.multifile.ApexMultifileAnalysis - PMD will not attempt to initialize Apexlink further, this can cause rules like UnusedMethod to be dysfunctional

Code Sample demonstrating the issue:

Steps to reproduce:

Please provide detailed steps for how we can reproduce the bug.

  1. ... (e.g. if you're using maven: mvn clean verify)
  2. ... pmd check -dir force-app\main\default\classes -R category/apex/design.xml/UnusedMethod -f text -v

Running PMD through: [CLI]

openjdk 17.0.6 2023-01-17
OpenJDK Runtime Environment Temurin-17.0.6+10 (build 17.0.6+10)
OpenJDK 64-Bit Server VM Temurin-17.0.6+10 (build 17.0.6+10, mixed mode, sharing)

@nwcm nwcm added the a:bug PMD crashes or fails to analyse a file. label Apr 4, 2023
@nwcm nwcm changed the title [apex] Exception while initializing Apexlink (Index 34812 out of bounds for length 34812) [apex] [7.0-rc1] Exception while initializing Apexlink (Index 34812 out of bounds for length 34812) Apr 4, 2023
@Arkin69
Copy link

Arkin69 commented Jun 13, 2023

same issue for me with 7.0.0-rc3

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.reflection.CachedClass (file:/C:/pmd-bin-7.0.0-rc3/lib/groovy-2.4.21.jar) to method java.lang.Object.finalize()
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.reflection.CachedClass
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
[main] INFO net.sourceforge.pmd.cli.commands.internal.AbstractPmdSubcommand - Log level is at INFO
[main] WARN net.sourceforge.pmd.cli.commands.internal.PmdCommand - This analysis could be faster, please consider using Incremental Analysis: https://docs.pmd-code.org/pmd-doc-7.0.0-rc3/pmd_userdocs_incremental_analysis.html
ANTLR Tool version 4.8 used for code generation does not match the current runtime version 4.9.1
ANTLR Runtime version 4.8 used for parser compilation does not match the current runtime version 4.9.1
ANTLR Tool version 4.8 used for code generation does not match the current runtime version 4.9.1
ANTLR Runtime version 4.8 used for parser compilation does not match the current runtime version 4.9.1
[main] ERROR net.sourceforge.pmd.lang.apex.multifile.ApexMultifileAnalysis - Exception while initializing Apexlink (Index 5321 out of bounds for length 5321)
java.lang.ArrayIndexOutOfBoundsException: Index 5321 out of bounds for length 5321
        at com.nawforce.common.parsers.UTF8Decode$.getCharOffsetFrom(UTF8Decode.scala:38)
        at com.nawforce.runtime.parsers.ByteArraySourceData.subdata(SourceData.scala:66)
        at com.nawforce.runtime.parsers.ByteArraySourceData.subdata(SourceData.scala:61)
        at com.nawforce.runtime.parsers.Source.extractSource(Source.scala:57)
        at com.nawforce.runtime.parsers.CodeParser.extractSource(CodeParser.scala:76)
        at com.nawforce.common.cst.Block$.constructLazy(Statements.scala:108)
        at com.nawforce.common.cst.ApexMethodDeclaration$.$anonfun$construct$17(BodyDeclarations.scala:273)
        at scala.Option.map(Option.scala:242)
        at com.nawforce.common.cst.ApexMethodDeclaration$.construct(BodyDeclarations.scala:273)
        at com.nawforce.common.cst.ClassBodyDeclaration$.$anonfun$construct$1(BodyDeclarations.scala:106)
        at scala.Option.map(Option.scala:242)
        at com.nawforce.common.cst.ClassBodyDeclaration$.construct(BodyDeclarations.scala:99)
        at com.nawforce.common.cst.ClassDeclaration$.$anonfun$construct$9(TypeDeclarations.scala:120)
        at scala.Option.map(Option.scala:242)
        at com.nawforce.common.cst.ClassDeclaration$.$anonfun$construct$8(TypeDeclarations.scala:119)
        at scala.Option.orElse(Option.scala:477)
        at com.nawforce.common.cst.ClassDeclaration$.$anonfun$construct$6(TypeDeclarations.scala:119)
        at scala.collection.immutable.List.flatMap(List.scala:293)
        at scala.collection.immutable.List.flatMap(List.scala:79)
        at com.nawforce.common.cst.ClassDeclaration$.construct(TypeDeclarations.scala:114)
        at com.nawforce.common.types.apex.FullDeclaration$.$anonfun$construct$1(FullDeclaration.scala:275)
        at scala.Option.map(Option.scala:242)
        at com.nawforce.common.types.apex.FullDeclaration$.construct(FullDeclaration.scala:268)
        at com.nawforce.common.cst.CompilationUnit$.$anonfun$construct$1(TypeDeclarations.scala:47)
        at scala.util.DynamicVariable.withValue(DynamicVariable.scala:59)
        at com.nawforce.common.cst.CompilationUnit$.construct(TypeDeclarations.scala:48)
        at com.nawforce.common.types.apex.FullDeclaration$.create(FullDeclaration.scala:253)
        at com.nawforce.common.org.PackageDeploy.$anonfun$loadClasses$12(PackageDeploy.scala:146)
        at com.nawforce.common.org.PackageDeploy.$anonfun$loadClasses$11(PackageDeploy.scala:146)
        at scala.collection.immutable.List.flatMap(List.scala:293)
        at scala.collection.immutable.List.flatMap(List.scala:79)
        at com.nawforce.common.org.PackageDeploy.$anonfun$loadClasses$10(PackageDeploy.scala:143)
        at com.nawforce.common.org.PackageDeploy.loadClasses(PackageDeploy.scala:139)
        at com.nawforce.common.org.PackageDeploy.deployFromWorkspace(PackageDeploy.scala:69)
        at com.nawforce.common.org.PackageDeploy.deployFromWorkspace$(PackageDeploy.scala:51)
        at com.nawforce.common.org.PackageImpl.deployFromWorkspace(PackageImpl.scala:48)
        at com.nawforce.common.org.PackageImpl.<init>(PackageImpl.scala:68)
        at com.nawforce.common.org.OrgImpl.$anonfun$addPackage$1(OrgImpl.scala:207)
        at scala.util.DynamicVariable.withValue(DynamicVariable.scala:59)
        at com.nawforce.common.org.OrgImpl.addPackage(OrgImpl.scala:206)
        at com.nawforce.common.org.OrgImpl.newSFDXPackageInternal(OrgImpl.scala:158)
        at com.nawforce.common.org.OrgImpl.newSFDXPackage(OrgImpl.scala:148)
        at net.sourceforge.pmd.lang.apex.multifile.ApexMultifileAnalysis.<init>(ApexMultifileAnalysis.java:68)
        at net.sourceforge.pmd.lang.apex.ApexLanguageProcessor.<init>(ApexLanguageProcessor.java:21)
        at net.sourceforge.pmd.lang.apex.ApexLanguageModule.createProcessor(ApexLanguageModule.java:43)
        at net.sourceforge.pmd.lang.LanguageProcessorRegistry.create(LanguageProcessorRegistry.java:131)
        at net.sourceforge.pmd.PmdAnalysis.performAnalysisImpl(PmdAnalysis.java:410)
        at net.sourceforge.pmd.PmdAnalysis.performAnalysisImpl(PmdAnalysis.java:370)
        at net.sourceforge.pmd.PmdAnalysis.performAnalysis(PmdAnalysis.java:350)
        at net.sourceforge.pmd.PmdAnalysis.runAndReturnStats(PmdAnalysis.java:573)
        at net.sourceforge.pmd.cli.commands.internal.PmdCommand.execute(PmdCommand.java:355)
        at net.sourceforge.pmd.cli.commands.internal.AbstractPmdSubcommand.call(AbstractPmdSubcommand.java:35)
        at net.sourceforge.pmd.cli.commands.internal.AbstractPmdSubcommand.call(AbstractPmdSubcommand.java:20)
        at picocli.CommandLine.executeUserObject(CommandLine.java:2041)
        at picocli.CommandLine.access$1500(CommandLine.java:148)
        at picocli.CommandLine$RunLast.executeUserObjectOfLastSubcommandWithSameParent(CommandLine.java:2461)
        at picocli.CommandLine$RunLast.handle(CommandLine.java:2453)
        at picocli.CommandLine$RunLast.handle(CommandLine.java:2415)
        at picocli.CommandLine$AbstractParseResultHandler.execute(CommandLine.java:2273)
        at picocli.CommandLine$RunLast.execute(CommandLine.java:2417)
        at picocli.CommandLine.execute(CommandLine.java:2170)
        at net.sourceforge.pmd.cli.PmdCli.main(PmdCli.java:18)
[main] ERROR net.sourceforge.pmd.lang.apex.multifile.ApexMultifileAnalysis - PMD will not attempt to initialize Apexlink further, this can cause rules like UnusedMethod to be dysfunctional

@oowekyala
Copy link
Member

@nawforce can you take a look at this?

@adangel
Copy link
Member

adangel commented Jun 13, 2023

Note that @nawforce already created #4528 to update apexlink, which may or may not solve this issue.

@Arkin69
Copy link

Arkin69 commented Jun 15, 2023

@adangel @nawforce cool but what are we waiting for validating this PR ? :)

@nawforce
Copy link
Contributor

Sorry for delay on this. The issue I think is the encoding on Windows is not UTF-8, you could try using -Dfile.encoding=UTF-8 as a temporary hack. To really get rid of this we will need the update that is pending on #4528.

@adangel
Copy link
Member

adangel commented Jun 26, 2023

I don't think #4528 will solve this issue - apexlink currently expects that the apex source code in encoded in UTF-8. Until this is solved, you have two options: a) Use UTF-8 for all your source code, b) disable the apex rule "UnusedMethod".

I figured out, that we don't have a single unit test for the rule UnusedMethod - that means I can't proof anything, but looking at https://github.com/nawforce/apex-link/blob/7688adcb7a2d7f8aa28d0618ffb2a3aa81151858/pkgforce/jvm/src/main/scala/com/nawforce/runtime/parsers/SourceData.scala#L36-L49 I think, apexlink still expects UTF-8. It might change however with "apex-dev-tools".

For Java (or in general), we have the (cli) option --encoding, which declares the encoding to be used for reading the source files. That would need to be forwarded to apexlink, since apexlink reads the sources on its own.
Maybe the encoding is a property in these sfdx json files? I wonder how apex tooling in general figures out what encoding is used...

adangel added a commit to adangel/pmd that referenced this issue Jun 29, 2023
adangel added a commit to adangel/pmd that referenced this issue Jun 29, 2023
adangel added a commit to adangel/pmd that referenced this issue Jun 29, 2023
@adangel adangel added this to the 7.0.0 milestone Jun 29, 2023
@adangel
Copy link
Member

adangel commented Jun 29, 2023

I was able to reproduce the issue now - The conditions are:

  • platform encoding is not UTF-8, e.g. ISO-8859-15
  • apex code is in UTF-8

Therefore, the workaround is indeed, to force the JVM's default charset to be UTF-8, e.g. when using the PMD CLI, you can set the env variable PMD_JAVA_OPTS="-Dfile.encoding=UTF-8".

I could verify, that this problem is indeed fixed by #4528 . The new version of apexlink now always uses UTF-8 (also for the antlr charstream, see https://github.com/nawforce/apex-link/blob/7688adcb7a2d7f8aa28d0618ffb2a3aa81151858/pkgforce/jvm/src/main/scala/com/nawforce/runtime/parsers/SourceData.scala#L55-L59 ) and doesn't depend anymore on the platform encoding.

FYI: This issue will be closed once #4610 is merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:bug PMD crashes or fails to analyse a file.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants