本文整理汇总了Java中com.github.javaparser.ParseException类的典型用法代码示例。如果您正苦于以下问题:Java ParseException类的具体用法?Java ParseException怎么用?Java ParseException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ParseException类属于com.github.javaparser包,在下文中一共展示了ParseException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: analyze
import com.github.javaparser.ParseException; //导入依赖的package包/类
public List<EntityField> analyze(File source) throws IOException, ParseException {
List<EntityField> entityFields = new ArrayList<>();
CompilationUnit cu = JavaParser.parse(source);
cu.accept(new VoidVisitorAdapter<List<EntityField>>() {
@Override
public void visit(FieldDeclaration fd, List<EntityField> f) {
if (fd.getAnnotations().stream().anyMatch(anno -> anno.getName().getName().equals("Column"))) {
Class<?> type = null;
switch (fd.getType().toString()) {
case "String": type = String.class; break;
case "Long": type = Long.class; break;
case "Integer": type = Integer.class; break;
case "boolean": type = boolean.class; break;
}
if (type == null) return;
f.add(new EntityField(
fd.getVariables().get(0).getId().getName(),
type,
fd.getAnnotations().stream().anyMatch(anno -> anno.getName().getName().equals("Id"))));
}
}
}, entityFields);
return entityFields;
}
示例2: PackageNode
import com.github.javaparser.ParseException; //导入依赖的package包/类
PackageNode( File file) throws ParseException, IOException
{
super(file.getName());
this.file = file;
File[] files = file.listFiles();
for(int i = 0; i< files.length; i++)
{
if (files[i].isDirectory()) {
add( new PackageNode((files[i])));
}
else if(files[i].getAbsolutePath().endsWith(".java"))
{
add( new ClassNode(files[i]));
}
}
}
示例3: getPackageNmae
import com.github.javaparser.ParseException; //导入依赖的package包/类
public static String getPackageNmae(File f) {
if (f == null || !f.exists())
return "";
if (f.getName().endsWith(".java")) {
CompilationUnit parse;
try {
parse = com.github.javaparser.JavaParser.parse(f, "utf-8");
return GargoyleJavaParser.getPackageName(parse);
} catch (ParseException | IOException e) {
LOGGER.error(ValueUtil.toString(e));
}
}
return "";
}
示例4: getPackageName
import com.github.javaparser.ParseException; //导入依赖的package包/类
/********************************
* 작성일 : 2016. 7. 14. 작성자 : KYJ
*
*
* @param javaFile
* @param converter
* @return
* @throws FileNotFoundException
* @throws IOException
* @throws ParseException
********************************/
public static String getPackageName(File javaFile, FileCheckHandler<String> converter)
throws FileNotFoundException, IOException, ParseException {
String packageName = null;
if (javaFile == null) {
packageName = converter.ifNull();
} else if (!javaFile.exists())
packageName = converter.notExists();
else if (javaFile.isFile()) {
CompilationUnit cu = getCompileUnit(javaFile);
packageName = getPackageName(cu);
}
return packageName;
}
示例5: extractClassName
import com.github.javaparser.ParseException; //导入依赖的package包/类
@Nullable
private static String extractClassName( String code, PrintStream err ) {
InputStream inputStream = new ByteArrayInputStream( code.getBytes( StandardCharsets.UTF_8 ) );
try {
CompilationUnit compilationUnit = JavaParser.parse( inputStream );
List<TypeDeclaration> types = compilationUnit.getTypes();
if ( types.size() == 1 ) {
String simpleType = types.get( 0 ).getName();
return Optional.ofNullable( compilationUnit.getPackage() )
.map( PackageDeclaration::getPackageName )
.map( it -> it + "." + simpleType )
.orElse( simpleType );
} else if ( types.size() == 0 ) {
err.println( "No class definition found" );
} else {
err.println( "Too many class definitions found. Only one class can be defined at a time." );
}
} catch ( ParseException e ) {
// ignore error, let the compiler provide an error message
return "Err";
}
return null;
}
示例6: setUp
import com.github.javaparser.ParseException; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
SourceWriter sourceWriter = (packageName, simpleClassName, source) -> {
try {
JavaParser.parse(new StringReader(source), true);
} catch (ParseException e) {
// Source code failed to parse. Throw an exception that will cause the test to fail.
throw new RuntimeException("Failed to parse generated source code", e);
}
};
PortletTypeDescriptor descriptor = new PortletTypeDescriptor(
"edu.stanford.protege.MyPortlet",
"MyPortlet",
"edu.stanford.protege",
"my.portlet.id",
"My Portlet\"\"!",
"My amazing portlet.\n(Does all \"sorts\" of things)");
PortletModuleDescriptor moduleDescriptorA = new PortletModuleDescriptor("edu.stanford.protege.MyPortletModuleA");
PortletModuleDescriptor moduleDescriptorB = new PortletModuleDescriptor("edu.stanford.protege.MyPortletModuleB");
codeGenerator = new WebProtegeCodeGeneratorVelocityImpl(
Collections.singleton(descriptor),
Sets.newHashSet(moduleDescriptorA, moduleDescriptorB),
sourceWriter
);
}
示例7: resolveUsageOfGenericFieldAdvancedCase
import com.github.javaparser.ParseException; //导入依赖的package包/类
@Test
public void resolveUsageOfGenericFieldAdvancedCase() throws ParseException {
CompilationUnit cu = parseSample("Generics");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "SomeCollection");
MethodDeclaration method = Navigator.demandMethod(clazz, "foo2");
ExpressionStmt stmt = (ExpressionStmt) method.getBody().get().getStatements().get(0);
Expression expression = stmt.getExpression();
ResolvedType type = JavaParserFacade.get(new ReflectionTypeSolver()).getType(expression);
assertEquals(false, type.isTypeVariable());
assertEquals("java.util.List<java.lang.String>", type.describe());
assertEquals(1, type.asReferenceType().typeParametersValues().size());
assertEquals(false, type.asReferenceType().typeParametersValues().get(0).isTypeVariable());
assertEquals("java.lang.String", type.asReferenceType().typeParametersValues().get(0).describe());
}
示例8: testHasDirectlyAnnotation
import com.github.javaparser.ParseException; //导入依赖的package包/类
@Test
public void testHasDirectlyAnnotation() throws IOException, ParseException {
TypeSolver typeSolver = new ReflectionTypeSolver();
CompilationUnit cu = JavaParser.parse(adaptPath(new File("src/test/resources/Annotations.java.txt")));
JavaParserClassDeclaration ca = new JavaParserClassDeclaration(Navigator.demandClass(cu, "CA"), typeSolver);
assertEquals(true, ca.hasDirectlyAnnotation("foo.bar.MyAnnotation"));
assertEquals(false, ca.hasDirectlyAnnotation("foo.bar.MyAnnotation2"));
assertEquals(false, ca.hasDirectlyAnnotation("MyAnnotation"));
assertEquals(false, ca.hasDirectlyAnnotation("foo.bar.MyUnexistingAnnotation"));
JavaParserClassDeclaration cb = new JavaParserClassDeclaration(Navigator.demandClass(cu, "CB"), typeSolver);
assertEquals(false, cb.hasDirectlyAnnotation("foo.bar.MyAnnotation"));
assertEquals(true, cb.hasDirectlyAnnotation("foo.bar.MyAnnotation2"));
assertEquals(false, cb.hasDirectlyAnnotation("MyAnnotation"));
assertEquals(false, cb.hasDirectlyAnnotation("foo.bar.MyUnexistingAnnotation"));
}
示例9: resolveFieldWithGenericTypeToInteger
import com.github.javaparser.ParseException; //导入依赖的package包/类
@Test
public void resolveFieldWithGenericTypeToInteger() throws ParseException {
CompilationUnit cu = parseSample("Generics");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Generics");
VariableDeclarator fieldS = Navigator.demandField(clazz, "i");
SymbolSolver symbolSolver = new SymbolSolver(new ReflectionTypeSolver());
Optional<Value> symbolReference = symbolSolver.solveSymbolAsValue("i", fieldS);
assertEquals(true, symbolReference.isPresent());
assertEquals("i", symbolReference.get().getName());
ResolvedType type = symbolReference.get().getType();
assertEquals(1, type.asReferenceType().typeParametersValues().size());
assertEquals("java.lang.Integer", type.asReferenceType().typeParametersValues().get(0).describe());
}
示例10: parseModelDeclarations
import com.github.javaparser.ParseException; //导入依赖的package包/类
@Test
public void parseModelDeclarations() throws IOException, ParseException {
parse("java-symbol-solver-model", "com/github/javaparser/symbolsolver/model/declarations/AccessLevel");
parse("java-symbol-solver-model", "com/github/javaparser/symbolsolver/model/declarations/AnnotationDeclaration");
parse("java-symbol-solver-model", "com/github/javaparser/symbolsolver/model/declarations/ClassDeclaration");
parse("java-symbol-solver-model", "com/github/javaparser/symbolsolver/model/declarations/ConstructorDeclaration");
parse("java-symbol-solver-model", "com/github/javaparser/symbolsolver/model/declarations/Declaration");
parse("java-symbol-solver-model", "com/github/javaparser/symbolsolver/model/declarations/EnumDeclaration");
parse("java-symbol-solver-model", "com/github/javaparser/symbolsolver/model/declarations/FieldDeclaration");
parse("java-symbol-solver-model", "com/github/javaparser/symbolsolver/model/declarations/HasAccessLevel");
parse("java-symbol-solver-model", "com/github/javaparser/symbolsolver/model/declarations/InterfaceDeclaration");
parse("java-symbol-solver-model", "com/github/javaparser/symbolsolver/model/declarations/MethodAmbiguityException");
parse("java-symbol-solver-model", "com/github/javaparser/symbolsolver/model/declarations/MethodDeclaration");
parse("java-symbol-solver-model", "com/github/javaparser/symbolsolver/model/declarations/MethodLikeDeclaration");
parse("java-symbol-solver-model", "com/github/javaparser/symbolsolver/model/declarations/ParameterDeclaration");
parse("java-symbol-solver-model", "com/github/javaparser/symbolsolver/model/declarations/ReferenceTypeDeclaration");
parse("java-symbol-solver-model", "com/github/javaparser/symbolsolver/model/declarations/TypeDeclaration");
parse("java-symbol-solver-model", "com/github/javaparser/symbolsolver/model/declarations/TypeParameterDeclaration");
parse("java-symbol-solver-model", "com/github/javaparser/symbolsolver/model/declarations/TypeParametrizable");
parse("java-symbol-solver-model", "com/github/javaparser/symbolsolver/model/declarations/ValueDeclaration");
}
示例11: resolveReferenceToClassInJavaLang
import com.github.javaparser.ParseException; //导入依赖的package包/类
@Test
public void resolveReferenceToClassInJavaLang() throws ParseException {
CompilationUnit cu = parseSample("Navigator");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "Navigator");
MethodDeclaration method = Navigator.demandMethod(referencesToField, "findType");
Parameter param = method.getParameters().get(1);
ResolvedClassDeclaration stringDecl = createMock(ResolvedClassDeclaration.class);
expect(stringDecl.getName()).andReturn("String");
expect(stringDecl.getQualifiedName()).andReturn("java.lang.String");
TypeSolver typeSolver = createMock(TypeSolver.class);
expect(typeSolver.tryToSolveType("me.tomassetti.symbolsolver.javaparser.String")).andReturn(SymbolReference.unsolved(ResolvedReferenceTypeDeclaration.class));
expect(typeSolver.getRoot()).andReturn(typeSolver);
expect(typeSolver.solveType("java.lang.Object")).andReturn(new ReflectionClassDeclaration(Object.class, typeSolver));
expect(typeSolver.tryToSolveType("java.lang.String")).andReturn(SymbolReference.solved(stringDecl));
SymbolSolver symbolSolver = new SymbolSolver(typeSolver);
replay(typeSolver, stringDecl);
SymbolReference<? extends ResolvedTypeDeclaration> ref = symbolSolver.solveType("String", param);
assertEquals(true, ref.isSolved());
assertEquals("String", ref.getCorrespondingDeclaration().getName());
assertEquals("java.lang.String", ref.getCorrespondingDeclaration().getQualifiedName());
verify(typeSolver, stringDecl);
}
示例12: resolveFieldWithGenericTypeToDeclaredClass
import com.github.javaparser.ParseException; //导入依赖的package包/类
@Test
public void resolveFieldWithGenericTypeToDeclaredClass() throws ParseException {
CompilationUnit cu = parseSample("Generics");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Generics");
VariableDeclarator fieldS = Navigator.demandField(clazz, "g");
SymbolSolver symbolSolver = new SymbolSolver(new ReflectionTypeSolver());
Optional<Value> symbolReference = symbolSolver.solveSymbolAsValue("g", fieldS);
assertEquals(true, symbolReference.isPresent());
assertEquals("g", symbolReference.get().getName());
ResolvedType type = symbolReference.get().getType();
assertEquals(1, type.asReferenceType().typeParametersValues().size());
assertEquals("me.tomassetti.symbolsolver.javaparser.Generics", type.asReferenceType().typeParametersValues().get(0).describe());
}
示例13: solveParameterOfLambdaInMethodCallExpr
import com.github.javaparser.ParseException; //导入依赖的package包/类
@Test
public void solveParameterOfLambdaInMethodCallExpr() throws ParseException {
CompilationUnit cu = parseSample("Lambda");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Agenda");
MethodDeclaration method = Navigator.demandMethod(clazz, "lambdaMap");
ReturnStmt returnStmt = Navigator.findReturnStmt(method);
MethodCallExpr methodCallExpr = (MethodCallExpr) returnStmt.getExpression().get();
LambdaExpr lambdaExpr = (LambdaExpr) methodCallExpr.getArguments().get(0);
Context context = new LambdaExprContext(lambdaExpr, typeSolver);
Optional<Value> ref = context.solveSymbolAsValue("p", typeSolver);
assertTrue(ref.isPresent());
assertEquals("? super java.lang.String", ref.get().getType().describe());
}
示例14: resolveReferenceToMethod
import com.github.javaparser.ParseException; //导入依赖的package包/类
@Test
public void resolveReferenceToMethod() throws ParseException, IOException {
CompilationUnit cu = parseSample("Navigator");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "Navigator");
MethodDeclaration method = Navigator.demandMethod(referencesToField, "findType");
MethodCallExpr callToGetTypes = Navigator.findMethodCall(method, "getTypes");
String pathToJar = adaptPath("src/test/resources/javaparser-core-2.1.0.jar");
TypeSolver typeSolver = new CombinedTypeSolver(new JarTypeSolver(pathToJar), new ReflectionTypeSolver(true));
SymbolSolver symbolSolver = new SymbolSolver(typeSolver);
MethodUsage ref = symbolSolver.solveMethod("getTypes", Collections.emptyList(), callToGetTypes);
assertEquals("getTypes", ref.getName());
assertEquals("com.github.javaparser.ast.CompilationUnit", ref.declaringType().getQualifiedName());
//verify(typeSolver);
}
示例15: resolveLocalAndSeveralAnnidatedLevels
import com.github.javaparser.ParseException; //导入依赖的package包/类
@Test
public void resolveLocalAndSeveralAnnidatedLevels() throws ParseException {
CompilationUnit cu = parseSample("LocalVariableInParent");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "LocalVariableInParent");
MethodDeclaration method = Navigator.demandMethod(referencesToField, "foo4");
MethodCallExpr call = Navigator.findMethodCall(method, "add");
TypeSolver typeSolver = new ReflectionTypeSolver();
SymbolReference<? extends ResolvedValueDeclaration> ref = JavaParserFacade.get(typeSolver).solve(call.getScope().get());
assertTrue(ref.isSolved());
assertEquals("java.util.List<Comment>", ref.getCorrespondingDeclaration().getType().describe());
MethodUsage methodUsage = JavaParserFacade.get(typeSolver).solveMethodAsUsage(call);
assertEquals("add", methodUsage.getName());
}