Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #394 #429

Open
wants to merge 2 commits into
base: develop/1.11.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ public enum Type {

private static final BitSet EMPTY_BIT_SET = new BitSet(0);

private static final VarType JAVA_NIO_BUFFER = new VarType(CodeType.OBJECT, 0, "java/nio/Buffer");

private String name;
private String classname;
private boolean isStatic;
Expand Down Expand Up @@ -397,7 +395,7 @@ else if (!bounds.containsKey(from)) {
start = (newNode.access & CodeConstants.ACC_STATIC) == 0 ? 1 : 0;
}
}

Set<VarType> commonGenerics = new HashSet<>();
ClassNode currentNode = DecompilerContext.getContextProperty(DecompilerContext.CURRENT_CLASS_NODE);
MethodWrapper methodWrapper = DecompilerContext.getContextProperty(DecompilerContext.CURRENT_METHOD_WRAPPER);
Expand All @@ -410,7 +408,7 @@ else if (!bounds.containsKey(from)) {
parents.add(search);
search = (search.access & CodeConstants.ACC_STATIC) == 0 ? search.parent : null;
}

search = newNode;
while (search != null) {
if (parents.contains(search) && search.classStruct.getSignature() != null) {
Expand Down Expand Up @@ -812,20 +810,19 @@ else if (instance != null) {
ClassNode instNode = DecompilerContext.getClassProcessor().getMapRootClasses().get(classname);
// Don't cast to anonymous classes, since they by definition can't have a name
// TODO: better fix may be to change equals to isSuperSet? all anonymous classes are superset of Object
if (rightType.equals(VarType.VARTYPE_OBJECT) && !leftType.equals(rightType) && (instNode != null && instNode.type != ClassNode.Type.ANONYMOUS)) {
if (!leftType.equals(rightType) &&
(rightType.equals(VarType.VARTYPE_OBJECT) ||
// try to preserve for navigation in certain cases: virtual call on variable
(rightType.type != CodeType.UNKNOWN && instance instanceof VarExprent && invocationType == InvocationType.VIRTUAL && !leftType.equals(VarType.VARTYPE_OBJECT))
)
) {
appendInstCast(buf, leftType, res);
} else if (remappedInstType != null) {
// If we have a remap inst type, do a cast
appendInstCast(buf, remappedInstType, res);
} else if (instance.getPrecedence() > getPrecedence() && !canSkipParenEnclose(instance)) {
buf.append("(").append(res).append(")");
}
//Java 9+ adds some overrides to java/nio/Buffer's subclasses that alter the return types.
//This isn't properly handled by the compiler. So explicit casts are needed to retain J8 compatibility.
else if (JAVA_NIO_BUFFER.equals(descriptor.ret) && !JAVA_NIO_BUFFER.equals(rightType)
&& DecompilerContext.getStructContext().instanceOf(rightType.value, JAVA_NIO_BUFFER.value)) {
buf.append("((").appendCastTypeName(JAVA_NIO_BUFFER).append(")").append(res).append(")");
}
else {
buf.append(res);
}
Expand Down
1 change: 1 addition & 0 deletions test/org/jetbrains/java/decompiler/SingleClassesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,7 @@ private void registerDefault() {
register(JAVA_21, "TestCastIntersectionJ21");
register(JAVA_16, "TestRecordLocal");
register(JAVA_8, "TestAnonymousClassToLambda");
register(JAVA_8, "TryToPreserveCast");
}

private void registerEntireClassPath() {
Expand Down
1 change: 1 addition & 0 deletions testData/results/pkg/TestSynchronizedTry.dec
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

package pkg;

public class TestSynchronizedTry {
Expand Down
18 changes: 18 additions & 0 deletions testData/src/java8/pkg/TryToPreserveCast.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package pkg;

import java.nio.Buffer;
import java.nio.ByteBuffer;

public class TryToPreserveCast {
public TryToPreserveCast() {
}

public static void main(String[] args) {

}

public void test(ByteBuffer buffer) {
((Buffer) buffer).limit(1);
(buffer).limit(2);
}
}
Loading