Skip to content

Commit

Permalink
Have Refaster resolve classes against the appropriate modules
Browse files Browse the repository at this point in the history
Looking up classes only in java.base seems to work fine on JDK 8, but fails on
JDK 9+. This causes Refaster templates that reference non-JDK types to silently
not match in places where they should. By resolving classes against the
appropriate modules the problem goes away.

Prior to this change the newly added test would pass when executed on JDK 8,

Fixes #1239

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=303219121
  • Loading branch information
Stephan202 authored and nick-someone committed Mar 27, 2020
1 parent 76852fc commit cb344d8
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ public ClassSymbol resolveClass(CharSequence qualifiedClass)
throws CouldNotResolveImportException {
try {
Symbol symbol =
JavaCompiler.instance(context)
.resolveIdent(symtab().java_base, qualifiedClass.toString());
JavaCompiler.instance(context).resolveBinaryNameOrIdent(qualifiedClass.toString());
if (symbol.equals(symtab().errSymbol) || !(symbol instanceof ClassSymbol)) {
throw new CouldNotResolveImportException(qualifiedClass);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@

import com.google.common.base.Joiner;
import com.sun.tools.javac.file.JavacFileManager;
import com.sun.tools.javac.main.JavaCompiler;
import com.sun.tools.javac.parser.Parser;
import com.sun.tools.javac.parser.ParserFactory;
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.tree.JCTree.JCIdent;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.List;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Before;
Expand All @@ -44,6 +46,8 @@ public abstract class AbstractUTreeTest {
public void createContext() {
context = new Context();
JavacFileManager.preRegister(context);
JavaCompiler compiler = JavaCompiler.instance(context);
compiler.initModules(List.nil());
unifier = new Unifier(context);
inliner = unifier.createInliner();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,9 @@ public void placeholderAllowedVars() throws IOException {
public void unnecessaryLambdaParens() throws IOException {
runTest("UnnecessaryLambdaParens");
}

@Test
public void nonJdkType() throws IOException {
runTest("NonJdkTypeTemplate");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2020 The Error Prone Authors.
*
* 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.google.errorprone.refaster.testdata;

import com.google.common.collect.ImmutableList;
import java.util.stream.Stream;

/** Test data for {@code NonJdkTypeTemplate}. */
public class NonJdkTypeTemplateExample {
ImmutableList<Integer> example() {
return ImmutableList.copyOf(Stream.of(1).iterator());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2020 The Error Prone Authors.
*
* 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.google.errorprone.refaster.testdata;

import static com.google.common.collect.ImmutableList.toImmutableList;

import com.google.common.collect.ImmutableList;
import java.util.stream.Stream;

/** Test data for {@code NonJdkTypeTemplate}. */
public class NonJdkTypeTemplateExample {
ImmutableList<Integer> example() {
return Stream.of(1).collect(toImmutableList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2020 The Error Prone Authors.
*
* 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.google.errorprone.refaster.testdata.template;

import static com.google.common.collect.ImmutableList.toImmutableList;

import com.google.common.collect.ImmutableList;
import com.google.errorprone.refaster.ImportPolicy;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.UseImportPolicy;
import java.util.stream.Stream;

/** Example template that uses a non-JDK type. */
public class NonJdkTypeTemplate<T> {
@BeforeTemplate
public ImmutableList<T> before(Stream<T> stream) {
return ImmutableList.copyOf(stream.iterator());
}

@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
public ImmutableList<T> after(Stream<T> stream) {
return stream.collect(toImmutableList());
}
}

0 comments on commit cb344d8

Please sign in to comment.