Skip to content

Commit

Permalink
added PathVisitorFileFilterTest - ternaryop and line number fix (#208)
Browse files Browse the repository at this point in the history
Co-authored-by: nbauma109 <nbauma109@github.com>
  • Loading branch information
nbauma109 and nbauma109 authored Mar 26, 2023
1 parent 8a7c028 commit 42202c9
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.bcel.classfile.ConstantNameAndType;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

Expand All @@ -35,7 +36,7 @@
import jd.core.model.instruction.bytecode.instruction.InvokeNoStaticInstruction;
import jd.core.model.instruction.bytecode.instruction.Invokevirtual;
import jd.core.model.instruction.bytecode.instruction.StoreInstruction;
import jd.core.process.layouter.visitor.MinMaxLineNumberVisitor;
import jd.core.process.layouter.visitor.MinLineNumberVisitor;

/**
* try-catch-finally
Expand All @@ -60,13 +61,51 @@ public List<Instruction> getFinallyInstructions() {
return finallyInstructions;
}

public record FastCatch(int exceptionOffset, int exceptionTypeIndex, int[] otherExceptionTypeIndexes, int localVarIndex, List<Instruction> instructions) {
public static class FastCatch {
int exceptionOffset;
int exceptionTypeIndex;
int[] otherExceptionTypeIndexes;
int localVarIndex;
List<Instruction> instructions;

public FastCatch(int exceptionOffset, int exceptionTypeIndex, int[] otherExceptionTypeIndexes, int localVarIndex, List<Instruction> instructions) {
this.exceptionOffset = exceptionOffset;
this.exceptionTypeIndex = exceptionTypeIndex;
this.otherExceptionTypeIndexes = otherExceptionTypeIndexes;
this.localVarIndex = localVarIndex;
this.instructions = instructions;
}

public boolean removeOutOfBounds(int firstLineNumber) {
return instructions.removeIf(instr -> instr.getLineNumber() < firstLineNumber);
}

public int minLineNumber() {
return MinMaxLineNumberVisitor.visit(instructions).minLineNumber();
return instructions
.stream()
.min(Comparator.comparing(MinLineNumberVisitor::visit))
.map(Instruction::getLineNumber)
.orElse(Integer.MAX_VALUE);
}

public int localVarIndex() {
return localVarIndex;
}

public int exceptionOffset() {
return exceptionOffset;
}

public int exceptionTypeIndex() {
return exceptionTypeIndex;
}

public List<Instruction> instructions() {
return instructions;
}

public int[] otherExceptionTypeIndexes() {
return otherExceptionTypeIndexes;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1634,7 +1634,7 @@ private static boolean checkTernaryOperator(List<Instruction> list, int index)
Goto g = (Goto)list.get(index-1);
int jumpOffset = g.getJumpOffset();
int returnOffset = list.get(index).getOffset();
if (g.getOffset() < jumpOffset && jumpOffset < returnOffset)
if (g.getOffset() < jumpOffset && jumpOffset <= returnOffset)
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ public static int visit(Instruction instruction)
}
break;
case ByteCodeConstants.IFCMP:
maxLineNumber = visit(((IfCmp)instruction).getValue2());
IfCmp ifCmp = (IfCmp)instruction;
maxLineNumber = Math.max(visit(ifCmp.getValue1()), visit(ifCmp.getValue2()));
break;
case ByteCodeConstants.IF,
ByteCodeConstants.IFXNULL:
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/jd/core/test/PathVisitorFileFilterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package jd.core.test;

import org.apache.commons.io.IOUtils;
import org.junit.Test;

import java.nio.charset.StandardCharsets;

import static org.junit.Assert.assertEquals;

public class PathVisitorFileFilterTest extends AbstractTestCase {
@Test
public void test() throws Exception {
String output = decompile("org/apache/commons/io/filefilter/PathVisitorFileFilter");
assertEquals(IOUtils.toString(getClass().getResource("PathVisitorFileFilter.txt"), StandardCharsets.UTF_8), output);
}
}
83 changes: 83 additions & 0 deletions src/test/resources/jd/core/test/PathVisitorFileFilter.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* */ package org.apache.commons.io.filefilter;
/* */
/* */ import java.io.File;
/* */ import java.io.IOException;
/* */ import java.nio.file.FileVisitResult;
/* */ import java.nio.file.Files;
/* */ import java.nio.file.LinkOption;
/* */ import java.nio.file.Path;
/* */ import java.nio.file.attribute.BasicFileAttributes;
/* */ import org.apache.commons.io.file.NoopPathVisitor;
/* */ import org.apache.commons.io.file.PathUtils;
/* */ import org.apache.commons.io.file.PathVisitor;
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */ public class PathVisitorFileFilter
/* */ extends AbstractFileFilter
/* */ {
/* */ private final PathVisitor pathVisitor;
/* */
/* */ public PathVisitorFileFilter(PathVisitor pathVisitor)
/* */ {
/* 46 */ this.pathVisitor = (pathVisitor == null ? NoopPathVisitor.INSTANCE : pathVisitor);
/* */ }
/* */
/* */ @Override
/* */ public boolean accept(File file) {
/* */ try {
/* 52 */ Path path = file.toPath();
/* 53 */ return visitFile(path, file
/* 54 */ .exists() ? PathUtils.readBasicFileAttributes(path) : null) == FileVisitResult.CONTINUE;
/* */ } catch (IOException e) {
/* 56 */ return handle(e) == FileVisitResult.CONTINUE;
/* */ }
/* */ }
/* */
/* */ @Override
/* */ public boolean accept(File dir, String name) {
/* */ try {
/* 63 */ Path path = dir.toPath().resolve(name);
/* 64 */ return accept(path, PathUtils.readBasicFileAttributes(path)) == FileVisitResult.CONTINUE;
/* */ } catch (IOException e) {
/* 66 */ return handle(e) == FileVisitResult.CONTINUE;
/* */ }
/* */ }
/* */
/* */ @Override
/* */ public FileVisitResult accept(Path path, BasicFileAttributes attributes) {
/* */ try {
/* 73 */ return Files.isDirectory(path) ? this.pathVisitor.postVisitDirectory(path, null) : visitFile(path, attributes);
/* */ } catch (IOException e) {
/* 75 */ return handle(e);
/* */ }
/* */ }
/* */
/* */ @Override
/* */ public FileVisitResult visitFile(Path path, BasicFileAttributes attributes) throws IOException {
/* 81 */ return this.pathVisitor.visitFile(path, attributes);
/* */ }
/* */ }

0 comments on commit 42202c9

Please sign in to comment.