Skip to content
This repository has been archived by the owner on Feb 6, 2019. It is now read-only.

Commit

Permalink
Merge pull request #279 from javaparser/fix_solving_local_types
Browse files Browse the repository at this point in the history
Fix solving local types
  • Loading branch information
ftomassetti committed Aug 3, 2017
2 parents 8d59ff6 + e1bf5fb commit d07c426
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.github.javaparser.ast.body.Parameter;
import com.github.javaparser.ast.nodeTypes.NodeWithParameters;
import com.github.javaparser.ast.nodeTypes.NodeWithTypeParameters;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFactory;
import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter;
import com.github.javaparser.symbolsolver.model.declarations.MethodDeclaration;
Expand Down Expand Up @@ -76,6 +77,19 @@ public final SymbolReference<TypeDeclaration> solveType(String name, TypeSolver
}
}
}

// Local types
List<com.github.javaparser.ast.body.TypeDeclaration> localTypes = wrappedNode.getChildNodesByType(
com.github.javaparser.ast.body.TypeDeclaration.class);
for (com.github.javaparser.ast.body.TypeDeclaration<?> localType : localTypes) {
if (localType.getName().getId().equals(name)) {
return SymbolReference.solved(JavaParserFacade.get(typeSolver).getTypeDeclaration(localType));
} else if (name.startsWith(String.format("%s.", localType.getName()))) {
return JavaParserFactory.getContext(localType, typeSolver).solveType(
name.substring(localType.getName().getId().length() + 1), typeSolver);
}
}

return getParent().solveType(name, typeSolver);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import org.junit.Test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2016 Federico Tomassetti
*
* 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.github.javaparser.symbolsolver.resolution.javaparser.contexts;

import com.github.javaparser.ParseException;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.symbolsolver.core.resolution.Context;
import com.github.javaparser.symbolsolver.javaparser.Navigator;
import com.github.javaparser.symbolsolver.javaparsermodel.contexts.MethodContext;
import com.github.javaparser.symbolsolver.model.declarations.*;
import com.github.javaparser.symbolsolver.model.resolution.SymbolReference;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* @author Malte Langkabel
*/
public class MethodContextResolutionTest extends AbstractResolutionTest {

private TypeSolver typeSolver;

@Before
public void setup() {
typeSolver = new ReflectionTypeSolver();
}

@Test
public void solveTypeRefToLocalClass() throws ParseException {
CompilationUnit cu = parseSample("MethodWithTypes");
ClassOrInterfaceDeclaration cd = Navigator.demandClass(cu, "Main");
MethodDeclaration md = Navigator.demandMethod(cd, "methodWithLocalTypes");
Context context = new MethodContext(md, typeSolver);

SymbolReference<TypeDeclaration> ref = context.solveType("LocalClass", new MemoryTypeSolver());
assertEquals(true, ref.isSolved());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Main {
public void methodWithLocalTypes() {
class LocalClass {
}
}
}

0 comments on commit d07c426

Please sign in to comment.