Skip to content

Commit

Permalink
Add a language level option for parsing (#232)
Browse files Browse the repository at this point in the history
Needed to work around javaparser/javaparser#4041.  Users impacted by that issue can set the language level to 11, assuming they are not using language constructs introduced after that version.
  • Loading branch information
msridhar authored May 15, 2024
1 parent 2229914 commit c2359ee
Show file tree
Hide file tree
Showing 16 changed files with 109 additions and 34 deletions.
36 changes: 36 additions & 0 deletions annotator-core/src/main/java/edu/ucr/cs/riple/core/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package edu.ucr.cs.riple.core;

import com.github.javaparser.ParserConfiguration;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
Expand Down Expand Up @@ -152,6 +153,9 @@ public class Config {
*/
public final String checkerName;

/** Language level to use when parsing Java code. Defaults to Java 17. */
public final ParserConfiguration.LanguageLevel languageLevel;

/**
* Builds context from command line arguments.
*
Expand Down Expand Up @@ -347,6 +351,16 @@ public Config(String[] args) {
nonnullAnnotationsOption.setValueSeparator(',');
options.addOption(nonnullAnnotationsOption);

// Language level to use when parsing
Option languageLevelOption =
new Option(
"ll",
"language-level",
true,
"Java language level to use when parsing code. Supported values are 11 and 17. Defaults to 17.");
languageLevelOption.setRequired(false);
options.addOption(languageLevelOption);

HelpFormatter formatter = new HelpFormatter();
CommandLineParser parser = new DefaultParser();
CommandLine cmd;
Expand Down Expand Up @@ -448,12 +462,30 @@ public Config(String[] args) {
cmd.hasOption(disableRegionDetectionByLombok)
? ImmutableSet.of()
: Sets.immutableEnumSet(SourceType.LOMBOK);
this.languageLevel = getLanguageLevel(cmd.getOptionValue(languageLevelOption, "17"));
this.nonnullAnnotations =
!cmd.hasOption(nonnullAnnotationsOption)
? ImmutableSet.of()
: ImmutableSet.copyOf(cmd.getOptionValue(nonnullAnnotationsOption).split(","));
}

/**
* Gets the language level from the string representation. "11" for Java 11 and "17" for Java 17.
*
* @param languageLevelString string representation of the language level.
* @return the language level.
*/
private ParserConfiguration.LanguageLevel getLanguageLevel(String languageLevelString) {
switch (languageLevelString) {
case "11":
return ParserConfiguration.LanguageLevel.JAVA_11;
case "17":
return ParserConfiguration.LanguageLevel.JAVA_17;
default:
throw new IllegalArgumentException("Unsupported language level: " + languageLevelString);
}
}

/**
* Builds context from json context file.
*
Expand Down Expand Up @@ -540,6 +572,8 @@ public Config(Path configPath) {
.orElse(true);
this.generatedCodeDetectors =
lombokCodeDetectorActivated ? Sets.immutableEnumSet(SourceType.LOMBOK) : ImmutableSet.of();
this.languageLevel =
getLanguageLevel(getValueFromKey(jsonObject, "LANGUAGE_LEVEL", String.class).orElse("17"));
this.nonnullAnnotations =
ImmutableSet.copyOf(
getArrayValueFromKey(
Expand Down Expand Up @@ -675,6 +709,7 @@ public static class Builder {
public Set<SourceType> sourceTypes = new HashSet<>();
public int depth = 1;
public String checker;
public ParserConfiguration.LanguageLevel languageLevel;

@SuppressWarnings("unchecked")
public void write(Path path) {
Expand Down Expand Up @@ -707,6 +742,7 @@ public void write(Path path) {
json.put("REDIRECT_BUILD_OUTPUT_TO_STDERR", redirectBuildOutputToStdErr);
json.put("SUPPRESS_REMAINING_ERRORS", suppressRemainingErrors);
json.put("INFERENCE_ACTIVATION", inferenceActivated);
json.put("LANGUAGE_LEVEL", languageLevel.name().split("_")[1]);
JSONArray configPathsJson = new JSONArray();
configPathsJson.addAll(
configPaths.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ private ImmutableSet<Location> retrieveLocationsToCacheImpactsOnDownstreamDepend
Context context, ModuleInfo moduleInfo) {
ImmutableSet.Builder<Location> locationsToCache = ImmutableSet.builder();
// Used to collect callers of each method.
MethodRegionRegistry methodRegionRegistry = new MethodRegionRegistry(moduleInfo);
FieldRegionRegistry fieldRegionRegistry = new FieldRegionRegistry(moduleInfo);
MethodRegionRegistry methodRegionRegistry = new MethodRegionRegistry(moduleInfo, context);
FieldRegionRegistry fieldRegionRegistry = new FieldRegionRegistry(moduleInfo, context);
// Collect public methods with non-primitive return types.
locationsToCache.addAll(
context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class PhysicalInjector extends AnnotationInjector {
*/
public PhysicalInjector(Context context) {
super(context);
this.injector = new Injector();
this.injector = new Injector(context.config.languageLevel);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ public ModuleInfo(
// Build with scanner checker activated to generate required files to create the moduleInfo.
context.checker.prepareConfigFilesForBuild(configurations);
Utility.runScannerChecker(context, configurations, buildCommand);
this.nonnullStore = new NonnullStore(configurations);
this.fieldRegistry = new FieldRegistry(configurations);
this.nonnullStore = new NonnullStore(configurations, context);
this.fieldRegistry = new FieldRegistry(configurations, context);
this.methodRegistry = new MethodRegistry(context);
this.regionRegistry = new CompoundRegionRegistry(this);
this.regionRegistry = new CompoundRegionRegistry(this, context);
ImmutableSet.Builder<AnnotationProcessorHandler> builder = new ImmutableSet.Builder<>();
if (context.config.generatedCodeDetectors.contains(SourceType.LOMBOK)) {
builder.add(new LombokHandler(this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import edu.ucr.cs.riple.core.Context;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -54,14 +55,17 @@ public abstract class Registry<T> {
*/
protected final ImmutableMultimap<Integer, T> contents;

protected final Context context;

/**
* Constructor for this container. Once this constructor is invoked, all data will be loaded from
* the file.
*
* @param path Path to the file containing the data.
*/
public Registry(Path path) {
public Registry(Path path, Context context) {
ImmutableMultimap.Builder<Integer, T> builder = ImmutableMultimap.builder();
this.context = context;
setup();
try {
populateContent(path, builder);
Expand All @@ -77,8 +81,9 @@ public Registry(Path path) {
*
* @param paths Paths to all files containing data.
*/
public Registry(ImmutableSet<Path> paths) {
public Registry(ImmutableSet<Path> paths, Context context) {
ImmutableMultimap.Builder<Integer, T> builder = ImmutableMultimap.builder();
this.context = context;
setup();
paths.forEach(
path -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class FieldInitializationStore extends Registry<FieldInitializationNode>
* @param context Annotator context.
*/
public FieldInitializationStore(Context context) {
super(context.targetConfiguration.dir.resolve(FILE_NAME));
super(context.targetConfiguration.dir.resolve(FILE_NAME), context);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.google.common.collect.Multimap;
import com.google.common.collect.MultimapBuilder;
import com.google.common.collect.Sets;
import edu.ucr.cs.riple.core.Context;
import edu.ucr.cs.riple.core.module.ModuleConfiguration;
import edu.ucr.cs.riple.core.registries.Registry;
import edu.ucr.cs.riple.injector.Helper;
Expand Down Expand Up @@ -63,26 +64,26 @@ public class FieldRegistry extends Registry<ClassFieldRecord> {
* initialized at declaration.
*/
private Multimap<String, String> uninitializedFields;

/**
* Constructor for {@link FieldRegistry}.
*
* @param module Information of the target module.
*/
public FieldRegistry(ModuleConfiguration module) {
this(ImmutableSet.of(module));
public FieldRegistry(ModuleConfiguration module, Context context) {
this(ImmutableSet.of(module), context);
}

/**
* Constructor for {@link FieldRegistry}. Contents are accumulated from multiple sources.
*
* @param modules Information of set of modules.
*/
public FieldRegistry(ImmutableSet<ModuleConfiguration> modules) {
public FieldRegistry(ImmutableSet<ModuleConfiguration> modules, Context context) {
super(
modules.stream()
.map(info -> info.dir.resolve(Serializer.CLASS_RECORD_FILE_NAME))
.collect(ImmutableSet.toImmutableSet()));
.collect(ImmutableSet.toImmutableSet()),
context);
}

@Override
Expand Down Expand Up @@ -111,7 +112,7 @@ public ClassFieldRecord build(String[] values) {
tree = lastParsedSourceFile.b;
} else {
// Not visited yet, parse the source file.
tree = Injector.parse(path);
tree = Injector.parse(path, context.config.languageLevel);
lastParsedSourceFile = new Pair<>(path, tree);
}
if (tree == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package edu.ucr.cs.riple.core.registries.index;

import com.google.common.collect.ImmutableSet;
import edu.ucr.cs.riple.core.Context;
import edu.ucr.cs.riple.core.module.ModuleConfiguration;
import edu.ucr.cs.riple.core.registries.Registry;
import edu.ucr.cs.riple.injector.location.Location;
Expand All @@ -37,11 +38,12 @@
*/
public class NonnullStore extends Registry<Location> {

public NonnullStore(ImmutableSet<ModuleConfiguration> modules) {
public NonnullStore(ImmutableSet<ModuleConfiguration> modules, Context context) {
super(
modules.stream()
.map(moduleInfo -> moduleInfo.dir.resolve(Serializer.NON_NULL_ELEMENTS_FILE_NAME))
.collect(ImmutableSet.toImmutableSet()));
.collect(ImmutableSet.toImmutableSet()),
context);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ public class MethodRegistry extends Registry<MethodRecord> {
private Set<String> declaredClasses;

public MethodRegistry(Context context) {
this(ImmutableSet.of(context.targetConfiguration));
this(ImmutableSet.of(context.targetConfiguration), context);
}

public MethodRegistry(ImmutableSet<ModuleConfiguration> modules) {
public MethodRegistry(ImmutableSet<ModuleConfiguration> modules, Context context) {
super(
modules.stream()
.map(moduleInfo -> moduleInfo.dir.resolve(Serializer.METHOD_RECORD_FILE_NAME))
.collect(ImmutableSet.toImmutableSet()));
.collect(ImmutableSet.toImmutableSet()),
context);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package edu.ucr.cs.riple.core.registries.region;

import com.google.common.collect.ImmutableSet;
import edu.ucr.cs.riple.core.Context;
import edu.ucr.cs.riple.core.module.ModuleInfo;
import edu.ucr.cs.riple.core.registries.region.generatedcode.AnnotationProcessorHandler;
import edu.ucr.cs.riple.injector.location.Location;
Expand All @@ -46,12 +47,12 @@ public class CompoundRegionRegistry implements RegionRegistry {
*/
private final MethodRegionRegistry methodRegionRegistry;

public CompoundRegionRegistry(ModuleInfo moduleInfo) {
public CompoundRegionRegistry(ModuleInfo moduleInfo, Context context) {
this.moduleInfo = moduleInfo;
this.methodRegionRegistry = new MethodRegionRegistry(moduleInfo);
this.methodRegionRegistry = new MethodRegionRegistry(moduleInfo, context);
this.registries =
ImmutableSet.of(
new FieldRegionRegistry(moduleInfo),
new FieldRegionRegistry(moduleInfo, context),
methodRegionRegistry,
new ParameterRegionRegistry(moduleInfo, methodRegionRegistry));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package edu.ucr.cs.riple.core.registries.region;

import com.google.common.collect.ImmutableSet;
import edu.ucr.cs.riple.core.Context;
import edu.ucr.cs.riple.core.module.ModuleInfo;
import edu.ucr.cs.riple.core.registries.Registry;
import edu.ucr.cs.riple.core.util.Utility;
Expand All @@ -42,13 +43,14 @@ public class FieldRegionRegistry extends Registry<RegionRecord> implements Regio
/** ModuleInfo of the module which usages of fields are stored. */
private final ModuleInfo moduleInfo;

public FieldRegionRegistry(ModuleInfo moduleInfo) {
public FieldRegionRegistry(ModuleInfo moduleInfo, Context context) {
super(
moduleInfo.getModuleConfigurations().stream()
.map(
configuration ->
configuration.dir.resolve(Serializer.FIELD_IMPACTED_REGION_FILE_NAME))
.collect(ImmutableSet.toImmutableSet()));
.collect(ImmutableSet.toImmutableSet()),
context);
this.moduleInfo = moduleInfo;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package edu.ucr.cs.riple.core.registries.region;

import com.google.common.collect.ImmutableSet;
import edu.ucr.cs.riple.core.Context;
import edu.ucr.cs.riple.core.module.ModuleInfo;
import edu.ucr.cs.riple.core.registries.Registry;
import edu.ucr.cs.riple.core.registries.method.MethodRecord;
Expand All @@ -42,11 +43,12 @@ public class MethodRegionRegistry extends Registry<RegionRecord> implements Regi
/** ModuleInfo of the module which usage of methods are stored. */
private final ModuleInfo moduleInfo;

public MethodRegionRegistry(ModuleInfo moduleInfo) {
public MethodRegionRegistry(ModuleInfo moduleInfo, Context context) {
super(
moduleInfo.getModuleConfigurations().stream()
.map(info -> info.dir.resolve(Serializer.METHOD_IMPACTED_REGION_FILE_NAME))
.collect(ImmutableSet.toImmutableSet()));
.collect(ImmutableSet.toImmutableSet()),
context);
this.moduleInfo = moduleInfo;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package edu.ucr.cs.riple.core;

import com.github.javaparser.ParserConfiguration;
import edu.ucr.cs.riple.core.tools.TReport;
import edu.ucr.cs.riple.injector.location.OnField;
import edu.ucr.cs.riple.injector.location.OnMethod;
Expand Down Expand Up @@ -56,6 +57,7 @@ public void patternMatchingInJava17Test() {
"}")
.withExpectedReports(
new TReport(new OnField("Main.java", "test.Main", Set.of("f1", "f2", "f3", "f4")), -4))
.withLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_17)
.start();
}

Expand All @@ -74,6 +76,7 @@ public void recordDeclarationTest() {
.withExpectedReports(
new TReport(new OnMethod("A.java", "test.A", "create(java.lang.String)"), -1))
.toDepth(5)
.withLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_17)
.start();
}
}
Loading

0 comments on commit c2359ee

Please sign in to comment.