<s> package org . eclipse . jdt . internal . eval ; import org . eclipse . jdt . core . compiler . CharOperation ; import org . eclipse . jdt . internal . compiler . classfmt . ClassFileConstants ; import org . eclipse . jdt . internal . compiler . lookup . ArrayBinding ; import org . eclipse . jdt . internal . compiler . lookup . Binding ; import org . eclipse . jdt . internal . compiler . lookup . BlockScope ; import org . eclipse . jdt . internal . compiler . lookup . FieldBinding ; import org . eclipse . jdt . internal . compiler . lookup . InvocationSite ; import org . eclipse . jdt . internal . compiler . lookup . MethodBinding ; import org . eclipse . jdt . internal . compiler . lookup . PackageBinding ; import org . eclipse . jdt . internal . compiler . lookup . ProblemBinding ; import org . eclipse . jdt . internal . compiler . lookup . ProblemFieldBinding ; import org . eclipse . jdt . internal . compiler . lookup . ProblemMethodBinding ; import org . eclipse . jdt . internal . compiler . lookup . ProblemReasons ; import org . eclipse . jdt . internal . compiler . lookup . ProblemReferenceBinding ; import org . eclipse . jdt . internal . compiler . lookup . ReferenceBinding ; import org . eclipse . jdt . internal . compiler . lookup . Scope ; import org . eclipse . jdt . internal . compiler . lookup . TypeBinding ; import org . eclipse . jdt . internal . compiler . lookup . TypeConstants ; import org . eclipse . jdt . internal . compiler . lookup . VariableBinding ; public class CodeSnippetScope extends BlockScope { protected CodeSnippetScope ( int kind , Scope parent ) { super ( kind , parent ) ; } public CodeSnippetScope ( BlockScope parent ) { super ( parent ) ; } public CodeSnippetScope ( BlockScope parent , int variableCount ) { super ( parent , variableCount ) ; } public final boolean canBeSeenByForCodeSnippet ( FieldBinding fieldBinding , TypeBinding receiverType , InvocationSite invocationSite , Scope scope ) { if ( fieldBinding . isPublic ( ) ) return true ; ReferenceBinding invocationType = ( ReferenceBinding ) receiverType ; if ( invocationType == fieldBinding . declaringClass ) return true ; if ( fieldBinding . isProtected ( ) ) { if ( invocationType == fieldBinding . declaringClass ) return true ; if ( invocationType . fPackage == fieldBinding . declaringClass . fPackage ) return true ; if ( fieldBinding . declaringClass . isSuperclassOf ( invocationType ) ) { if ( invocationSite . isSuperAccess ( ) ) return true ; if ( receiverType instanceof ArrayBinding ) return false ; if ( invocationType . isSuperclassOf ( ( ReferenceBinding ) receiverType ) ) return true ; if ( fieldBinding . isStatic ( ) ) return true ; } return false ; } if ( fieldBinding . isPrivate ( ) ) { if ( receiverType != fieldBinding . declaringClass ) return false ; if ( invocationType != fieldBinding . declaringClass ) { ReferenceBinding outerInvocationType = invocationType ; ReferenceBinding temp = outerInvocationType . enclosingType ( ) ; while ( temp != null ) { outerInvocationType = temp ; temp = temp . enclosingType ( ) ; } ReferenceBinding outerDeclaringClass = fieldBinding . declaringClass ; temp = outerDeclaringClass . enclosingType ( ) ; while ( temp != null ) { outerDeclaringClass = temp ; temp = temp . enclosingType ( ) ; } if ( outerInvocationType != outerDeclaringClass ) return false ; } return true ; } if ( invocationType . fPackage != fieldBinding . declaringClass . fPackage ) return false ; if ( receiverType instanceof ArrayBinding ) return false ; ReferenceBinding type = ( ReferenceBinding ) receiverType ; PackageBinding declaringPackage = fieldBinding . declaringClass . fPackage ; TypeBinding originalDeclaringClass = fieldBinding . declaringClass . original ( ) ; do { if ( type . isCapture ( ) ) { if ( originalDeclaringClass == type . erasure ( ) . original ( ) ) return true ; } else { if ( originalDeclaringClass == type . original ( ) ) return true ; } if ( declaringPackage != type . fPackage ) return false ; } while ( ( type = type . superclass ( ) ) != null ) ; return false ; } public final boolean canBeSeenByForCodeSnippet ( MethodBinding methodBinding , TypeBinding receiverType , InvocationSite invocationSite , Scope scope ) { if ( methodBinding . isPublic ( ) ) return true ; ReferenceBinding invocationType = ( ReferenceBinding ) receiverType ; if ( invocationType == methodBinding . declaringClass ) return true ; if ( methodBinding . isProtected ( ) ) { if ( invocationType == methodBinding . declaringClass ) return true ; if ( invocationType . fPackage == methodBinding . declaringClass . fPackage ) return true ; if ( methodBinding . declaringClass . isSuperclassOf ( invocationType ) ) { if ( invocationSite . isSuperAccess ( ) ) return true ; if ( receiverType instanceof ArrayBinding ) return false ; if ( invocationType . isSuperclassOf ( ( ReferenceBinding ) receiverType ) ) return true ; if ( methodBinding . isStatic ( ) ) return true ; } return false ; } if ( methodBinding . isPrivate ( ) ) { if ( receiverType != methodBinding . declaringClass ) return false ; if ( invocationType != methodBinding . declaringClass ) { ReferenceBinding outerInvocationType = invocationType ; ReferenceBinding temp = outerInvocationType . enclosingType ( ) ; while ( temp != null ) { outerInvocationType = temp ; temp = temp . enclosingType ( ) ; } ReferenceBinding outerDeclaringClass = methodBinding . declaringClass ; temp = outerDeclaringClass . enclosingType ( ) ; while ( temp != null ) { outerDeclaringClass = temp ; temp = temp . enclosingType ( ) ; } if ( outerInvocationType != outerDeclaringClass ) return false ; } return true ; } if ( invocationType . fPackage != methodBinding . declaringClass . fPackage ) return false ; if ( receiverType instanceof ArrayBinding ) return false ; ReferenceBinding type = ( ReferenceBinding ) receiverType ; PackageBinding declaringPackage = methodBinding . declaringClass . fPackage ; TypeBinding originalDeclaringClass = methodBinding . declaringClass . original ( ) ; do { if ( type . isCapture ( ) ) { if ( originalDeclaringClass == type . erasure ( ) . original ( ) ) return true ; } else { if ( originalDeclaringClass == type . original ( ) ) return true ; } if ( declaringPackage != type . fPackage ) return false ; } while ( ( type = type . superclass ( ) ) != null ) ; return false ; } public final boolean canBeSeenByForCodeSnippet ( ReferenceBinding referenceBinding , ReferenceBinding receiverType ) { if ( referenceBinding . isPublic ( ) ) return true ; if ( receiverType == referenceBinding ) return true ; if ( referenceBinding . isProtected ( ) ) { return receiverType . fPackage == referenceBinding . fPackage || referenceBinding . isSuperclassOf ( receiverType ) || referenceBinding . enclosingType ( ) . isSuperclassOf ( receiverType ) ; } if ( referenceBinding . isPrivate ( ) ) { ReferenceBinding outerInvocationType = receiverType ; ReferenceBinding temp = outerInvocationType . enclosingType ( ) ; while ( temp != null ) { outerInvocationType = temp ; temp = temp . enclosingType ( ) ; } ReferenceBinding outerDeclaringClass = referenceBinding ; temp = outerDeclaringClass . enclosingType ( ) ; while ( temp != null ) { outerDeclaringClass = temp ; temp = temp . enclosingType ( ) ; } return outerInvocationType == outerDeclaringClass ; } return receiverType . fPackage == referenceBinding . fPackage ; } public MethodBinding findExactMethod ( ReferenceBinding receiverType , char [ ] selector , TypeBinding [ ] argumentTypes , InvocationSite invocationSite ) { MethodBinding exactMethod = receiverType . getExactMethod ( selector , argumentTypes , null ) ; if ( exactMethod != null ) { if ( receiverType . isInterface ( ) || canBeSeenByForCodeSnippet ( exactMethod , receiverType , invocationSite , this ) ) return exactMethod ; } return null ; } public FieldBinding findFieldForCodeSnippet ( TypeBinding receiverType , char [ ] fieldName , InvocationSite invocationSite ) { if ( receiverType . isBaseType ( ) ) return null ; if ( receiverType . isArrayType ( ) ) { TypeBinding leafType = receiverType . leafComponentType ( ) ; if ( leafType instanceof ReferenceBinding ) if ( ! ( ( ReferenceBinding ) leafType ) . canBeSeenBy ( this ) ) { return new ProblemFieldBinding ( ( ReferenceBinding ) leafType , fieldName , ProblemReasons . ReceiverTypeNotVisible ) ; } if ( CharOperation . equals ( fieldName , TypeConstants . LENGTH ) ) return ArrayBinding . ArrayLength ; return null ; } ReferenceBinding currentType = ( ReferenceBinding ) receiverType ; if ( ! currentType . canBeSeenBy ( this ) ) return new ProblemFieldBinding ( currentType , fieldName , ProblemReasons . ReceiverTypeNotVisible ) ; FieldBinding field = currentType . getField ( fieldName , true ) ; if ( field != null ) { if ( canBeSeenByForCodeSnippet ( field , currentType , invocationSite , this ) ) return field ; else return new ProblemFieldBinding ( field , field . declaringClass , fieldName , ProblemReasons . NotVisible ) ; } ReferenceBinding [ ] [ ] interfacesToVisit = null ; int lastPosition = - 1 ; FieldBinding visibleField = null ; boolean keepLooking = true ; boolean notVisible = false ; while ( keepLooking ) { ReferenceBinding [ ] itsInterfaces = currentType . superInterfaces ( ) ; if ( itsInterfaces != Binding . NO_SUPERINTERFACES ) { if ( interfacesToVisit == null ) interfacesToVisit = new ReferenceBinding [ 5 ] [ ] ; if ( ++ lastPosition == interfacesToVisit . length ) System . arraycopy ( interfacesToVisit , 0 , interfacesToVisit = new ReferenceBinding [ lastPosition * 2 ] [ ] , 0 , lastPosition ) ; interfacesToVisit [ lastPosition ] = itsInterfaces ; } if ( ( currentType = currentType . superclass ( ) ) == null ) break ; if ( ( field = currentType . getField ( fieldName , true ) ) != null ) { keepLooking = false ; if ( canBeSeenByForCodeSnippet ( field , receiverType , invocationSite , this ) ) { if ( visibleField == null ) visibleField = field ; else return new ProblemFieldBinding ( visibleField , visibleField . declaringClass , fieldName , ProblemReasons . Ambiguous ) ; } else { notVisible = true ; } } } if ( interfacesToVisit != null ) { ProblemFieldBinding ambiguous = null ; org . eclipse . jdt . internal . compiler . util . SimpleSet interfacesSeen = new org . eclipse . jdt . internal . compiler . util . SimpleSet ( lastPosition * 2 ) ; done : for ( int i = 0 ; i <= lastPosition ; i ++ ) { ReferenceBinding [ ] interfaces = interfacesToVisit [ i ] ; for ( int j = 0 , length = interfaces . length ; j < length ; j ++ ) { ReferenceBinding anInterface = interfaces [ j ] ; if ( interfacesSeen . addIfNotIncluded ( anInterface ) == anInterface ) { if ( ( field = anInterface . getField ( fieldName , true ) ) != null ) { if ( visibleField == null ) { visibleField = field ; } else { ambiguous = new ProblemFieldBinding ( visibleField , visibleField . declaringClass , fieldName , ProblemReasons . Ambiguous ) ; break done ; } } else { ReferenceBinding [ ] itsInterfaces = anInterface . superInterfaces ( ) ; if ( itsInterfaces != Binding . NO_SUPERINTERFACES ) { if ( ++ lastPosition == interfacesToVisit . length ) System . arraycopy ( interfacesToVisit , 0 , interfacesToVisit = new ReferenceBinding [ lastPosition * 2 ] [ ] , 0 , lastPosition ) ; interfacesToVisit [ lastPosition ] = itsInterfaces ; } } } } } if ( ambiguous != null ) return ambiguous ; } if ( visibleField != null ) return visibleField ; if ( notVisible ) return new ProblemFieldBinding ( currentType , fieldName , ProblemReasons . NotVisible ) ; return null ; } public MethodBinding findMethod ( ReferenceBinding receiverType , char [ ] selector , TypeBinding [ ] argumentTypes , InvocationSite invocationSite ) { MethodBinding methodBinding = super . findMethod ( receiverType , selector , argumentTypes , invocationSite ) ; if ( methodBinding != null && methodBinding . isValidBinding ( ) ) if ( ! canBeSeenByForCodeSnippet ( methodBinding , receiverType , invocationSite , this ) ) return new ProblemMethodBinding ( methodBinding , selector , argumentTypes , ProblemReasons . NotVisible ) ; return methodBinding ; } public MethodBinding findMethodForArray ( ArrayBinding receiverType , char [ ] selector , TypeBinding [ ] argumentTypes , InvocationSite invocationSite ) { ReferenceBinding object = getJavaLangObject ( ) ; MethodBinding methodBinding = object . getExactMethod ( selector , argumentTypes , null ) ; if ( methodBinding != null ) { if ( argumentTypes == Binding . NO_PARAMETERS && CharOperation . equals ( selector , TypeConstants . CLONE ) ) return new MethodBinding ( ( methodBinding . modifiers & ~ ClassFileConstants . AccProtected ) | ClassFileConstants . AccPublic , TypeConstants . CLONE , methodBinding . returnType , argumentTypes , null , object ) ; if ( canBeSeenByForCodeSnippet ( methodBinding , receiverType , invocationSite , this ) ) return methodBinding ; } methodBinding = findMethod ( object , selector , argumentTypes , invocationSite ) ; if ( methodBinding == null ) return new ProblemMethodBinding ( selector , argumentTypes , ProblemReasons . NotFound ) ; if ( methodBinding . isValidBinding ( ) ) { MethodBinding compatibleMethod = computeCompatibleMethod ( methodBinding , argumentTypes , invocationSite ) ; if ( compatibleMethod == null ) return new ProblemMethodBinding ( methodBinding , selector , argumentTypes , ProblemReasons . NotFound ) ; methodBinding = compatibleMethod ; if ( ! canBeSeenByForCodeSnippet ( methodBinding , receiverType , invocationSite , this ) ) return new ProblemMethodBinding ( methodBinding , selector , methodBinding . parameters , ProblemReasons . NotVisible ) ; } return methodBinding ; } public Binding getBinding ( char [ ] [ ] compoundName , int mask , InvocationSite invocationSite , ReferenceBinding receiverType ) { Binding binding = getBinding ( compoundName [ 0 ] , mask | Binding . TYPE | Binding . PACKAGE , invocationSite , true ) ; invocationSite . setFieldIndex ( 1 ) ; if ( ! binding . isValidBinding ( ) || binding instanceof VariableBinding ) return binding ; int length = compoundName . length ; int currentIndex = 1 ; foundType : if ( binding instanceof PackageBinding ) { PackageBinding packageBinding = ( PackageBinding ) binding ; while ( currentIndex < length ) { binding = packageBinding . getTypeOrPackage ( compoundName [ currentIndex ++ ] ) ; invocationSite . setFieldIndex ( currentIndex ) ; if ( binding == null ) { if ( currentIndex == length ) return new ProblemReferenceBinding ( CharOperation . subarray ( compoundName , 0 , currentIndex ) , null , ProblemReasons . NotFound ) ; else return new ProblemBinding ( CharOperation . subarray ( compoundName , 0 , currentIndex ) , ProblemReasons . NotFound ) ; } if ( binding instanceof ReferenceBinding ) { if ( ! binding . isValidBinding ( ) ) return new ProblemReferenceBinding ( CharOperation . subarray ( compoundName , 0 , currentIndex ) , ( ReferenceBinding ) ( ( ReferenceBinding ) binding ) . closestMatch ( ) , binding . problemId ( ) ) ; if ( ! this . canBeSeenByForCodeSnippet ( ( ReferenceBinding ) binding , receiverType ) ) return new ProblemReferenceBinding ( CharOperation . subarray ( compoundName , 0 , currentIndex ) , ( ReferenceBinding ) binding , ProblemReasons . NotVisible ) ; break foundType ; } packageBinding = ( PackageBinding ) binding ; } return new ProblemReferenceBinding ( CharOperation . subarray ( compoundName , 0 , currentIndex ) , null , ProblemReasons . NotFound ) ; } while ( currentIndex < length ) { ReferenceBinding typeBinding = ( ReferenceBinding ) binding ; char [ ] nextName = compoundName [ currentIndex ++ ] ; invocationSite . setFieldIndex ( currentIndex ) ; if ( ( binding = findFieldForCodeSnippet ( typeBinding , nextName , invocationSite ) ) != null ) { if ( ! binding . isValidBinding ( ) ) { return new ProblemFieldBinding ( ( FieldBinding ) binding , ( ( FieldBinding ) binding ) . declaringClass , CharOperation . concatWith ( CharOperation . subarray ( compoundName , 0 , currentIndex ) , '.' ) , binding . problemId ( ) ) ; } break ; } if ( ( binding = findMemberType ( nextName , typeBinding ) ) == null ) return new ProblemBinding ( CharOperation . subarray ( compoundName , 0 , currentIndex ) , typeBinding , ProblemReasons . NotFound ) ; if ( ! binding . isValidBinding ( ) ) return new ProblemReferenceBinding ( CharOperation . subarray ( compoundName , 0 , currentIndex ) , ( ReferenceBinding ) ( ( ReferenceBinding ) binding ) . closestMatch ( ) , binding . problemId ( ) ) ; } if ( ( mask & Binding . FIELD ) != 0 && ( binding instanceof FieldBinding ) ) { FieldBinding field = ( FieldBinding ) binding ; if ( ! field . isStatic ( ) ) { return new ProblemFieldBinding ( field , field . declaringClass , CharOperation . concatWith ( CharOperation . subarray ( compoundName , 0 , currentIndex ) , '.' ) , ProblemReasons . NonStaticReferenceInStaticContext ) ; } return binding ; } if ( ( mask & Binding . TYPE ) != 0 && ( binding instanceof ReferenceBinding ) ) { return binding ; } return new ProblemBinding ( CharOperation . subarray ( compoundName , 0 , currentIndex ) , ProblemReasons . NotFound ) ; } public MethodBinding getConstructor ( ReferenceBinding receiverType , TypeBinding [ ] argumentTypes , InvocationSite invocationSite ) { MethodBinding methodBinding = receiverType . getExactConstructor ( argumentTypes ) ; if ( methodBinding != null ) { if ( canBeSeenByForCodeSnippet ( methodBinding , receiverType , invocationSite , this ) ) { return methodBinding ; } } MethodBinding [ ] methods = receiverType . getMethods ( TypeConstants . INIT ) ; if ( methods == Binding . NO_METHODS ) { return new ProblemMethodBinding ( TypeConstants . INIT , argumentTypes , ProblemReasons . NotFound ) ; } MethodBinding [ ] compatible = new MethodBinding [ methods . length ] ; int compatibleIndex = 0 ; for ( int i = 0 , length = methods . length ; i < length ; i ++ ) { MethodBinding compatibleMethod = computeCompatibleMethod ( methods [ i ] , argumentTypes , invocationSite ) ; if ( compatibleMethod != null ) compatible [ compatibleIndex ++ ] = compatibleMethod ; } if ( compatibleIndex == 0 ) return new ProblemMethodBinding ( TypeConstants . INIT , argumentTypes , ProblemReasons . NotFound ) ; MethodBinding [ ] visible = new MethodBinding [ compatibleIndex ] ; int visibleIndex = 0 ; for ( int i = 0 ; i < compatibleIndex ; i ++ ) { MethodBinding method = compatible [ i ] ; if ( canBeSeenByForCodeSnippet ( method , receiverType , invocationSite , this ) ) { visible [ visibleIndex ++ ] = method ; } } if ( visibleIndex == 1 ) { return visible [ 0 ] ; } if ( visibleIndex == 0 ) { return new ProblemMethodBinding ( compatible [ 0 ] , TypeConstants . INIT , compatible [ 0 ] . parameters , ProblemReasons . NotVisible ) ; } return mostSpecificClassMethodBinding ( visible , visibleIndex , invocationSite ) ; } public FieldBinding getFieldForCodeSnippet ( TypeBinding receiverType , char [ ] fieldName , InvocationSite invocationSite ) { FieldBinding field = findFieldForCodeSnippet ( receiverType , fieldName , invocationSite ) ; if ( field == null ) return new ProblemFieldBinding ( receiverType instanceof ReferenceBinding ? ( ReferenceBinding ) receiverType : null , fieldName , ProblemReasons . NotFound ) ; else return field ; } public MethodBinding getImplicitMethod ( ReferenceBinding receiverType , char [ ] selector , TypeBinding [ ] argumentTypes , InvocationSite invocationSite ) { MethodBinding methodBinding = findExactMethod ( receiverType , selector , argumentTypes , invocationSite ) ; if ( methodBinding == null ) methodBinding = findMethod ( receiverType , selector , argumentTypes , invocationSite ) ; if ( methodBinding != null ) { if ( methodBinding . isValidBinding ( ) ) if ( ! canBeSeenByForCodeSnippet ( methodBinding , receiverType , invocationSite , this ) ) return new ProblemMethodBinding ( methodBinding , selector , argumentTypes , ProblemReasons . NotVisible ) ; return methodBinding ; } return new ProblemMethodBinding ( selector , argumentTypes , ProblemReasons . NotFound ) ; } } </s>