Skip to content

Commit

Permalink
fixed bug for JDK 1.6 new expression with type parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
nbauma109 committed Oct 30, 2022
1 parent 536aff5 commit 733412c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 17 deletions.
13 changes: 12 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.nbauma109</groupId>
<artifactId>jd-core</artifactId>
Expand Down Expand Up @@ -94,6 +95,10 @@
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>nbauma109</id>
<url>https://raw.githubusercontent.com/nbauma109/mvn-repo/main</url>
</repository>
</repositories>
<dependencies>
<dependency>
Expand Down Expand Up @@ -323,5 +328,11 @@
<version>3.31.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jd</groupId>
<artifactId>jd-core</artifactId>
<version>0.7.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -463,11 +463,12 @@ public void visit(NewExpression expression) {
if (parameters != null) {
boolean unique = typeMaker.matchCount(expression.getObjectType().getInternalName(), StringConstants.INSTANCE_CONSTRUCTOR, parameters.size(), true) <= 1;
boolean forceCast = !unique && typeMaker.matchCount(Collections.emptyMap(), typeBounds, expression.getObjectType().getInternalName(), StringConstants.INSTANCE_CONSTRUCTOR, parameters, true) > 1;
boolean rawCast = (returnedType instanceof ObjectType && expression.getType() instanceof ObjectType
&& typeMaker.isRawTypeAssignable((ObjectType) returnedType, (ObjectType) expression.getType())
&& !typeMaker.isAssignable(typeBounds, (ObjectType) returnedType, (ObjectType) expression.getType()));
Type currentType = type == null ? returnedType : type;
boolean rawCast = (currentType instanceof ObjectType && expression.getType() instanceof ObjectType
&& typeMaker.isRawTypeAssignable((ObjectType) currentType, expression.getObjectType())
&& !typeMaker.isAssignable(typeBounds, (ObjectType) currentType, expression.getObjectType()));
if (rawCast) {
expression.setObjectType(expression.getObjectType().createType(((ObjectType) returnedType).getTypeArguments()));
expression.setObjectType(expression.getObjectType().createType(((ObjectType) currentType).getTypeArguments()));
}
BaseType parameterTypes = ((ClassFileNewExpression)expression).getParameterTypes();
expression.setParameters(updateParameters(Collections.emptyMap(), typeBounds, parameterTypes, null, parameters, forceCast, unique, rawCast));
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/org/jd/core/v1/ExpectedExceptionMatcherBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.jd.core.v1;

import org.hamcrest.Matcher;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class ExpectedExceptionMatcherBuilder {
private final List<Matcher<?>> matchers = new ArrayList<Matcher<?>>();

@SuppressWarnings({ "unchecked", "rawtypes", "unused" })
private List<Matcher<? super Throwable>> castedMatchers() {
return new ArrayList<Matcher<? super Throwable>>((Collection) matchers);
}
}
30 changes: 18 additions & 12 deletions src/test/java/org/jd/core/v1/MiscTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import org.apache.logging.log4j.spi.AbstractLogger;
import org.apache.logging.log4j.util.IndexedStringMap;
import org.apache.logging.log4j.util.TriConsumer;
import org.hamcrest.Matcher;
import org.jd.core.test.TryResourcesImaging;
import org.jd.core.v1.api.loader.Loader;
import org.jd.core.v1.compiler.CompilerUtil;
Expand Down Expand Up @@ -110,6 +109,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import jd.core.process.analyzer.instruction.fast.FastCodeExceptionAnalyzer;

public class MiscTest extends AbstractJdTest {

// TODO: in progress
Expand Down Expand Up @@ -326,6 +327,15 @@ public void testTryResourcesImaging() throws Exception {
assertTrue(CompilerUtil.compile("11", new InMemoryJavaSourceFileObject(internalClassName, source)));
}
}

@Test
public void testFastCodeExceptionAnalyzer() throws Exception {
String internalClassName = FastCodeExceptionAnalyzer.class.getName().replace('.', '/');
String source = decompileSuccess(new ClassPathLoader(), new PlainTextPrinter(), internalClassName);

// Recompile decompiled source code and check errors
assertTrue(CompilerUtil.compile("1.6", new InMemoryJavaSourceFileObject(internalClassName, source)));
}

@Test
public void testBmpImageParser() throws Exception {
Expand Down Expand Up @@ -485,18 +495,14 @@ static <T> Iterable<T> callServiceLoader(MethodHandle handle, Class<T> serviceTy

@Test
public void testExpectedExceptionMatcherBuilder() throws Exception {
class ExpectedExceptionMatcherBuilder {
private final List<Matcher<?>> matchers = new ArrayList<Matcher<?>>();
@SuppressWarnings({"unchecked", "rawtypes", "unused"})
private List<Matcher<? extends Throwable>> castedMatchers() {
return new ArrayList<Matcher<? extends Throwable>>((Collection) matchers);
}
}
String internalClassName = ExpectedExceptionMatcherBuilder.class.getName().replace('.', '/');
String source = decompileSuccess(new ClassPathLoader(), new PlainTextPrinter(), internalClassName);

// Recompile decompiled source code and check errors
assertTrue(CompilerUtil.compile("1.8", new InMemoryJavaSourceFileObject(internalClassName, source)));
try (InputStream is = this.getClass().getResourceAsStream("/jar/expected-exception-matcher-builder-jdk1.6.0u119.jar")) {
Loader loader = new ZipLoader(is);
String source = decompileSuccess(loader, new PlainTextPrinter(), internalClassName);

// Recompile decompiled source code and check errors
assertTrue(CompilerUtil.compile("1.6", new InMemoryJavaSourceFileObject(internalClassName, source)));
}
}

@Test
Expand Down
Binary file not shown.

0 comments on commit 733412c

Please sign in to comment.