Skip to content

Commit

Permalink
Added GSON test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Strum355 committed Sep 2, 2020
1 parent a58547f commit 4ebe590
Show file tree
Hide file tree
Showing 224 changed files with 39,348 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/java/spoon/reflect/factory/TypeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -565,9 +565,9 @@ public <T> CtType<T> get(Class<?> cl) {
} catch (Throwable e) {
throw new SpoonClassNotFoundException("cannot create shadow class: " + cl.getName(), e);
}
if (newShadowClass == null) {
/* if (newShadowClass == null) {
return null;
}
} */
newShadowClass.setFactory(factory);
newShadowClass.accept(new CtScanner() {
@Override
Expand Down
59 changes: 59 additions & 0 deletions src/test/java/spoon/test/factory/TypeFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,31 @@
import com.mysema.query.types.expr.ComparableExpressionBase;
import org.junit.Test;
import spoon.Launcher;
import spoon.MavenLauncher;
import spoon.reflect.code.CtFieldRead;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.code.CtJavaDoc;
import spoon.reflect.code.CtVariableRead;
import spoon.reflect.cu.position.NoSourcePosition;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtParameter;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
import spoon.reflect.factory.TypeFactory;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.CtScanner;
import spoon.test.factory.testclasses3.Cooking;
import spoon.test.factory.testclasses3.Prepare;
import spoon.testing.utils.ModelUtils;

import java.io.File;
import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
Expand All @@ -56,6 +70,51 @@ public void testCreateTypeRef() {
assertEquals("spoon.reflect.code.CtComment$CommentType", ctTypeReference.getQualifiedName());
}

@Test
public void gsonTestCase() throws ClassNotFoundException {
String projectRoot = "./src/test/resources/gsontest";
Launcher launcher = new MavenLauncher(projectRoot, MavenLauncher.SOURCE_TYPE.ALL_SOURCE);
launcher.getEnvironment().setIgnoreDuplicateDeclarations(true);
launcher.getEnvironment().setComplianceLevel(9);
launcher.buildModel();

Map<String, CtElement> typeByFile = new HashMap<>();
for (CtElement el : launcher.getModel().getAllTypes()) {
typeByFile.put(el.getPosition().getFile().getPath(), el);
}

Map<String, DocumentIndexer> indexers = new HashMap<>();
for (Map.Entry<String, CtElement> pathname : typeByFile.entrySet()) {
indexers.put(pathname.getKey(), new DocumentIndexer(pathname.getValue()));
}

for (DocumentIndexer indexer : indexers.values()) {
indexer.visitReferences();
}
}

public class DocumentIndexer {
private CtElement spoonElement;

public DocumentIndexer(CtElement spoonElement) {
this.spoonElement = spoonElement;
}

public void visitReferences() {
this.spoonElement.accept(new ReferencesVisitor());
}

private class ReferencesVisitor extends CtScanner {
@Override
public <T> void visitCtVariableRead(CtVariableRead<T> el) {
super.visitCtVariableRead(el);
if (el.getVariable().getDeclaration() == null) {
return;
}
}
}
}

@Test
public void reflectionAPI() {
// Spoon can be used as reflection API
Expand Down
22 changes: 22 additions & 0 deletions src/test/resources/gsontest/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.classpath
.project
.settings
eclipsebin
target
*/target
pom.xml.*
release.properties

.idea
*.iml
*.ipr
*.iws
classes

.gradle
local.properties
build

.DS_Store

examples/android-proguard-example/gen
182 changes: 182 additions & 0 deletions src/test/resources/gsontest/codegen/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.code.gson</groupId>
<artifactId>gson-codegen</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<inceptionYear>2008</inceptionYear>
<name>Gson Code Gen</name>
<parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>7</version>
</parent>
<url>http://code.google.com/p/google-gson/</url>
<description>Google Gson grab bag of utilities, type adapters, etc.</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<scm>
<connection>scm:svn:http://google-gson.googlecode.com/svn/trunk/extras</connection>
<developerConnection>scm:svn:https://google-gson.googlecode.com/svn/trunk/extras</developerConnection>
<url>http://google-gson.codegoogle.com/svn/trunk/extras</url>
</scm>
<issueManagement>
<system>Google Code Issue Tracking</system>
<url>http://code.google.com/p/google-gson/issues/list</url>
</issueManagement>
<organization>
<name>Google, Inc.</name>
<url>http://www.google.com</url>
</organization>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<!-- Activate PGP signing only when performing a release -->
<profile>
<id>release-sign-artifacts</id>
<activation>
<property>
<name>performRelease</name>
<value>true</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<build>
<defaultGoal>package</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument>-proc:none</compilerArgument>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<links>
<link>http://download.oracle.com/javase/1.5.0/docs/api/</link>
</links>
<version>true</version>
<show>public</show>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
<workspace>
../eclipse-ws/
</workspace>
<workspaceCodeStylesURL>
file:///${basedir}/../lib/gson-formatting-styles.xml
</workspaceCodeStylesURL>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<!-- version>2.3.2</version -->
<configuration>
<arguments>-DenableCiProfile=true</arguments>
<tagBase>https://google-gson.googlecode.com/svn/tags/</tagBase>
</configuration>
</plugin>
</plugins>
</build>
<developers>
<developer>
<name>Inderjeet Singh</name>
<organization>Trymph Inc.</organization>
</developer>
<developer>
<name>Joel Leitch</name>
<organization>Google Inc.</organization>
</developer>
<developer>
<name>Jesse Wilson</name>
<organization>Square Inc.</organization>
</developer>
</developers>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2012 Square, Inc.
*
* Licensed 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 com.google.gson.codegen;

import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;

public class CodeGen {
private CodeGen() {
}

public static PackageElement getPackage(Element type) {
while (type.getKind() != ElementKind.PACKAGE) {
type = type.getEnclosingElement();
}
return (PackageElement) type;
}

/**
* Returns a fully qualified class name to complement {@code type}.
*/
public static String adapterName(TypeElement typeElement, String suffix) {
StringBuilder builder = new StringBuilder();
rawTypeToString(builder, typeElement, '$');
builder.append(suffix);
return builder.toString();
}

static void rawTypeToString(StringBuilder result, TypeElement type, char innerClassSeparator) {
String packageName = getPackage(type).getQualifiedName().toString();
String qualifiedName = type.getQualifiedName().toString();
result.append(packageName);
result.append('.');
result.append(
qualifiedName.substring(packageName.length() + 1).replace('.', innerClassSeparator));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (C) 2012 Google Inc.
*
* Licensed 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 com.google.gson.codegen;

public @interface GeneratedTypeAdapter {
Class<?>[] value() default {};
}
Loading

0 comments on commit 4ebe590

Please sign in to comment.