|
| 1 | +package org.adoptopenjdk.lambda.tutorial.util; |
| 2 | + |
| 3 | +/* |
| 4 | + * #%L |
| 5 | + * lambda-tutorial |
| 6 | + * %% |
| 7 | + * Copyright (C) 2013 Adopt OpenJDK |
| 8 | + * %% |
| 9 | + * This program is free software: you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU General Public License as |
| 11 | + * published by the Free Software Foundation, either version 2 of the |
| 12 | + * License, or (at your option) any later version. |
| 13 | + * |
| 14 | + * This program is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU General Public |
| 20 | + * License along with this program. If not, see |
| 21 | + * <http://www.gnu.org/licenses/gpl-2.0.html>. |
| 22 | + * #L% |
| 23 | + */ |
| 24 | + |
| 25 | +import org.hamcrest.Description; |
| 26 | +import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 27 | +import org.objectweb.asm.ClassReader; |
| 28 | +import org.objectweb.asm.ClassVisitor; |
| 29 | +import org.objectweb.asm.Opcodes; |
| 30 | + |
| 31 | +import java.io.File; |
| 32 | +import java.io.IOException; |
| 33 | +import java.nio.ByteBuffer; |
| 34 | +import java.nio.charset.StandardCharsets; |
| 35 | +import java.nio.file.Files; |
| 36 | +import java.nio.file.Path; |
| 37 | +import java.nio.file.Paths; |
| 38 | +import java.util.Arrays; |
| 39 | +import java.util.Optional; |
| 40 | + |
| 41 | +import static java.util.stream.Collectors.toList; |
| 42 | + |
| 43 | +public final class CodeUsesMethodReferencesMatcher extends TypeSafeDiagnosingMatcher<Class<?>> { |
| 44 | + |
| 45 | + private final String methodName; |
| 46 | + |
| 47 | + private CodeUsesMethodReferencesMatcher(String methodName) { |
| 48 | + this.methodName = methodName; |
| 49 | + } |
| 50 | + |
| 51 | + public static CodeUsesMethodReferencesMatcher usesMethodReferences(String methodName) { |
| 52 | + return new CodeUsesMethodReferencesMatcher(methodName); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void describeTo(Description description) { |
| 57 | + description.appendText("a source file using a method reference to invoke ").appendValue(methodName); |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + @Override |
| 62 | + protected boolean matchesSafely(Class<?> clazz, Description mismatchDescription) { |
| 63 | + try { |
| 64 | + Optional<String> sourceFileContent = getSourceContent(clazz); |
| 65 | + return sourceFileContent.map(c -> usesMethodReference(c, mismatchDescription)).orElseGet(() -> { |
| 66 | + mismatchDescription.appendText("could not read source file to discover if you used method references."); |
| 67 | + return false; |
| 68 | + }); |
| 69 | + } catch (IOException e) { |
| 70 | + mismatchDescription.appendText("could not read source file to discover if you used method references."); |
| 71 | + mismatchDescription.appendValue(e); |
| 72 | + return false; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private boolean usesMethodReference(String sourceCode, Description mismatchDescription) { |
| 77 | + if (sourceCode.contains("::"+methodName)) { |
| 78 | + return true; |
| 79 | + } else { |
| 80 | + mismatchDescription.appendText("source code did not use a method reference to invoke " + methodName + ". "); |
| 81 | + context(sourceCode, methodName, mismatchDescription); |
| 82 | + return false; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private void context(String sourceCode, String methodName, Description mismatchDescription) { |
| 87 | + if (!sourceCode.contains(methodName)) { |
| 88 | + mismatchDescription.appendText("You did not appear to invoke the method at all."); |
| 89 | + } else { |
| 90 | + String[] lines = sourceCode.split("\\n"); |
| 91 | + mismatchDescription.appendText("Actual invocations: "); |
| 92 | + mismatchDescription.appendValueList("[", ",", "]", |
| 93 | + Arrays.stream(lines).filter(l -> l.contains(methodName)).map(String::trim).collect(toList())); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private Optional<String> getSourceContent(Class<?> clazz) throws IOException { |
| 98 | + String sourceFileName = getSourceFileName(clazz); |
| 99 | + Optional<File> sourceFile = findPathTo(sourceFileName); |
| 100 | + |
| 101 | + return sourceFile.map(this::toContent); |
| 102 | + } |
| 103 | + |
| 104 | + private Optional<File> findPathTo(String sourceFileName) throws IOException { |
| 105 | + File cwd = new File("."); |
| 106 | + File rootOfProject = findRootOfProject(cwd); |
| 107 | + return findSourceFile(rootOfProject, sourceFileName); |
| 108 | + } |
| 109 | + |
| 110 | + private String toContent(File file) { |
| 111 | + try { |
| 112 | + byte[] encoded = Files.readAllBytes(Paths.get(file.toURI())); |
| 113 | + return StandardCharsets.UTF_8.decode(ByteBuffer.wrap(encoded)).toString(); |
| 114 | + } catch (IOException e) { |
| 115 | + throw new RuntimeException("Could not read Java source file.", e); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private Optional<File> findSourceFile(File rootOfProject, String sourceFileName) throws IOException { |
| 120 | + Path startingDir = Paths.get(rootOfProject.toURI()); |
| 121 | + return Files.find(startingDir, 15, (path, attrs) -> path.endsWith(sourceFileName)) |
| 122 | + .map(p -> new File(p.toUri())) |
| 123 | + .findFirst(); |
| 124 | + } |
| 125 | + |
| 126 | + private File findRootOfProject(File cwd) { |
| 127 | + File[] pomFiles = cwd.listFiles((file, name) -> { return name.equals("pom.xml"); }); |
| 128 | + if (pomFiles != null && pomFiles.length == 1) { |
| 129 | + return cwd; |
| 130 | + } else if (cwd.getParentFile() == null) { |
| 131 | + throw new RuntimeException("Couldn't find directory containing pom.xml. Last looked in: " + cwd.getAbsolutePath()); |
| 132 | + } else { |
| 133 | + return findRootOfProject(cwd.getParentFile()); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private String getSourceFileName(Class<?> clazz) throws IOException { |
| 138 | + String resourceName = clazz.getName().replace(".", "/").concat(".class"); |
| 139 | + ClassReader reader = new ClassReader(clazz.getClassLoader().getResourceAsStream(resourceName)); |
| 140 | + SourceFileNameVisitor sourceFileNameVisitor = new SourceFileNameVisitor(); |
| 141 | + reader.accept(sourceFileNameVisitor, 0); |
| 142 | + |
| 143 | + return sourceFileNameVisitor.getSourceFile(); |
| 144 | + } |
| 145 | + |
| 146 | + |
| 147 | + private static final class SourceFileNameVisitor extends ClassVisitor { |
| 148 | + |
| 149 | + private String sourceFile = null; |
| 150 | + private boolean visitedYet = false; |
| 151 | + |
| 152 | + public SourceFileNameVisitor() { |
| 153 | + super(Opcodes.ASM5); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public void visitSource(String source, String debug) { |
| 158 | + this.visitedYet = true; |
| 159 | + this.sourceFile = source; |
| 160 | + super.visitSource(source, debug); |
| 161 | + } |
| 162 | + |
| 163 | + public String getSourceFile() { |
| 164 | + if (!visitedYet) throw new IllegalStateException("Must visit a class before asking for source file"); |
| 165 | + return this.sourceFile; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | +} |
0 commit comments