@ -1,5 +1,5 @@
/ *
/ *
* Copyright 2000 - 2015 JetBrains s . r . o .
* Copyright 2000 - 2016 JetBrains s . r . o .
*
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* you may not use this file except in compliance with the License .
@ -23,6 +23,7 @@ import org.jetbrains.java.decompiler.main.rels.MethodWrapper;
import org.jetbrains.java.decompiler.modules.decompiler.exps.* ;
import org.jetbrains.java.decompiler.modules.decompiler.exps.* ;
import org.jetbrains.java.decompiler.modules.decompiler.stats.RootStatement ;
import org.jetbrains.java.decompiler.modules.decompiler.stats.RootStatement ;
import org.jetbrains.java.decompiler.modules.decompiler.stats.Statement ;
import org.jetbrains.java.decompiler.modules.decompiler.stats.Statement ;
import org.jetbrains.java.decompiler.modules.decompiler.stats.Statements ;
import org.jetbrains.java.decompiler.modules.decompiler.vars.VarVersionPair ;
import org.jetbrains.java.decompiler.modules.decompiler.vars.VarVersionPair ;
import org.jetbrains.java.decompiler.struct.StructClass ;
import org.jetbrains.java.decompiler.struct.StructClass ;
import org.jetbrains.java.decompiler.struct.StructField ;
import org.jetbrains.java.decompiler.struct.StructField ;
@ -31,14 +32,11 @@ import org.jetbrains.java.decompiler.util.InterpreterUtil;
import java.util.ArrayList ;
import java.util.ArrayList ;
import java.util.List ;
import java.util.List ;
public class InitializerProcessor {
public class InitializerProcessor {
public static void extractInitializers ( ClassWrapper wrapper ) {
public static void extractInitializers ( ClassWrapper wrapper ) {
MethodWrapper method = wrapper . getMethodWrapper ( CodeConstants . CLINIT_NAME , "()V" ) ;
MethodWrapper meth = wrapper . getMethodWrapper ( CodeConstants . CLINIT_NAME , "()V" ) ;
if ( method ! = null & & method . root ! = null ) { // successfully decompiled static constructor
if ( meth ! = null & & meth . root ! = null ) { // successfully decompiled static constructor
extractStaticInitializers ( wrapper , method ) ;
extractStaticInitializers ( wrapper , meth ) ;
}
}
extractDynamicInitializers ( wrapper ) ;
extractDynamicInitializers ( wrapper ) ;
@ -52,30 +50,26 @@ public class InitializerProcessor {
}
}
}
}
private static void liftConstructor ( ClassWrapper wrapper ) {
private static void liftConstructor ( ClassWrapper wrapper ) {
for ( MethodWrapper method : wrapper . getMethods ( ) ) {
for ( MethodWrapper meth : wrapper . getMethods ( ) ) {
if ( CodeConstants . INIT_NAME . equals ( method . methodStruct . getName ( ) ) & & method . root ! = null ) {
if ( CodeConstants . INIT_NAME . equals ( meth . methodStruct . getName ( ) ) & & meth . root ! = null ) {
Statement firstData = Statements . findFirstData ( method . root ) ;
Statement firstdata = findFirstData ( meth . root ) ;
if ( firstData = = null ) {
if ( firstdata = = null ) {
return ;
return ;
}
}
int index = 0 ;
int index = 0 ;
List < Exprent > lstExprents = firstd ata . getExprents ( ) ;
List < Exprent > lstExprents = firstD ata . getExprents ( ) ;
for ( Exprent exprent : lstExprents ) {
for ( Exprent exprent : lstExprents ) {
int action = 0 ;
int action = 0 ;
if ( exprent . type = = Exprent . EXPRENT_ASSIGNMENT ) {
if ( exprent . type = = Exprent . EXPRENT_ASSIGNMENT ) {
AssignmentExprent ase xpr = ( AssignmentExprent ) exprent ;
AssignmentExprent assignE xpr = ( AssignmentExprent ) exprent ;
if ( ase xpr . getLeft ( ) . type = = Exprent . EXPRENT_FIELD & & ase xpr . getRight ( ) . type = = Exprent . EXPRENT_VAR ) {
if ( assignE xpr . getLeft ( ) . type = = Exprent . EXPRENT_FIELD & & assignE xpr . getRight ( ) . type = = Exprent . EXPRENT_VAR ) {
FieldExprent fe xpr = ( FieldExprent ) ase xpr . getLeft ( ) ;
FieldExprent fE xpr = ( FieldExprent ) assignE xpr . getLeft ( ) ;
if ( fe xpr . getClassname ( ) . equals ( wrapper . getClassStruct ( ) . qualifiedName ) ) {
if ( fE xpr . getClassname ( ) . equals ( wrapper . getClassStruct ( ) . qualifiedName ) ) {
StructField structField = wrapper . getClassStruct ( ) . getField ( fe xpr . getName ( ) , fe xpr . getDescriptor ( ) . descriptorString ) ;
StructField structField = wrapper . getClassStruct ( ) . getField ( fE xpr . getName ( ) , fE xpr . getDescriptor ( ) . descriptorString ) ;
if ( structField ! = null & & structField . hasModifier ( CodeConstants . ACC_FINAL ) ) {
if ( structField ! = null & & structField . hasModifier ( CodeConstants . ACC_FINAL ) ) {
action = 1 ;
action = 1 ;
}
}
@ -83,7 +77,7 @@ public class InitializerProcessor {
}
}
}
}
else if ( index > 0 & & exprent . type = = Exprent . EXPRENT_INVOCATION & &
else if ( index > 0 & & exprent . type = = Exprent . EXPRENT_INVOCATION & &
isInvocationInitConstructor ( ( InvocationExprent ) exprent , meth , wrapper , true ) ) {
Statements . isInvocationInitConstructor ( ( InvocationExprent ) exprent , method , wrapper , true ) ) {
// this() or super()
// this() or super()
lstExprents . add ( 0 , lstExprents . remove ( index ) ) ;
lstExprents . add ( 0 , lstExprents . remove ( index ) ) ;
action = 2 ;
action = 2 ;
@ -99,53 +93,50 @@ public class InitializerProcessor {
}
}
}
}
private static void hideEmptySuper ( ClassWrapper wrapper ) {
private static void hideEmptySuper ( ClassWrapper wrapper ) {
for ( MethodWrapper method : wrapper . getMethods ( ) ) {
for ( MethodWrapper meth : wrapper . getMethods ( ) ) {
if ( CodeConstants . INIT_NAME . equals ( method . methodStruct . getName ( ) ) & & method . root ! = null ) {
if ( CodeConstants . INIT_NAME . equals ( meth . methodStruct . getName ( ) ) & & meth . root ! = null ) {
Statement firstData = Statements . findFirstData ( method . root ) ;
Statement firstdata = findFirstData ( meth . root ) ;
if ( firstData = = null | | firstData . getExprents ( ) . isEmpty ( ) ) {
if ( firstdata = = null | | firstdata . getExprents ( ) . isEmpty ( ) ) {
return ;
return ;
}
}
Exprent exprent = firstd ata . getExprents ( ) . get ( 0 ) ;
Exprent exprent = firstD ata . getExprents ( ) . get ( 0 ) ;
if ( exprent . type = = Exprent . EXPRENT_INVOCATION ) {
if ( exprent . type = = Exprent . EXPRENT_INVOCATION ) {
InvocationExprent inve xpr = ( InvocationExprent ) exprent ;
InvocationExprent invE xpr = ( InvocationExprent ) exprent ;
if ( isInvocationInitConstructor ( inve xpr , meth , wrapper , false ) & & inve xpr . getLstParameters ( ) . isEmpty ( ) ) {
if ( Statements . isInvocationInitConstructor ( invE xpr , method , wrapper , false ) & & invE xpr . getLstParameters ( ) . isEmpty ( ) ) {
firstd ata . getExprents ( ) . remove ( 0 ) ;
firstD ata . getExprents ( ) . remove ( 0 ) ;
}
}
}
}
}
}
}
}
}
}
private static void extractStaticInitializers ( ClassWrapper wrapper , MethodWrapper meth ) {
private static void extractStaticInitializers ( ClassWrapper wrapper , MethodWrapper method ) {
RootStatement root = method . root ;
RootStatement root = meth . root ;
StructClass cl = wrapper . getClassStruct ( ) ;
StructClass cl = wrapper . getClassStruct ( ) ;
Statement firstData = Statements . findFirstData ( root ) ;
if ( firstData ! = null ) {
boolean isInterface = cl . hasModifier ( CodeConstants . ACC_INTERFACE ) ;
boolean isInterface = cl . hasModifier ( CodeConstants . ACC_INTERFACE ) ;
Statement firstdata = findFirstData ( root ) ;
while ( ! firstData . getExprents ( ) . isEmpty ( ) ) {
if ( firstdata ! = null ) {
Exprent exprent = firstData . getExprents ( ) . get ( 0 ) ;
while ( ! firstdata . getExprents ( ) . isEmpty ( ) ) {
Exprent exprent = firstdata . getExprents ( ) . get ( 0 ) ;
boolean found = false ;
boolean found = false ;
if ( exprent . type = = Exprent . EXPRENT_ASSIGNMENT ) {
if ( exprent . type = = Exprent . EXPRENT_ASSIGNMENT ) {
AssignmentExprent ase xpr = ( AssignmentExprent ) exprent ;
AssignmentExprent assignE xpr = ( AssignmentExprent ) exprent ;
if ( ase xpr . getLeft ( ) . type = = Exprent . EXPRENT_FIELD ) {
if ( assignE xpr . getLeft ( ) . type = = Exprent . EXPRENT_FIELD ) {
FieldExprent fe xpr = ( FieldExprent ) ase xpr . getLeft ( ) ;
FieldExprent fE xpr = ( FieldExprent ) assignE xpr . getLeft ( ) ;
if ( fe xpr . isStatic ( ) & & fe xpr . getClassname ( ) . equals ( cl . qualifiedName ) & &
if ( fE xpr . isStatic ( ) & & fE xpr . getClassname ( ) . equals ( cl . qualifiedName ) & &
cl . hasField ( fe xpr . getName ( ) , fe xpr . getDescriptor ( ) . descriptorString ) ) {
cl . hasField ( fE xpr . getName ( ) , fE xpr . getDescriptor ( ) . descriptorString ) ) {
// interfaces fields should always be initialized inline
// interfaces fields should always be initialized inline
if ( isInterface | | isExprentIndependent ( ase xpr . getRight ( ) , meth ) ) {
if ( isInterface | | isExprentIndependent ( assignE xpr . getRight ( ) , method ) ) {
String keyField = InterpreterUtil . makeUniqueKey ( fe xpr . getName ( ) , fe xpr . getDescriptor ( ) . descriptorString ) ;
String keyField = InterpreterUtil . makeUniqueKey ( fE xpr . getName ( ) , fE xpr . getDescriptor ( ) . descriptorString ) ;
if ( ! wrapper . getStaticFieldInitializers ( ) . containsKey ( keyField ) ) {
if ( ! wrapper . getStaticFieldInitializers ( ) . containsKey ( keyField ) ) {
wrapper . getStaticFieldInitializers ( ) . addWithKey ( ase xpr . getRight ( ) , keyField ) ;
wrapper . getStaticFieldInitializers ( ) . addWithKey ( assignE xpr . getRight ( ) , keyField ) ;
firstd ata . getExprents ( ) . remove ( 0 ) ;
firstD ata . getExprents ( ) . remove ( 0 ) ;
found = true ;
found = true ;
}
}
}
}
@ -161,27 +152,26 @@ public class InitializerProcessor {
}
}
private static void extractDynamicInitializers ( ClassWrapper wrapper ) {
private static void extractDynamicInitializers ( ClassWrapper wrapper ) {
StructClass cl = wrapper . getClassStruct ( ) ;
StructClass cl = wrapper . getClassStruct ( ) ;
boolean isAnonymous = DecompilerContext . getClassProcessor ( ) . getMapRootClasses ( ) . get ( cl . qualifiedName ) . type = = ClassNode . CLASS_ANONYMOUS ;
boolean isAnonymous = DecompilerContext . getClassProcessor ( ) . getMapRootClasses ( ) . get ( cl . qualifiedName ) . type = = ClassNode . CLASS_ANONYMOUS ;
List < List < Exprent > > lstFirst = new ArrayList < List < Exprent > > ( ) ;
List < List < Exprent > > lstFirst = new ArrayList < List < Exprent > > ( ) ;
List < MethodWrapper > lstMethWrappers = new ArrayList < MethodWrapper > ( ) ;
List < MethodWrapper > lstMethod Wrappers = new ArrayList < MethodWrapper > ( ) ;
for ( MethodWrapper meth : wrapper . getMethods ( ) ) {
for ( MethodWrapper method : wrapper . getMethods ( ) ) {
if ( CodeConstants . INIT_NAME . equals ( meth . methodStruct . getName ( ) ) & & meth . root ! = null ) { // successfully decompiled constructor
if ( CodeConstants . INIT_NAME . equals ( method . methodStruct . getName ( ) ) & & method . root ! = null ) { // successfully decompiled constructor
Statement firstd ata = findFirstData ( meth . root ) ;
Statement firstD ata = Statements . findFirstData ( method . root ) ;
if ( firstd ata = = null | | firstd ata . getExprents ( ) . isEmpty ( ) ) {
if ( firstD ata = = null | | firstD ata . getExprents ( ) . isEmpty ( ) ) {
return ;
return ;
}
}
lstFirst . add ( firstd ata . getExprents ( ) ) ;
lstFirst . add ( firstD ata . getExprents ( ) ) ;
lstMethWrappers . add ( meth ) ;
lstMethod Wrappers . add ( method ) ;
Exprent exprent = firstd ata . getExprents ( ) . get ( 0 ) ;
Exprent exprent = firstD ata . getExprents ( ) . get ( 0 ) ;
if ( ! isAnonymous ) { // FIXME: doesn't make sense
if ( ! isAnonymous ) { // FIXME: doesn't make sense
if ( exprent . type ! = Exprent . EXPRENT_INVOCATION | |
if ( exprent . type ! = Exprent . EXPRENT_INVOCATION | |
! isInvocationInitConstructor ( ( InvocationExprent ) exprent , meth , wrapper , false ) ) {
! Statements . isInvocationInitConstructor ( ( InvocationExprent ) exprent , method , wrapper , false ) ) {
return ;
return ;
}
}
}
}
@ -193,12 +183,10 @@ public class InitializerProcessor {
}
}
while ( true ) {
while ( true ) {
String fieldWithDescr = null ;
String fieldWithDescr = null ;
Exprent value = null ;
Exprent value = null ;
for ( int i = 0 ; i < lstFirst . size ( ) ; i + + ) {
for ( int i = 0 ; i < lstFirst . size ( ) ; i + + ) {
List < Exprent > lst = lstFirst . get ( i ) ;
List < Exprent > lst = lstFirst . get ( i ) ;
if ( lst . size ( ) < ( isAnonymous ? 1 : 2 ) ) {
if ( lst . size ( ) < ( isAnonymous ? 1 : 2 ) ) {
@ -210,22 +198,21 @@ public class InitializerProcessor {
boolean found = false ;
boolean found = false ;
if ( exprent . type = = Exprent . EXPRENT_ASSIGNMENT ) {
if ( exprent . type = = Exprent . EXPRENT_ASSIGNMENT ) {
AssignmentExprent asexpr = ( AssignmentExprent ) exprent ;
AssignmentExprent assignExpr = ( AssignmentExprent ) exprent ;
if ( asexpr . getLeft ( ) . type = = Exprent . EXPRENT_FIELD ) {
if ( assignExpr . getLeft ( ) . type = = Exprent . EXPRENT_FIELD ) {
FieldExprent fexpr = ( FieldExprent ) asexpr . getLeft ( ) ;
FieldExprent fExpr = ( FieldExprent ) assignExpr . getLeft ( ) ;
if ( ! fexpr . isStatic ( ) & & fexpr . getClassname ( ) . equals ( cl . qualifiedName ) & &
if ( ! fExpr . isStatic ( ) & & fExpr . getClassname ( ) . equals ( cl . qualifiedName ) & &
cl . hasField ( fexpr . getName ( ) , fexpr
cl . hasField ( fExpr . getName ( ) , fExpr . getDescriptor ( ) . descriptorString ) ) { // check for the physical existence of the field. Could be defined in a superclass.
. getDescriptor ( ) . descriptorString ) ) { // check for the physical existence of the field. Could be defined in a superclass.
if ( isExprentIndependent ( assignExpr . getRight ( ) , lstMethodWrappers . get ( i ) ) ) {
if ( isExprentIndependent ( asexpr . getRight ( ) , lstMethWrappers . get ( i ) ) ) {
String fieldKey = InterpreterUtil . makeUniqueKey ( fExpr . getName ( ) , fExpr . getDescriptor ( ) . descriptorString ) ;
String fieldKey = InterpreterUtil . makeUniqueKey ( fexpr . getName ( ) , fexpr . getDescriptor ( ) . descriptorString ) ;
if ( fieldWithDescr = = null ) {
if ( fieldWithDescr = = null ) {
fieldWithDescr = fieldKey ;
fieldWithDescr = fieldKey ;
value = ase xpr . getRight ( ) ;
value = assignE xpr . getRight ( ) ;
}
}
else {
else {
if ( ! fieldWithDescr . equals ( fieldKey ) | |
if ( ! fieldWithDescr . equals ( fieldKey ) | |
! value . equals ( ase xpr . getRight ( ) ) ) {
! value . equals ( assignE xpr . getRight ( ) ) ) {
return ;
return ;
}
}
}
}
@ -253,19 +240,17 @@ public class InitializerProcessor {
}
}
}
}
private static boolean isExprentIndependent ( Exprent exprent , MethodWrapper meth ) {
private static boolean isExprentIndependent ( Exprent exprent , MethodWrapper method ) {
List < Exprent > lst = exprent . getAllExprents ( true ) ;
List < Exprent > lst = exprent . getAllExprents ( true ) ;
lst . add ( exprent ) ;
lst . add ( exprent ) ;
for ( Exprent expr : lst ) {
for ( Exprent expr : lst ) {
switch ( expr . type ) {
switch ( expr . type ) {
case Exprent . EXPRENT_VAR :
case Exprent . EXPRENT_VAR :
VarVersionPair varpaar = new VarVersionPair ( ( VarExprent ) expr ) ;
VarVersionPair varPair = new VarVersionPair ( ( VarExprent ) expr ) ;
if ( ! meth . varproc . getExternalVars ( ) . contains ( varpaar ) ) {
if ( ! method . varproc . getExternalVars ( ) . contains ( varPair ) ) {
String varname = meth . varproc . getVarName ( varpaar ) ;
String varName = method . varproc . getVarName ( varPair ) ;
if ( ! varName . equals ( "this" ) & & ! varName . endsWith ( ".this" ) ) { // FIXME: remove direct comparison with strings
if ( ! varname . equals ( "this" ) & & ! varname . endsWith ( ".this" ) ) { // FIXME: remove direct comparison with strings
return false ;
return false ;
}
}
}
}
@ -277,48 +262,4 @@ public class InitializerProcessor {
return true ;
return true ;
}
}
private static Statement findFirstData ( Statement stat ) {
if ( stat . getExprents ( ) ! = null ) {
return stat ;
}
else {
if ( stat . isLabeled ( ) ) { // FIXME: Why??
return null ;
}
switch ( stat . type ) {
case Statement . TYPE_SEQUENCE :
case Statement . TYPE_IF :
case Statement . TYPE_ROOT :
case Statement . TYPE_SWITCH :
case Statement . TYPE_SYNCRONIZED :
return findFirstData ( stat . getFirst ( ) ) ;
default :
return null ;
}
}
}
private static boolean isInvocationInitConstructor ( InvocationExprent inv , MethodWrapper meth , ClassWrapper wrapper , boolean withThis ) {
if ( inv . getFunctype ( ) = = InvocationExprent . TYP_INIT ) {
if ( inv . getInstance ( ) . type = = Exprent . EXPRENT_VAR ) {
VarExprent instvar = ( VarExprent ) inv . getInstance ( ) ;
VarVersionPair varpaar = new VarVersionPair ( instvar ) ;
String classname = meth . varproc . getThisVars ( ) . get ( varpaar ) ;
if ( classname ! = null ) { // any this instance. TODO: Restrict to current class?
if ( withThis | | ! wrapper . getClassStruct ( ) . qualifiedName . equals ( inv . getClassname ( ) ) ) {
return true ;
}
}
}
}
return false ;
}
}
}