Skip to content

Commit

Permalink
SourceSet now takes a SourceInput
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Sep 27, 2016
1 parent b095597 commit 45d4102
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Copyright 2015-2016 Netflix, Inc.
#
# 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.
#

implementation-class=com.netflix.java.refactor.gradle.RefactorSourcePlugin
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ abstract class AbstractRefactorTest {
@JvmField @Rule
val temp = TemporaryFolder()

fun parseJava(target: File, vararg otherFiles: File): JavaSource =
fun parseJava(target: File, vararg otherFiles: File): JavaSource<*> =
parseJava(target, otherFiles.toList(), null)

fun parseJava(target: File, otherFiles: Iterable<File>, classpath: Iterable<File>? = null): JavaSource {
fun parseJava(target: File, otherFiles: Iterable<File>, classpath: Iterable<File>? = null): JavaSource<*> {
val parser = AstParser(classpath?.map { it.toPath() })
val allFiles = otherFiles.plus(target)
val cu = parser.parseFiles(allFiles.map { it.toPath() }).last()
return JavaSource(CompilationUnit(cu, parser))
return JavaSource(CompilationUnit(cu, parser), Unit)
}

fun java(sourceStr: String): File {
Expand Down
6 changes: 2 additions & 4 deletions src/main/kotlin/com/netflix/java/refactor/JavaSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import com.netflix.java.refactor.find.Annotation
import com.sun.tools.javac.code.Symbol
import com.sun.tools.javac.tree.JCTree
import java.nio.file.Files
import java.util.function.Consumer
import kotlin.concurrent.thread

class JavaSource(val cu: CompilationUnit) {
class JavaSource<P>(val cu: CompilationUnit, val datum: P) {
var changedFile = false

fun file() = cu.source()
Expand Down Expand Up @@ -55,7 +53,7 @@ class JavaSource(val cu: CompilationUnit) {

fun findMethodCalls(signature: String): List<Method> = FindMethods(signature).scanner().scan(cu)

fun diff(body: JavaSource.() -> Unit): String {
fun diff(body: JavaSource<*>.() -> Unit): String {
val before = text()
this.body()
val after = text()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.netflix.java.refactor

class JavaSourceDiff(private val source: JavaSource) {
class JavaSourceDiff(private val source: JavaSource<*>) {
private val before = source.text()

fun gitStylePatch() = InMemoryDiffEntry(source.file().toString(), before, source.text()).diff
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.netflix.java.refactor

interface JavaSourceScanner<out T> {
fun scan(source: JavaSource): T
interface JavaSourceScanner<P, out R> {
fun scan(source: JavaSource<P>): R
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.nio.file.Files
import java.nio.file.StandardOpenOption
import java.util.*

class RefactorTransaction(val refactorer: JavaSource) {
class RefactorTransaction(val refactorer: JavaSource<*>) {
private val ops = ArrayList<RefactoringAstScannerBuilder>()

fun changeType(from: String, to: String): RefactorTransaction {
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/com/netflix/java/refactor/SourceInput.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.netflix.java.refactor

import java.nio.file.Path

data class SourceInput<out D>(val path: Path, val datum: D)
34 changes: 21 additions & 13 deletions src/main/kotlin/com/netflix/java/refactor/SourceSet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,34 @@ import java.net.URLClassLoader
import java.nio.file.Path
import java.util.*

data class SourceSet(val allSourceFiles: Iterable<Path>, val classpath: Iterable<Path>) {
data class SourceSet<P>(val allSourceInputs: Iterable<SourceInput<P>>, val classpath: Iterable<Path>) {

val logger: Logger = LoggerFactory.getLogger(SourceSet::class.java)
val filteredClasspath = classpath.filter {
val fn = it.fileName.toString()
fn.endsWith(".jar") && !fn.endsWith("-javadoc.jar") && !fn.endsWith("-sources.jar")
}

private val parser = AstParser(filteredClasspath)
private val compilationUnits by lazy {
parser.parseFiles(allSourceFiles.filter { it.fileName.toString().endsWith(".java") }.toList()).map { CompilationUnit(it, parser) }

private val compilationUnits: List<JavaSource<P>> by lazy {
val javaFiles = allSourceInputs.filter { it.path.fileName.toString().endsWith(".java") }
val parsedFiles = parser.parseFiles(javaFiles.map { it.path })

parsedFiles.zip(javaFiles.map { it.datum }).map {
val (parsed, datum) = it
JavaSource(CompilationUnit(parsed, parser), datum)
}
}

fun allJava() = compilationUnits.map { cu -> JavaSource(cu) }
fun allJava(): List<JavaSource<P>> = compilationUnits

/**
* Find all classes annotated with @AutoRefactor on the classpath that implement JavaSourceScanner.
* Does not work on virtual file systems at this time.
*/
fun allAutoRefactorsOnClasspath(): Map<AutoRefactor, JavaSourceScanner<*>> {
val scanners = HashMap<AutoRefactor, JavaSourceScanner<*>>()
fun allAutoRefactorsOnClasspath(): Map<AutoRefactor, JavaSourceScanner<Any, *>> {
val scanners = HashMap<AutoRefactor, JavaSourceScanner<Any, *>>()
val classLoader = URLClassLoader(filteredClasspath.map { it.toFile().toURI().toURL() }.toTypedArray(), javaClass.classLoader)

val reporter = object: AnnotationDetector.TypeReporter {
Expand All @@ -39,11 +47,11 @@ data class SourceSet(val allSourceFiles: Iterable<Path>, val classpath: Iterable

try {
val scanner = clazz.newInstance()
if(scanner is JavaSourceScanner<*>) {
scanners.put(refactor, scanner)
if(scanner is JavaSourceScanner<*, *>) {
scanners.put(refactor, scanner as JavaSourceScanner<Any, *>)
}
else {
logger.warn("To be useable, an @AutoRefactor must implement JavaSourceScanner or extend JavaSourceVisitor")
logger.warn("To be useable, an @AutoRefactor must implement JavaSourceScanner")
}
} catch(ignored: ReflectiveOperationException) {
logger.warn("Unable to construct @AutoRefactor with id '${refactor.value}'. It must extend implement JavaSourceScanner or extend JavaSourceVisitor and have a zero argument public constructor.")
Expand All @@ -55,11 +63,11 @@ data class SourceSet(val allSourceFiles: Iterable<Path>, val classpath: Iterable
return scanners
}

fun <T> scan(scanner: JavaSourceScanner<T>): List<T> = allJava().map { scanner.scan(it) }.filter { it != null }
fun <R> scan(scanner: JavaSourceScanner<P, R>): List<R> = allJava().map { scanner.scan(it) }.filter { it != null }

fun scanForClasses(predicate: (JavaSource) -> Boolean): List<String> {
return scan(object : JavaSourceScanner<List<String>> {
override fun scan(source: JavaSource): List<String> {
fun scanForClasses(predicate: (JavaSource<P>) -> Boolean): List<String> {
return scan(object : JavaSourceScanner<P, List<String>> {
override fun scan(source: JavaSource<P>): List<String> {
return if (predicate.invoke(source))
source.classes()
else emptyList<String>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.netflix.java.refactor.gradle

import com.netflix.java.refactor.SourceInput
import com.netflix.java.refactor.SourceSet
import org.gradle.api.DefaultTask
import org.gradle.api.plugins.JavaPluginConvention
Expand All @@ -21,7 +22,7 @@ open class RefactorAndFixSourceTask : DefaultTask() {
val fixesByRule = hashMapOf<RuleDescriptor, MutableSet<Path>>()

project.convention.getPlugin(JavaPluginConvention::class.java).sourceSets.forEach {
val sourceSet = SourceSet(it.allJava.map { it.toPath() }, it.compileClasspath.map { it.toPath() })
val sourceSet = SourceSet(it.allJava.map { SourceInput<Any>(it.toPath(), Unit) }, it.compileClasspath.map { it.toPath() })
sourceSet.allAutoRefactorsOnClasspath().forEach {
val (refactor, scanner) = it
sourceSet.allJava().forEach { source ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
package com.netflix.java.refactor.ast

import com.netflix.java.refactor.AbstractRefactorTest
import org.junit.Assert.*
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Ignore
import org.junit.Test

@Ignore
class AnnotationMatcherTest : AbstractRefactorTest() {
@Test
fun matchesSimpleFullyQualifiedAnnotation() {
assertFalse(
parseJava(
java(
"""
@Deprecated
public class A {}
"""
)
)
val a = java("""
@Deprecated
public class A {}
""")

assertTrue(parseJava(a)
.findAnnotations("@java.lang.Deprecated")
.isEmpty()

)
.isNotEmpty())
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.netflix.java.refactor.ast

import com.github.marschall.memoryfilesystem.MemoryFileSystemBuilder
import com.netflix.java.refactor.AbstractRefactorTest
import com.netflix.java.refactor.SourceInput
import com.netflix.java.refactor.SourceSet
import org.junit.Ignore
import org.junit.Test
Expand Down Expand Up @@ -88,9 +89,10 @@ class AstParserTest : AbstractRefactorTest() {
Files.createDirectories(path.parent)
if(!entry.isDirectory)
Files.copy(zin, path)
entry = zin.nextEntry
}

val classes = SourceSet(listOf(a), listOf(fs.getPath("testng-6.9.9"))).scanForClasses { it.hasType("org.testng.annotations.Test") }
val classes = SourceSet(listOf(SourceInput(a, Unit)), listOf(fs.getPath("testng-6.9.9"))).scanForClasses { it.hasType("org.testng.annotations.Test") }
assertEquals(listOf("a.A"), classes)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package com.netflix.java.refactor.find

import com.netflix.java.refactor.AbstractRefactorTest
import org.junit.Assert.assertEquals
import org.junit.Ignore
import org.junit.Test
import java.lang.Deprecated

@Ignore
class FindAnnotationsTest: AbstractRefactorTest() {

@Test
Expand Down

0 comments on commit 45d4102

Please sign in to comment.