All Downloads are FREE. Search and download functionalities are using the official Maven repository.

twitter4jads.json.DataObjectFactory Maven / Gradle / Ivy

The newest version!
package twitter4jads.json;

import twitter4jads.internal.models4j.*;
import twitter4jads.internal.org.json.JSONArray;
import twitter4jads.internal.org.json.JSONException;
import twitter4jads.internal.org.json.JSONObject;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public final class DataObjectFactory {
    private DataObjectFactory() {
        throw new AssertionError("not intended to be instantiated.");
    }

    private static final Constructor statusConstructor;
    private static final Constructor userConstructor;
    private static final Constructor relationshipConstructor;
    private static final Constructor placeConstructor;
    private static final Constructor savedSearchConstructor;
    private static final Constructor trendConstructor;
    private static final Constructor trendsConstructor;
    private static final Constructor IDsConstructor;
    private static final Method rateLimitStatusConstructor;
    private static final Constructor categoryConstructor;
    private static final Constructor directMessageConstructor;
    private static final Constructor locationConstructor;
    private static final Constructor userListConstructor;
    private static final Constructor relatedResultsConstructor;
    private static final Constructor statusDeletionNoticeConstructor;
    private static final Constructor accountTotalsConstructor;
    private static final Constructor oembedConstructor;

    static {
        try {
            statusConstructor =
                    (Constructor) Class.forName("twitter4jads.internal.json.StatusJSONImpl").getDeclaredConstructor(JSONObject.class);
            statusConstructor.setAccessible(true);

            userConstructor = (Constructor) Class.forName("twitter4jads.internal.json.UserJSONImpl").getDeclaredConstructor(JSONObject.class);
            userConstructor.setAccessible(true);

            relationshipConstructor = (Constructor) Class.forName("twitter4jads.internal.json.RelationshipJSONImpl")
                    .getDeclaredConstructor(JSONObject.class);
            relationshipConstructor.setAccessible(true);

            placeConstructor = (Constructor) Class.forName("twitter4jads.internal.json.PlaceJSONImpl").getDeclaredConstructor(JSONObject.class);
            placeConstructor.setAccessible(true);

            savedSearchConstructor =
                    (Constructor) Class.forName("twitter4jads.internal.json.SavedSearchJSONImpl").getDeclaredConstructor(JSONObject.class);
            savedSearchConstructor.setAccessible(true);

            trendConstructor = (Constructor) Class.forName("twitter4jads.internal.json.TrendJSONImpl").getDeclaredConstructor(JSONObject.class);
            trendConstructor.setAccessible(true);

            trendsConstructor = (Constructor) Class.forName("twitter4jads.internal.json.TrendsJSONImpl").getDeclaredConstructor(String.class);
            trendsConstructor.setAccessible(true);

            IDsConstructor = (Constructor) Class.forName("twitter4jads.internal.json.IDsJSONImpl").getDeclaredConstructor(String.class);
            IDsConstructor.setAccessible(true);

            rateLimitStatusConstructor =
                    Class.forName("twitter4jads.internal.json.RateLimitStatusJSONImpl").getDeclaredMethod("createRateLimitStatuses", JSONObject.class);
            rateLimitStatusConstructor.setAccessible(true);

            categoryConstructor =
                    (Constructor) Class.forName("twitter4jads.internal.json.CategoryJSONImpl").getDeclaredConstructor(JSONObject.class);
            categoryConstructor.setAccessible(true);

            directMessageConstructor = (Constructor) Class.forName("twitter4jads.internal.json.DirectMessageJSONImpl")
                    .getDeclaredConstructor(JSONObject.class);
            directMessageConstructor.setAccessible(true);

            locationConstructor =
                    (Constructor) Class.forName("twitter4jads.internal.json.LocationJSONImpl").getDeclaredConstructor(JSONObject.class);
            locationConstructor.setAccessible(true);

            userListConstructor =
                    (Constructor) Class.forName("twitter4jads.internal.json.UserListJSONImpl").getDeclaredConstructor(JSONObject.class);
            userListConstructor.setAccessible(true);

            relatedResultsConstructor = (Constructor) Class.forName("twitter4jads.internal.json.RelatedResultsJSONImpl")
                    .getDeclaredConstructor(JSONArray.class);
            relatedResultsConstructor.setAccessible(true);

            statusDeletionNoticeConstructor =
                    (Constructor) Class.forName("twitter4jads.StatusDeletionNoticeImpl").getDeclaredConstructor(JSONObject.class);
            statusDeletionNoticeConstructor.setAccessible(true);

            accountTotalsConstructor = (Constructor) Class.forName("twitter4jads.internal.json.AccountTotalsJSONImpl")
                    .getDeclaredConstructor(JSONObject.class);
            accountTotalsConstructor.setAccessible(true);
            oembedConstructor =
                    (Constructor) Class.forName("twitter4jads.internal.json.OEmbedJSONImpl").getDeclaredConstructor(JSONObject.class);
            oembedConstructor.setAccessible(true);
        } catch (NoSuchMethodException e) {
            throw new ExceptionInInitializerError(e);
        } catch (ClassNotFoundException e) {
            throw new ExceptionInInitializerError(e);
        }
    }

    private static final ThreadLocal rawJsonMap = new ThreadLocal() {
        @Override
        protected Map initialValue() {
            return new HashMap();
        }
    };

    /**
     * Returns a raw JSON form of the provided object.
* Note that raw JSON forms can be retrieved only from the same thread invoked the last method call and will become inaccessible once another method call * * @param obj * @return raw JSON * @since Twitter4J 2.1.7 */ public static String getRawJSON(Object obj) { Object json = rawJsonMap.get().get(obj); if (json instanceof String) { return (String) json; } else if (json != null) { // object must be instance of JSONObject return json.toString(); } else { return null; } } /** * Constructs a Status object from rawJSON string. * * @param rawJSON raw JSON form as String * @return Status * @throws TwitterException when provided string is not a valid JSON string. * @since Twitter4J 2.1.7 */ public static Status createStatus(String rawJSON) throws TwitterException { try { JSONObject json = new JSONObject(rawJSON); return createStatus(json); } catch (JSONException e) { throw new TwitterException(e); } } public static Status createStatus(JSONObject json) throws TwitterException { try { return statusConstructor.newInstance(json); } catch (InstantiationException e) { throw new TwitterException(e); } catch (IllegalAccessException e) { throw new TwitterException(e); } catch (InvocationTargetException e) { throw new TwitterException(e); } } /** * Constructs a User object from rawJSON string. * * @param rawJSON raw JSON form as String * @return User * @throws TwitterException when provided string is not a valid JSON string. * @since Twitter4J 2.1.7 */ public static User createUser(String rawJSON) throws TwitterException { try { JSONObject json = new JSONObject(rawJSON); return createUser(json); } catch (JSONException e) { throw new TwitterException(e); } } public static User createUser(JSONObject json) throws TwitterException { try { return userConstructor.newInstance(json); } catch (InstantiationException e) { throw new TwitterException(e); } catch (IllegalAccessException e) { throw new TwitterException(e); } catch (InvocationTargetException e) { throw new TwitterException(e); } } /** * Constructs an AccountTotals object from rawJSON string. * * @param rawJSON raw JSON form as String * @return AccountTotals * @throws TwitterException when provided string is not a valid JSON string. * @since Twitter4J 2.1.9 */ public static AccountTotals createAccountTotals(String rawJSON) throws TwitterException { try { JSONObject json = new JSONObject(rawJSON); return createAccountTotals(json); } catch (JSONException e) { throw new TwitterException(e); } } public static AccountTotals createAccountTotals(JSONObject json) throws TwitterException { try { return accountTotalsConstructor.newInstance(json); } catch (InstantiationException e) { throw new TwitterException(e); } catch (IllegalAccessException e) { throw new TwitterException(e); } catch (InvocationTargetException e) { throw new TwitterException(e); } } /** * Constructs a Relationship object from rawJSON string. * * @param rawJSON raw JSON form as String * @return Relationship * @throws TwitterException when provided string is not a valid JSON string. * @since Twitter4J 2.1.7 */ public static Relationship createRelationship(String rawJSON) throws TwitterException { try { JSONObject json = new JSONObject(rawJSON); return createRelationship(json); } catch (JSONException e) { throw new TwitterException(e); } } private static Relationship createRelationship(JSONObject json) throws TwitterException { try { return relationshipConstructor.newInstance(json); } catch (InstantiationException e) { throw new TwitterException(e); } catch (IllegalAccessException e) { throw new TwitterException(e); } catch (InvocationTargetException e) { throw new TwitterException(e); } } /** * Constructs a Place object from rawJSON string. * * @param rawJSON raw JSON form as String * @return Place * @throws TwitterException when provided string is not a valid JSON string. * @since Twitter4J 2.1.7 */ public static Place createPlace(String rawJSON) throws TwitterException { try { JSONObject json = new JSONObject(rawJSON); return createPlace(json); } catch (JSONException e) { throw new TwitterException(e); } } public static Place createPlace(JSONObject json) throws TwitterException { try { return placeConstructor.newInstance(json); } catch (InstantiationException e) { throw new TwitterException(e); } catch (IllegalAccessException e) { throw new TwitterException(e); } catch (InvocationTargetException e) { throw new TwitterException(e); } } /** * Constructs a SavedSearch object from rawJSON string. * * @param rawJSON raw JSON form as String * @return SavedSearch * @throws TwitterException when provided string is not a valid JSON string. * @since Twitter4J 2.1.7 */ public static SavedSearch createSavedSearch(String rawJSON) throws TwitterException { try { JSONObject json = new JSONObject(rawJSON); return createSavedSearch(json); } catch (JSONException e) { throw new TwitterException(e); } } public static SavedSearch createSavedSearch(JSONObject json) throws TwitterException { try { return savedSearchConstructor.newInstance(json); } catch (InstantiationException e) { throw new TwitterException(e); } catch (IllegalAccessException e) { throw new TwitterException(e); } catch (InvocationTargetException e) { throw new TwitterException(e); } } /** * Constructs a Trend object from rawJSON string. * * @param rawJSON raw JSON form as String * @return Trend * @throws TwitterException when provided string is not a valid JSON string. * @since Twitter4J 2.1.7 */ public static Trend createTrend(String rawJSON) throws TwitterException { try { JSONObject json = new JSONObject(rawJSON); return createTrend(json); } catch (JSONException e) { throw new TwitterException(e); } } public static Trend createTrend(JSONObject json) throws TwitterException { try { return trendConstructor.newInstance(json); } catch (InstantiationException e) { throw new TwitterException(e); } catch (IllegalAccessException e) { throw new TwitterException(e); } catch (InvocationTargetException e) { throw new TwitterException(e); } } /** * Constructs a Trends object from rawJSON string. * * @param rawJSON raw JSON form as String * @return Trends * @throws TwitterException when provided string is not a valid JSON string. * @since Twitter4J 2.1.7 */ public static Trends createTrends(String rawJSON) throws TwitterException { try { return trendsConstructor.newInstance(rawJSON); } catch (InstantiationException e) { throw new TwitterException(e); } catch (IllegalAccessException e) { throw new TwitterException(e); } catch (InvocationTargetException e) { throw new AssertionError(e); } } /** * Constructs a IDs object from rawJSON string. * * @param rawJSON raw JSON form as String * @return IDs * @throws TwitterException when provided string is not a valid JSON string. * @since Twitter4J 2.1.7 */ public static IDs createIDs(String rawJSON) throws TwitterException { try { return IDsConstructor.newInstance(rawJSON); } catch (InstantiationException e) { throw new TwitterException(e); } catch (IllegalAccessException e) { throw new AssertionError(e); } catch (InvocationTargetException e) { throw new TwitterException(e); } } /** * Constructs a RateLimitStatus object from rawJSON string. * * @param rawJSON raw JSON form as String * @return RateLimitStatus * @throws TwitterException when provided string is not a valid JSON string. * @since Twitter4J 2.1.7 */ public static Map createRateLimitStatus(String rawJSON) throws TwitterException { try { JSONObject json = new JSONObject(rawJSON); return (Map) rateLimitStatusConstructor .invoke(Class.forName("twitter4jads.internal.json.RateLimitStatusJSONImpl"), json); } catch (ClassNotFoundException e) { throw new TwitterException(e); } catch (IllegalAccessException e) { throw new AssertionError(e); } catch (InvocationTargetException e) { throw new TwitterException(e); } catch (JSONException e) { throw new TwitterException(e); } } /** * Constructs a Category object from rawJSON string. * * @param rawJSON raw JSON form as String * @return Category * @throws TwitterException when provided string is not a valid JSON string. * @since Twitter4J 2.1.7 */ public static Category createCategory(String rawJSON) throws TwitterException { try { JSONObject json = new JSONObject(rawJSON); return createCategory(json); } catch (JSONException e) { throw new TwitterException(e); } } public static Category createCategory(JSONObject json) throws TwitterException { try { return categoryConstructor.newInstance(json); } catch (InstantiationException e) { throw new TwitterException(e); } catch (IllegalAccessException e) { throw new TwitterException(e); } catch (InvocationTargetException e) { throw new TwitterException(e); } } /** * Constructs a DirectMessage object from rawJSON string. * * @param rawJSON raw JSON form as String * @return DirectMessage * @throws TwitterException when provided string is not a valid JSON string. * @since Twitter4J 2.1.7 */ public static DirectMessage createDirectMessage(String rawJSON) throws TwitterException { try { JSONObject json = new JSONObject(rawJSON); return createDirectMessage(json); } catch (JSONException e) { throw new TwitterException(e); } } public static DirectMessage createDirectMessage(JSONObject json) throws TwitterException { try { return directMessageConstructor.newInstance(json); } catch (InstantiationException e) { throw new TwitterException(e); } catch (IllegalAccessException e) { throw new TwitterException(e); } catch (InvocationTargetException e) { throw new TwitterException(e); } } /** * Constructs a Location object from rawJSON string. * * @param rawJSON raw JSON form as String * @return Location * @throws TwitterException when provided string is not a valid JSON string. * @since Twitter4J 2.1.7 */ public static Location createLocation(String rawJSON) throws TwitterException { try { JSONObject json = new JSONObject(rawJSON); return createLocation(json); } catch (JSONException e) { throw new TwitterException(e); } } public static Location createLocation(JSONObject json) throws TwitterException { try { return locationConstructor.newInstance(json); } catch (InstantiationException e) { throw new TwitterException(e); } catch (IllegalAccessException e) { throw new TwitterException(e); } catch (InvocationTargetException e) { throw new TwitterException(e); } } /** * Constructs a UserList object from rawJSON string. * * @param rawJSON raw JSON form as String * @return UserList * @throws TwitterException when provided string is not a valid JSON string. * @since Twitter4J 2.1.7 */ public static UserList createUserList(String rawJSON) throws TwitterException { try { JSONObject json = new JSONObject(rawJSON); return createUserList(json); } catch (JSONException e) { throw new TwitterException(e); } } public static UserList createUserList(JSONObject json) throws TwitterException { try { return userListConstructor.newInstance(json); } catch (InstantiationException e) { throw new TwitterException(e); } catch (IllegalAccessException e) { throw new TwitterException(e); } catch (InvocationTargetException e) { throw new TwitterException(e); } } /** * Constructs a RelatedResults object from rawJSON string. * * @param rawJSON raw JSON form as String * @return RelatedResults * @throws TwitterException when provided string is not a valid JSON string. * @since Twitter4J 2.1.8 */ public static RelatedResults createRelatedResults(String rawJSON) throws TwitterException { try { JSONArray json = new JSONArray(rawJSON); return createRelatedResults(json); } catch (JSONException e) { throw new TwitterException(e); } } public static RelatedResults createRelatedResults(JSONArray json) throws TwitterException { try { return relatedResultsConstructor.newInstance(json); } catch (InstantiationException e) { throw new TwitterException(e); } catch (IllegalAccessException e) { throw new TwitterException(e); } catch (InvocationTargetException e) { throw new TwitterException(e); } } /** * Constructs an OEmbed object from rawJSON string. * * @param rawJSON raw JSON form as String * @return OEmbed * @throws TwitterException when provided string is not a valid JSON string. * @since Twitter4J 3.0.2 */ public static OEmbed createOEmbed(String rawJSON) throws TwitterException { try { JSONObject json = new JSONObject(rawJSON); return createOEmbed(json); } catch (JSONException e) { throw new TwitterException(e); } } public static OEmbed createOEmbed(JSONObject json) throws TwitterException { try { return oembedConstructor.newInstance(json); } catch (InstantiationException e) { throw new TwitterException(e); } catch (IllegalAccessException e) { throw new TwitterException(e); } catch (InvocationTargetException e) { throw new TwitterException(e); } } /** * Construct an object from rawJSON string. This method may be called * when you do not know what a given raw JSON string contains. It will * do the work of determining what type of object the JSON represents, * and constructing the respective object type. For example, if the JSON * contents represents a Status, then a Status will be returned. If it * represents a deletion notice, then a StatusDeletionNotice will be * returned. The caller can simply use instanceof to handle the returned * object as applicable. * NOTE: the raw JSONObject will be returned in cases where there isn't * a discrete respective object type that can be constructed. That way, * the caller can at least have access to the JSON itself. * * @param rawJSON raw JSON form as String * @return the respective constructed object, or the JSONObject in the * case where we cannot determine the object type. * @throws TwitterException when provided string is not a valid JSON string. * @since Twitter4J 2.1.9 */ public static Object createObject(String rawJSON) throws TwitterException { try { JSONObject json = new JSONObject(rawJSON); JSONObjectType.Type jsonObjectType = JSONObjectType.determine(json); switch (jsonObjectType) { case SENDER: return registerJSONObject(directMessageConstructor.newInstance(json.getJSONObject("direct_message")), json); case STATUS: return registerJSONObject(statusConstructor.newInstance(json), json); case DIRECT_MESSAGE: return registerJSONObject(directMessageConstructor.newInstance(json.getJSONObject("direct_message")), json); case DELETE: return registerJSONObject(statusDeletionNoticeConstructor.newInstance(json.getJSONObject("delete").getJSONObject("status")), json); case LIMIT: // TODO: Perhaps there should be a TrackLimitationNotice object? // The onTrackLimitationNotice method could take that as an arg. return json; case SCRUB_GEO: return json; default: // The object type is unrecognized...just return the json return json; } } catch (InstantiationException e) { throw new TwitterException(e); } catch (IllegalAccessException e) { throw new AssertionError(e); } catch (InvocationTargetException e) { throw new TwitterException(e); } catch (JSONException e) { throw new TwitterException(e); } } /** * clear raw JSON forms associated with the current thread.
* Currently this method is called indirectly by twitter4jads.internal.util.DataObjectFactoryUtil, and should be called directly once *JSONImpl classes are migrated to twitter4jads.json.* package. * * @since Twitter4J 2.1.7 */ static void clearThreadLocalMap() { rawJsonMap.get().clear(); } /** * associate a raw JSON form to the current thread
* Currently this method is called indirectly by twitter4jads.internal.util.DataObjectFactoryUtil, and should be called directly once *JSONImpl classes are migrated to twitter4jads.json.* package. * * @since Twitter4J 2.1.7 */ static T registerJSONObject(T key, Object json) { rawJsonMap.get().put(key, json); return key; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy