You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by tv...@apache.org on 2010/08/30 20:49:17 UTC

svn commit: r990893 - /pivot/trunk/core/src/org/apache/pivot/util/TypeLiteral.java

Author: tvolkert
Date: Mon Aug 30 18:49:17 2010
New Revision: 990893

URL: http://svn.apache.org/viewvc?rev=990893&view=rev
Log:
Added TypeLiteral class

Added:
    pivot/trunk/core/src/org/apache/pivot/util/TypeLiteral.java

Added: pivot/trunk/core/src/org/apache/pivot/util/TypeLiteral.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/util/TypeLiteral.java?rev=990893&view=auto
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/util/TypeLiteral.java (added)
+++ pivot/trunk/core/src/org/apache/pivot/util/TypeLiteral.java Mon Aug 30 18:49:17 2010
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pivot.util;
+
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+
+/**
+ * Represents a generic type {@code T}. Java doesn't yet provide a way to
+ * represent generic types, so this class does. Clients create a subclass
+ * of this class, which enables retrieval the type information even at runtime.
+ * <p>
+ * For example, to get a reference to a generic type {@code List<String>}, you
+ * create an empty anonymous inner class, like so:
+ * <p>
+ * {@code Type genericType = (new TypeLiteral<List<String>>() {}).getType();}
+ * <p>
+ * This class is a drastically reduced derivation from
+ * <a href="http://code.google.com/p/google-guice/">Google Guice</a>'s
+ * {@code TypeLiteral} class, written by Bob Lee and Jesse Wilson.
+ */
+public class TypeLiteral<T> {
+    final Type type;
+
+    /**
+     * Constructs a new type literal. Derives represented class from type
+     * parameter.
+     * <p>
+     * Clients create an empty anonymous subclass. Doing so embeds the type
+     * parameter in the anonymous class's type hierarchy so we can reconstitute it
+     * at runtime despite erasure.
+     */
+    protected TypeLiteral() {
+        Type superclass = getClass().getGenericSuperclass();
+        if (superclass instanceof Class) {
+            throw new RuntimeException("Missing type parameter.");
+        }
+
+        ParameterizedType parameterizedType = (ParameterizedType)superclass;
+        this.type = parameterizedType.getActualTypeArguments()[0];
+    }
+
+    /**
+     * Gets underlying {@code Type} instance.
+     */
+    public final Type getType() {
+        return type;
+    }
+}



Re: svn commit: r990893 - /pivot/trunk/core/src/org/apache/pivot/util/TypeLiteral.java

Posted by Todd Volkert <tv...@gmail.com>.
Not from what I can tell because Guice is licensed with the ASL 2.0.
I re-read the apache license when I checked this is, and it says that
if the other code has a NOTICE file, we need to include their NOTICE
file (but in this case, there is no such file in Guice).  It also says
that we need to clearly and prominently state that we modified the
file and whence it came, which I did in the class Javadoc.

-T

On Tue, Aug 31, 2010 at 10:31 AM, Sandro Martini
<sa...@gmail.com> wrote:
>
> Hi Greg,
>
>> Do we need to add this to the NOTICE file?
> I think yes (also if it's a very small part of code), as shown also here
> under "Treatment of Third-Party Works ":
> http://www.apache.org/legal/src-headers.html
>
> Bye,
> Sandro
>
> --
> View this message in context: http://apache-pivot-developers.417237.n3.nabble.com/Re-svn-commit-r990893-pivot-trunk-core-src-org-apache-pivot-util-TypeLiteral-java-tp1390641p1394554.html
> Sent from the Apache Pivot - Developers mailing list archive at Nabble.com.
>

Re: svn commit: r990893 - /pivot/trunk/core/src/org/apache/pivot/util/TypeLiteral.java

Posted by Sandro Martini <sa...@gmail.com>.
Hi Greg,

> Do we need to add this to the NOTICE file?
I think yes (also if it's a very small part of code), as shown also here
under "Treatment of Third-Party Works ": 
http://www.apache.org/legal/src-headers.html

Bye,
Sandro

-- 
View this message in context: http://apache-pivot-developers.417237.n3.nabble.com/Re-svn-commit-r990893-pivot-trunk-core-src-org-apache-pivot-util-TypeLiteral-java-tp1390641p1394554.html
Sent from the Apache Pivot - Developers mailing list archive at Nabble.com.

Re: svn commit: r990893 - /pivot/trunk/core/src/org/apache/pivot/util/TypeLiteral.java

Posted by Greg Brown <gk...@mac.com>.
Do we need to add this to the NOTICE file?

On Aug 30, 2010, at 2:49 PM, tvolkert@apache.org wrote:

> Author: tvolkert
> Date: Mon Aug 30 18:49:17 2010
> New Revision: 990893
> 
> URL: http://svn.apache.org/viewvc?rev=990893&view=rev
> Log:
> Added TypeLiteral class
> 
> Added:
>    pivot/trunk/core/src/org/apache/pivot/util/TypeLiteral.java
> 
> Added: pivot/trunk/core/src/org/apache/pivot/util/TypeLiteral.java
> URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/util/TypeLiteral.java?rev=990893&view=auto
> ==============================================================================
> --- pivot/trunk/core/src/org/apache/pivot/util/TypeLiteral.java (added)
> +++ pivot/trunk/core/src/org/apache/pivot/util/TypeLiteral.java Mon Aug 30 18:49:17 2010
> @@ -0,0 +1,63 @@
> +/*
> + * Licensed to the Apache Software Foundation (ASF) under one or more
> + * contributor license agreements.  See the NOTICE file distributed with
> + * this work for additional information regarding copyright ownership.
> + * The ASF licenses this file to you under the Apache License,
> + * Version 2.0 (the "License"); you may not use this file except in
> + * compliance with the License.  You may obtain a copy of the License at
> + *
> + *     http://www.apache.org/licenses/LICENSE-2.0
> + *
> + * Unless required by applicable law or agreed to in writing, software
> + * distributed under the License is distributed on an "AS IS" BASIS,
> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> + * See the License for the specific language governing permissions and
> + * limitations under the License.
> + */
> +package org.apache.pivot.util;
> +
> +import java.lang.reflect.ParameterizedType;
> +import java.lang.reflect.Type;
> +
> +/**
> + * Represents a generic type {@code T}. Java doesn't yet provide a way to
> + * represent generic types, so this class does. Clients create a subclass
> + * of this class, which enables retrieval the type information even at runtime.
> + * <p>
> + * For example, to get a reference to a generic type {@code List<String>}, you
> + * create an empty anonymous inner class, like so:
> + * <p>
> + * {@code Type genericType = (new TypeLiteral<List<String>>() {}).getType();}
> + * <p>
> + * This class is a drastically reduced derivation from
> + * <a href="http://code.google.com/p/google-guice/">Google Guice</a>'s
> + * {@code TypeLiteral} class, written by Bob Lee and Jesse Wilson.
> + */
> +public class TypeLiteral<T> {
> +    final Type type;
> +
> +    /**
> +     * Constructs a new type literal. Derives represented class from type
> +     * parameter.
> +     * <p>
> +     * Clients create an empty anonymous subclass. Doing so embeds the type
> +     * parameter in the anonymous class's type hierarchy so we can reconstitute it
> +     * at runtime despite erasure.
> +     */
> +    protected TypeLiteral() {
> +        Type superclass = getClass().getGenericSuperclass();
> +        if (superclass instanceof Class) {
> +            throw new RuntimeException("Missing type parameter.");
> +        }
> +
> +        ParameterizedType parameterizedType = (ParameterizedType)superclass;
> +        this.type = parameterizedType.getActualTypeArguments()[0];
> +    }
> +
> +    /**
> +     * Gets underlying {@code Type} instance.
> +     */
> +    public final Type getType() {
> +        return type;
> +    }
> +}
> 
>