Constants for Android-Types

Added Android-Specific types as TypeName and TypeReference
This commit is contained in:
Tobias Blaschke 2014-02-02 17:06:35 +01:00 committed by Juergen Graf
parent f8970f92b7
commit b8b1fca292
2 changed files with 415 additions and 0 deletions

View File

@ -0,0 +1,251 @@
/*
* Copyright (c) 2013,
* Tobias Blaschke <code@tobiasblaschke.de>
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The names of the contributors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.ibm.wala.dalvik.util;
import com.ibm.wala.classLoader.IMethod;
import com.ibm.wala.ipa.cha.IClassHierarchy;
import com.ibm.wala.types.MethodReference;
import com.ibm.wala.types.TypeName;
import com.ibm.wala.types.TypeReference;
import com.ibm.wala.classLoader.IClass;
import com.ibm.wala.types.ClassLoaderReference;
import com.ibm.wala.util.strings.StringStuff;
import com.ibm.wala.util.strings.Atom;
import java.util.Comparator;
/**
* Android Components like Activity, Service, ...
*
* @author Tobias Blaschke <code@tobiasblaschke.de>
* @since 2013-10-16
*/
public enum AndroidComponent {
APPLICATION(AndroidTypes.Application, "Application"),
ACTIVITY(AndroidTypes.Activity, "Activity"),
FRAGMENT(AndroidTypes.Fragment, "Fragment"),
SERVICE(AndroidTypes.Service, "Service"),
INTENT_SERVICE(AndroidTypes.IntentService, "IntentService"),
ABSTRACT_INPUT_METHOD_SERVICE(AndroidTypes.AbstractInputMethodService, "AbstractInputMethodService"),
ACCESSIBILITY_SERVICE(AndroidTypes.AccessibilityService, "AccessibilityService"),
DREAM_SERVICE(AndroidTypes.DreamService, "DreamService"),
HOST_APDU_SERVICE(AndroidTypes.HostApduService, "HostApduService"),
MEDIA_ROUTE_PROVIDER_SERVICE(AndroidTypes.MediaRouteProviderService, "MediaRouteProviderService"),
NOTIFICATION_LISTENER_SERVICE(AndroidTypes.NotificationListenerService, "NotificationListenerService"),
OFF_HOST_APDU_SERVICE(AndroidTypes.OffHostApduService, "OffHostApduService"),
PRINT_SERVICE(AndroidTypes.PrintService, "PrintService"),
RECOGNITION_SERVICE(AndroidTypes.RecognitionService, "RecognitionService"),
REMOTE_VIEWS_SERVICE(AndroidTypes.RemoteViewsService, "RemoteViewsService"),
SETTING_INJECTOR_SERVICE(AndroidTypes.SettingInjectorService, "SettingInjectorService"),
SPELL_CHECKER_SERVICE(AndroidTypes.SpellCheckerService, "SpellCheckerService"),
TEXT_TO_SPEECH_SERVICE(AndroidTypes.TextToSpeechService, "TextToSpeechService"),
VPN_SERVICE(AndroidTypes.VpnService, "VpnService"),
WALLPAPER_SERVICE(AndroidTypes.WallpaperService, "WallpaperService"),
INPUT_METHOD_SERVICE(AndroidTypes.InputMethodService, "InputMethodService"),
PROVIDER(AndroidTypes.ContentProvider, "ContentProvider"),
BROADCAST_RECEIVER(AndroidTypes.BroadcastReceiver, "BroadcastReceiver"),
// Additional CallBacks:
LOADER_CB("Landroid/app/LoaderManager/LoaderCallbacks", "CallBackFromLoader"),
RESOLVER (AndroidTypes.ContentResolver, "ContentResolver"),
CONTEXT (AndroidTypes.Context, "Context"),
HTTP ("Landroid/net/AndroidHttpClient", "AndroidHttpClient"),
BINDER (AndroidTypes.IBinder, "IBinder"),
LOCATION_MGR ("Landroid/location/LocationManager", "LocationManager"),
TELEPHONY ("Landroid/telephony/TelephonyManager", "TelephonyManager"),
SMS ("Landroid/telephony/SmsManager", "SmsManager"),
SMS_GSM ("Landroid/telephony/gsm/SmsManager", "GsmSmsManager"),
LOCATION_LISTENER ("Landroid/location/LocationListener", "LocationListener"),
GPS_LISTENER ("Landroid/location/GpsStatus$Listener", "GpsStatusListener"),
GPS_NMEA_LISTENER ("Landroid/location/GpsStatus$NmeaListener", "GpsNmeaListener"),
UNKNOWN((String)null, "NULL");
private TypeReference tRef;
private final TypeName type;
private final String prettyName;
AndroidComponent(TypeReference type, final String prettyName) {
this.tRef = type;
this.type = type.getName();
this.prettyName = prettyName;
}
AndroidComponent(final String type, final String prettyName) {
if (prettyName.contains(".") || prettyName.contains("/") || prettyName.contains("$") ||
prettyName.contains(" ") || prettyName.contains("\t")) {
throw new IllegalArgumentException("The prettyName may not contain one of the reserved characters " +
"., /, $ or whitespace. The given name was " + prettyName);
}
this.tRef = null;
this.prettyName = prettyName;
if (type != null) {
this.type = TypeName.findOrCreate(type);
} else {
this.type = null;
}
}
/**
* A name usable for display-output.
*
* The name returned by this function may not necessarily as 'basename' for the class.
*/
public String getPrettyName() {
return this.prettyName;
}
/**
* The TypeName associated to the component.
*/
public TypeName getName() {
return this.type;
}
/**
* Generates a TypeReference for the component.
*/
/* package private */ TypeReference toReference() {
if (this.tRef != null ) {
return this.tRef;
} else if (this.type == null) {
return null;
} else {
this.tRef = TypeReference.find(ClassLoaderReference.Primordial, this.type);
if (this.tRef == null) {
/* { DEBUG
System.out.println("AndroidComponent WARNING: TypeReference.find did not resolve " + this.type.toString());
} // */
this.tRef = TypeReference.findOrCreate(ClassLoaderReference.Primordial, this.type);
}
if (this.tRef == null) {
throw new IllegalStateException("Unable to resolve " + this.type.toString() + " in Primordial-Loader. " +
"Perhaps the Android-Stubs used are the wrong ones: The analysis needs a view more functions, " +
"than the Vanilla-Stubs offer. A script 'stubsBuilder.sh' should have been bundled to generate " +
"these.");
}
return this.tRef;
}
}
/**
* Returns the Element the type matches exactly the given type.
*
* @return The Element if found or AndroidComponent.UNKNOWN if not
*/
public static AndroidComponent explicit(final TypeName type) {
for (AndroidComponent test: AndroidComponent.values()) {
if (type.equals(test.type)) {
return test;
}
}
return UNKNOWN;
}
/**
* Returns the Element the type matches exactly the given type.
*
* @return The Element if found or AndroidComponent.UNKNOWN if not
*/
public static AndroidComponent explicit(final TypeReference type) {
return explicit(type.getName());
}
/**
* Returns the Element the type matches exactly the given type.
*
* @return The Element if found or AndroidComponent.UNKNOWN if not
*/
public static AndroidComponent explicit(String type) {
if (!(type.startsWith("L") || type.contains("/"))) {
type = StringStuff.deployment2CanonicalTypeString(type);
}
return explicit(TypeName.findOrCreate(type));
}
/**
* Return the Item that is a matching superclass.
*
* For example returns AndroidComponent.ACTIVITY for 'LauncherActivity'
*
* @return the corresponding Enum-Element or AndroidComponent.UNKNOWN
*/
public static AndroidComponent from(final IClass type, final IClassHierarchy cha) {
for (AndroidComponent test: AndroidComponent.values()) {
if (cha.isSubclassOf(type, cha.lookupClass(test.toReference()))) {
return test;
}
}
return UNKNOWN;
}
/**
* Returns the AndroidComponent the method is declared in.
*
* @return the corresponding Enum-Element or AndroidComponent.UNKNOWN
*/
public static AndroidComponent from(final IMethod method, final IClassHierarchy cha) {
IClass type = method.getDeclaringClass();
if (type == null) {
throw new IllegalStateException("Unable to retreive the declaring class of " + method);
}
for (AndroidComponent test: AndroidComponent.values()) {
if (test.equals(AndroidComponent.UNKNOWN)) continue;
final TypeReference testRef = test.toReference();
if (testRef == null) {
continue; // Happens when the Android-Stubs are to old
}
final IClass testClass = cha.lookupClass(testRef);
if (testClass == null) {
continue; // Happens when the Android-Stubs are to old
}
if (testClass.isInterface()) {
if (cha.isAssignableFrom(testClass, type)) {
if (testClass.getMethod(method.getSelector()) != null) {
return test;
}
}
} else {
if (cha.isSubclassOf(type, testClass)) {
if (testClass.getMethod(method.getSelector()) != null) {
return test;
}
}
}
}
return UNKNOWN;
}
}

View File

@ -0,0 +1,164 @@
/*
* Copyright (c) 2013,
* Tobias Blaschke <code@tobiasblaschke.de>
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The names of the contributors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.ibm.wala.dalvik.util;
import com.ibm.wala.types.TypeReference;
import com.ibm.wala.types.TypeName;
import com.ibm.wala.types.ClassLoaderReference;
/**
* Constants for types used by the AndroidModel
*
* @author Tobias Blaschke <code@tobiasblaschke.de>
*/
public final class AndroidTypes {
public static final TypeName IntentName = TypeName.string2TypeName("Landroid/content/Intent");
public static final TypeReference Intent = TypeReference.findOrCreate(ClassLoaderReference.Primordial, IntentName);
public static final TypeName ApplicationName = TypeName.string2TypeName("Landroid/app/Application");
public static final TypeReference Application = TypeReference.findOrCreate(ClassLoaderReference.Primordial, ApplicationName);
public static final TypeName ActivityName = TypeName.string2TypeName("Landroid/app/Activity");
public static final TypeReference Activity = TypeReference.findOrCreate(ClassLoaderReference.Primordial, ActivityName);
public static final TypeName FragmentName = TypeName.string2TypeName("Landroid/app/Fragment");
public static final TypeReference Fragment = TypeReference.findOrCreate(ClassLoaderReference.Primordial, FragmentName);
public static final TypeName ServiceName = TypeName.string2TypeName("Landroid/app/Service");
public static final TypeReference Service = TypeReference.findOrCreate(ClassLoaderReference.Primordial, ServiceName);
public static final TypeName IntentServiceName = TypeName.string2TypeName("Landroid/app/IntentService");
public static final TypeReference IntentService = TypeReference.findOrCreate(ClassLoaderReference.Primordial, IntentServiceName);
public static final TypeName AbstractInputMethodServiceName = TypeName.string2TypeName("Landroid/inputmethodservice/AbstractInputMethodService");
public static final TypeReference AbstractInputMethodService = TypeReference.findOrCreate(ClassLoaderReference.Primordial, AbstractInputMethodServiceName);
public static final TypeName AccessibilityServiceName = TypeName.string2TypeName("Landroid/accessibilityservice/AccessibilityService");
public static final TypeReference AccessibilityService = TypeReference.findOrCreate(ClassLoaderReference.Primordial, AccessibilityServiceName);
public static final TypeName DreamServiceName = TypeName.string2TypeName("Landroid/service/dreams/DreamService");
public static final TypeReference DreamService = TypeReference.findOrCreate(ClassLoaderReference.Primordial, DreamServiceName);
public static final TypeName HostApduServiceName = TypeName.string2TypeName("Landroid/nfc/cardemulation/HostApduService");
public static final TypeReference HostApduService = TypeReference.findOrCreate(ClassLoaderReference.Primordial, HostApduServiceName);
public static final TypeName MediaRouteProviderServiceName = TypeName.string2TypeName("Landroid/support/v7/media/MediaRouteProviderService");
public static final TypeReference MediaRouteProviderService = TypeReference.findOrCreate(ClassLoaderReference.Primordial, MediaRouteProviderServiceName);
public static final TypeName NotificationListenerServiceName = TypeName.string2TypeName("Landroid/service/notification/NotificationListenerService");
public static final TypeReference NotificationListenerService = TypeReference.findOrCreate(ClassLoaderReference.Primordial, NotificationListenerServiceName);
public static final TypeName OffHostApduServiceName = TypeName.string2TypeName("Landroid/nfc/cardemulation/OffHostApduService");
public static final TypeReference OffHostApduService = TypeReference.findOrCreate(ClassLoaderReference.Primordial, OffHostApduServiceName);
public static final TypeName PrintServiceName = TypeName.string2TypeName("Landroid/printservice/PrintService");
public static final TypeReference PrintService = TypeReference.findOrCreate(ClassLoaderReference.Primordial, PrintServiceName);
public static final TypeName RecognitionServiceName = TypeName.string2TypeName("Landroid/speech/RecognitionService");
public static final TypeReference RecognitionService = TypeReference.findOrCreate(ClassLoaderReference.Primordial, RecognitionServiceName);
public static final TypeName RemoteViewsServiceName = TypeName.string2TypeName("Landroid/widget/RemoteViewsService");
public static final TypeReference RemoteViewsService = TypeReference.findOrCreate(ClassLoaderReference.Primordial, RemoteViewsServiceName);
public static final TypeName SettingInjectorServiceName = TypeName.string2TypeName("Landroid/location/SettingInjectorService");
public static final TypeReference SettingInjectorService = TypeReference.findOrCreate(ClassLoaderReference.Primordial, SettingInjectorServiceName);
public static final TypeName SpellCheckerServiceName = TypeName.string2TypeName("Landroid/service/textservice/SpellCheckerService");
public static final TypeReference SpellCheckerService = TypeReference.findOrCreate(ClassLoaderReference.Primordial, SpellCheckerServiceName);
public static final TypeName TextToSpeechServiceName = TypeName.string2TypeName("Landroid/speech/tts/TextToSpeechService");
public static final TypeReference TextToSpeechService = TypeReference.findOrCreate(ClassLoaderReference.Primordial, TextToSpeechServiceName);
public static final TypeName VpnServiceName = TypeName.string2TypeName("Landroid/net/VpnService");
public static final TypeReference VpnService = TypeReference.findOrCreate(ClassLoaderReference.Primordial, VpnServiceName);
public static final TypeName WallpaperServiceName = TypeName.string2TypeName("Landroid/service/wallpaper/WallpaperService");
public static final TypeReference WallpaperService = TypeReference.findOrCreate(ClassLoaderReference.Primordial, WallpaperServiceName);
public static final TypeName InputMethodServiceName = TypeName.string2TypeName("Landroid/inputmethodservice/InputMethodService");
public static final TypeReference InputMethodService = TypeReference.findOrCreate(ClassLoaderReference.Primordial, InputMethodServiceName);
public static final TypeName ContentProviderName = TypeName.string2TypeName("Landroid/content/ContentProvider");
public static final TypeReference ContentProvider = TypeReference.findOrCreate(ClassLoaderReference.Primordial, ContentProviderName);
public static final TypeName BroadcastReceiverName = TypeName.string2TypeName("Landroid/content/BroadcastReceiver");
public static final TypeReference BroadcastReceiver = TypeReference.findOrCreate(ClassLoaderReference.Primordial, BroadcastReceiverName);
public static final TypeName ContentResolverName = TypeName.string2TypeName("Landroid/content/ContentResolver");
public static final TypeReference ContentResolver = TypeReference.findOrCreate(ClassLoaderReference.Primordial, ContentResolverName);
public static final TypeName MenuName = TypeName.findOrCreate("Landroid/view/Menu");
public static final TypeReference Menu = TypeReference.findOrCreate(ClassLoaderReference.Primordial, MenuName);
public static final TypeName ContextMenuName = TypeName.findOrCreate("Landroid/view/ContextMenu");
public static final TypeReference ContextMenu = TypeReference.findOrCreate(ClassLoaderReference.Primordial, ContextMenuName);
public static final TypeName MenuItemName = TypeName.findOrCreate("Landroid/view/MenuItem");
public static final TypeReference MenuItem = TypeReference.findOrCreate(ClassLoaderReference.Primordial, MenuItemName);
public static final TypeName ActionModeName = TypeName.findOrCreate("Landroid/view/ActionMode");
public static final TypeReference ActionMode = TypeReference.findOrCreate(ClassLoaderReference.Primordial, ActionModeName);
public static final TypeName AttributeSetName = TypeName.findOrCreate("Landroid/util/AttributeSet");
public static final TypeReference AttributeSet = TypeReference.findOrCreate(ClassLoaderReference.Primordial, AttributeSetName);
public static final TypeName ActionModeCallbackName = TypeName.findOrCreate("Landroid/view/ActionMode$Callback");
public static final TypeReference ActionModeCallback = TypeReference.findOrCreate(ClassLoaderReference.Primordial, ActionModeCallbackName);
public static final TypeName BundleName = TypeName.findOrCreate("Landroid/os/Bundle");
public static final TypeReference Bundle = TypeReference.findOrCreate(ClassLoaderReference.Primordial, BundleName);
public static final TypeName IntentSenderName = TypeName.findOrCreate("Landroid/content/IntentSender");
public static final TypeReference IntentSender = TypeReference.findOrCreate(ClassLoaderReference.Primordial, IntentSenderName);
public static final TypeName IIntentSenderName = TypeName.findOrCreate("Landroid/content/IIntentSender");
public static final TypeReference IIntentSender = TypeReference.findOrCreate(ClassLoaderReference.Primordial, IntentSenderName);
public static final TypeName IBinderName = TypeName.findOrCreate("Landroid/os/IBinder");
public static final TypeReference IBinder = TypeReference.findOrCreate(ClassLoaderReference.Primordial, IBinderName);
public static final TypeName ActivityThreadName = TypeName.findOrCreate("Landroid/app/ActivityThread");
public static final TypeReference ActivityThread = TypeReference.findOrCreate(ClassLoaderReference.Primordial, ActivityThreadName);
public static final TypeName ContextName = TypeName.findOrCreate("Landroid/content/Context");
public static final TypeReference Context = TypeReference.findOrCreate(ClassLoaderReference.Primordial, ContextName);
public static final TypeName ContextWrapperName = TypeName.findOrCreate("Landroid/content/ContextWrapper");
public static final TypeReference ContextWrapper = TypeReference.findOrCreate(ClassLoaderReference.Primordial, ContextWrapperName);
public static final TypeName ContextImplName = TypeName.findOrCreate("Landroid/app/ContextImpl");
public static final TypeReference ContextImpl = TypeReference.findOrCreate(ClassLoaderReference.Primordial, ContextImplName);
public static final TypeName BridgeContextName = TypeName.findOrCreate("Lcom/android/layoutlib/bridge/android/BridgeContext");
public static final TypeReference BridgeContext = TypeReference.findOrCreate(ClassLoaderReference.Primordial, BridgeContextName);
public static final TypeName ContextThemeWrapperName = TypeName.findOrCreate("Landroid/view/ContextThemeWrapper");
public static final TypeReference ContextThemeWrapper = TypeReference.findOrCreate(ClassLoaderReference.Primordial, ContextThemeWrapperName);
public static final TypeName PolicyManagerName = TypeName.findOrCreate("Lcom/android/internal/policy/PolicyManager");
public static final TypeReference PolicyManager = TypeReference.findOrCreate(ClassLoaderReference.Primordial, PolicyManagerName);
public static final TypeName WindowName = TypeName.findOrCreate("Landroid/view/Window");
public static final TypeReference Window = TypeReference.findOrCreate(ClassLoaderReference.Primordial, WindowName);
public static final TypeName UserHandleName = TypeName.findOrCreate("Landroid/os/UserHandle");
public static final TypeReference UserHandle = TypeReference.findOrCreate(ClassLoaderReference.Primordial, UserHandleName);
public static final TypeName LoadedApkName = TypeName.findOrCreate("Landroid/app/LoadedApk");
public static final TypeReference LoadedApk = TypeReference.findOrCreate(ClassLoaderReference.Primordial, LoadedApkName);
public static final TypeName ResourcesName = TypeName.findOrCreate("Landroid/content/res/Resources");
public static final TypeReference Resources = TypeReference.findOrCreate(ClassLoaderReference.Primordial, ResourcesName);
public static final TypeName InstrumentationName = TypeName.findOrCreate("Landroid/app/Instrumentation");
public static final TypeReference Instrumentation = TypeReference.findOrCreate(ClassLoaderReference.Primordial, InstrumentationName);
public static final TypeName ActivityInfoName = TypeName.findOrCreate("Landroid/content/pm/ActivityInfo");
public static final TypeReference ActivityInfo = TypeReference.findOrCreate(ClassLoaderReference.Primordial, ActivityInfoName);
public static final TypeName ConfigurationName = TypeName.findOrCreate("Landroid/content/res/Configuration");
public static final TypeReference Configuration = TypeReference.findOrCreate(ClassLoaderReference.Primordial, ConfigurationName);
public enum AndroidContextType {
CONTEXT_IMPL,
CONTEXT_BRIDGE,
ACTIVITY,
/**
* For internal use during development
*/
USELESS
}
}