当前位置: 首页>>代码示例>>Java>>正文


Java JavaParser.parse方法代码示例

本文整理汇总了Java中japa.parser.JavaParser.parse方法的典型用法代码示例。如果您正苦于以下问题:Java JavaParser.parse方法的具体用法?Java JavaParser.parse怎么用?Java JavaParser.parse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在japa.parser.JavaParser的用法示例。


在下文中一共展示了JavaParser.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: main

import japa.parser.JavaParser; //导入方法依赖的package包/类
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    try {
        if (args.length != 2) {
            System.out.println("usage: inputFile (.java) outputFile (.json)");
            System.exit(1);
        }

        File inputFile = new File(args[0]);
        String factFile = args[1];

        CompilationUnit compilationUnit = JavaParser.parse(inputFile);
        Fact fact = new Fact(inputFile, compilationUnit);

        FileWriter writer = new FileWriter(factFile);
        writer.write(gson.toJson(fact));
        writer.close();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
}
 
开发者ID:amritbhat786,项目名称:DocIT,代码行数:25,代码来源:JFactExtractor.java

示例2: main

import japa.parser.JavaParser; //导入方法依赖的package包/类
public static void main(String[] args) {
    if (args.length != 1) {
        System.out.println("usage: inputFile (.java)");
        System.exit(1);
    }
     
    String filePath = args[0];
    
    try {
        JavaParser.parse(new File(filePath));
    } catch (Exception ex) {
System.err.println(ex);
        System.exit(1);
    }
    
    System.exit(0);
}
 
开发者ID:amritbhat786,项目名称:DocIT,代码行数:18,代码来源:JValidator.java

示例3: parseType

import japa.parser.JavaParser; //导入方法依赖的package包/类
private JType parseType(String type, JCodeModel codeModel) {
	final String text = "public class Ignored extends " + type + " {}";
	try {
		CompilationUnit compilationUnit = JavaParser.parse(
				new ByteArrayInputStream(text.getBytes("UTF-8")), "UTF-8");
		final List<TypeDeclaration> typeDeclarations = compilationUnit
				.getTypes();
		final TypeDeclaration typeDeclaration = typeDeclarations.get(0);
		final ClassOrInterfaceDeclaration classDeclaration = (ClassOrInterfaceDeclaration) typeDeclaration;
		final List<ClassOrInterfaceType> _extended = classDeclaration
				.getExtends();
		final ClassOrInterfaceType classOrInterfaceType = _extended.get(0);

		return classOrInterfaceType.accept(
				this.typeToJTypeConvertingVisitor, codeModel);
	} catch (ParseException pex) {
		throw new IllegalArgumentException(
				"Could not parse the type definition [" + type + "].", pex);
	} catch (UnsupportedEncodingException uex) {
		throw new UnsupportedOperationException(uex);
	}
}
 
开发者ID:highsource,项目名称:jaxb2-basics,代码行数:23,代码来源:JavaTypeParser.java

示例4: getJavaType

import japa.parser.JavaParser; //导入方法依赖的package包/类
public final JavaType getJavaType(final String fileIdentifier) {
    Validate.notBlank(fileIdentifier, "Compilation unit path required");
    Validate.isTrue(new File(fileIdentifier).exists(),
            "The file doesn't exist");
    Validate.isTrue(new File(fileIdentifier).isFile(),
            "The identifier doesn't represent a file");
    try {
        final File file = new File(fileIdentifier);
        String typeContents = "";
        try {
            typeContents = FileUtils.readFileToString(file);
        }
        catch (IOException ignored) {
        }
        if (StringUtils.isBlank(typeContents)) {
            return null;
        }
        final CompilationUnit compilationUnit = JavaParser
                .parse(new ByteArrayInputStream(typeContents.getBytes()));
        final String typeName = fileIdentifier.substring(
                fileIdentifier.lastIndexOf(File.separator) + 1,
                fileIdentifier.lastIndexOf("."));
        for (final TypeDeclaration typeDeclaration : compilationUnit
                .getTypes()) {
            if (typeName.equals(typeDeclaration.getName())) {
                return new JavaType(compilationUnit.getPackage().getName()
                        .getName()
                        + "." + typeDeclaration.getName());
            }
        }
        return null;
    }
    catch (final ParseException e) {
        throw new IllegalStateException("Failed to parse " + fileIdentifier
                + " : " + e.getMessage());
    }
}
 
开发者ID:gvSIGAssociation,项目名称:gvnix1,代码行数:38,代码来源:JavaParserTypeResolutionService.java

示例5: createCompilationUnitFromFile

import japa.parser.JavaParser; //导入方法依赖的package包/类
private CompilationUnit createCompilationUnitFromFile(File aFile) throws IOException {
    FileInputStream theStream = null;
    try {
        theStream = new FileInputStream(aFile);
        return JavaParser.parse(theStream);
    } catch (ParseException e) {
        throw new IOException(e);
    } finally {
        if (theStream != null) {
            theStream.close();
        }
    }
}
 
开发者ID:mirkosertic,项目名称:ERDesignerNG,代码行数:14,代码来源:OpenXavaGenerator.java

示例6: getPackage

import japa.parser.JavaParser; //导入方法依赖的package包/类
public final JavaPackage getPackage(final String fileIdentifier) {
    Validate.notBlank(fileIdentifier, "Compilation unit path required");
    Validate.isTrue(new File(fileIdentifier).exists(),
            "The file doesn't exist");
    Validate.isTrue(new File(fileIdentifier).isFile(),
            "The identifier doesn't represent a file");
    try {
        final File file = new File(fileIdentifier);
        String typeContents = "";
        try {
            typeContents = FileUtils.readFileToString(file);
        }
        catch (final IOException ignored) {
        }
        if (StringUtils.isBlank(typeContents)) {
            return null;
        }
        final CompilationUnit compilationUnit = JavaParser
                .parse(new ByteArrayInputStream(typeContents.getBytes()));
        if (compilationUnit == null || compilationUnit.getPackage() == null) {
            return null;
        }
        return new JavaPackage(compilationUnit.getPackage().getName()
                .toString());
    }
    catch (final ParseException e) {
        throw new IllegalStateException("Failed to parse " + fileIdentifier
                + " : " + e.getMessage());
    }
}
 
开发者ID:gvSIGAssociation,项目名称:gvnix1,代码行数:31,代码来源:JavaParserTypeResolutionService.java

示例7: getTypeFromString

import japa.parser.JavaParser; //导入方法依赖的package包/类
public ClassOrInterfaceTypeDetails getTypeFromString(
        final String fileContents, final String declaredByMetadataId,
        final JavaType typeName) {
    if (StringUtils.isBlank(fileContents)) {
        return null;
    }

    Validate.notBlank(declaredByMetadataId,
            "Declaring metadata ID required");
    Validate.notNull(typeName, "Java type to locate required");
    try {
        final CompilationUnit compilationUnit = JavaParser
                .parse(new ByteArrayInputStream(fileContents.getBytes()));
        final TypeDeclaration typeDeclaration = JavaParserUtils
                .locateTypeDeclaration(compilationUnit, typeName);
        if (typeDeclaration == null) {
            return null;
        }
        return JavaParserClassOrInterfaceTypeDetailsBuilder.getInstance(
                compilationUnit, null, typeDeclaration,
                declaredByMetadataId, typeName, metadataService,
                typeLocationService).build();
    }
    catch (final ParseException e) {
        throw new IllegalStateException("Failed to parse " + typeName
                + " : " + e.getMessage());
    }
}
 
开发者ID:gvSIGAssociation,项目名称:gvnix1,代码行数:29,代码来源:JavaParserTypeParsingService.java

示例8: parseFile

import japa.parser.JavaParser; //导入方法依赖的package包/类
/**
 * 
 * @param file
 * 		?	
 * @param level
 * 		?	
 * @throws ParseException
 * 		?	
 * @throws IOException
 * 		?	
 */
private static void parseFile(File file, int level) throws ParseException, IOException
{	for(int i=0;i<level;i++)
		System.out.print("..");
	System.out.println("Analyse de la classe "+file.getPath());
	
	// creates an input stream for the file to be parsed
       FileInputStream in = new FileInputStream(file);
       
       CompilationUnit cu;
       try
       {	// parse the file
           cu = JavaParser.parse(in);
       }
       finally
       {	in.close();
       }

       // display trace
       AiVisitor visitor = new AiVisitor(level);
       visitor.visit(cu,null);
       int errorCount = visitor.getErrorCount();
       if(errorCount>0)
       {	for(int i=0;i<level;i++)
			System.out.print("..");
       	System.out.println("   total : "+errorCount);
       }
}
 
开发者ID:vlabatut,项目名称:totalboumboum,代码行数:39,代码来源:ParseAi.java

示例9: parseFile

import japa.parser.JavaParser; //导入方法依赖的package包/类
/**
 * Analyse un fichier source.
 * 
 * @param file
 * 		Objet représentant le fichier source à analyser.
 * @param level
 * 		Niveau hiérarchique dans l'arbre d'appels.
 * 
 * @throws ParseException
 * 		Erreur en analysant un des fichiers source.
 * @throws IOException
 * 		Erreur en ouvrant un des fichiers source.
 */
private static void parseFile(File file, int level) throws ParseException, IOException
{	for(int i=0;i<level;i++)
		System.out.print("..");
	System.out.println("Analyse de la classe "+file.getPath());
	
	// creates an input stream for the file to be parsed
       FileInputStream in = new FileInputStream(file);
       
       CompilationUnit cu;
       try
       {	// parse the file
           cu = JavaParser.parse(in);
       }
       finally
       {	in.close();
       }

       // display trace
       AiVisitor visitor = new AiVisitor(level);
       visitor.visit(cu,null);
       int errorCount = visitor.getErrorCount();
       if(errorCount>0)
       {	for(int i=0;i<level;i++)
			System.out.print("..");
       	System.out.println("   total : "+errorCount);
       }
}
 
开发者ID:vlabatut,项目名称:totalboumboum,代码行数:41,代码来源:ParseAi.java

示例10: parseFile

import japa.parser.JavaParser; //导入方法依赖的package包/类
/**
 * Analyse un fichier source.
 * 
 * @param file
 * 		Objet représentant le fichier source à analyser.
 * @param level
 * 		Niveau hiérarchique dans l'arbre d'appels.
 * @return 
 * 		Le nombre d'erreurs pour le fichier traité.
 * 
 * @throws ParseException
 * 		Erreur en analysant un des fichiers source.
 * @throws IOException
 * 		Erreur en ouvrant un des fichiers source.
 */
private static int parseFile(File file, int level) throws ParseException, IOException
{	for(int i=0;i<level;i++)
		System.out.print("..");
	System.out.println("Analyse du fichier "+file.getPath());
	
	// creates an input stream for the file to be parsed
       FileInputStream in = new FileInputStream(file);
       
       CompilationUnit cu;
       try
       {	// parse the file
           cu = JavaParser.parse(in);
       }
       finally
       {	in.close();
       }

       // display trace
       AiVisitor visitor = new AiVisitor(level);
       visitor.visit(cu,null);
       int result = visitor.getErrorCount();
       if(result>0)
       {	for(int i=0;i<level;i++)
			System.out.print("..");
       	System.out.println("   total pour le fichier "+file.getName()+" : "+result);
       }
       
       return result;
}
 
开发者ID:vlabatut,项目名称:totalboumboum,代码行数:45,代码来源:ParseAi.java

示例11: printCompilationUnit

import japa.parser.JavaParser; //导入方法依赖的package包/类
public static void printCompilationUnit(String fileName) throws Exception{
    FileInputStream in = new FileInputStream(fileName);
    CompilationUnit compUnit;
    try {
        compUnit = JavaParser.parse(in);
    } finally {
        in.close();
    }
    
    System.out.println(compUnit.toString());
}
 
开发者ID:damorim,项目名称:compilers-cin,代码行数:12,代码来源:ParserExample.java

示例12: testParse

import japa.parser.JavaParser; //导入方法依赖的package包/类
public void testParse() throws Exception {

		final String text = "public class Dummy implements java.util.Comparator<java.lang.Integer>{}";

		final CompilationUnit compilationUnit = JavaParser.parse(
				new ByteArrayInputStream(text.getBytes("UTF-8")), "UTF-8");
		final List<TypeDeclaration> typeDeclarations = compilationUnit
				.getTypes();
		assertEquals(1, typeDeclarations.size());
		final TypeDeclaration typeDeclaration = typeDeclarations.get(0);
		assertTrue(typeDeclaration instanceof ClassOrInterfaceDeclaration);
		final ClassOrInterfaceDeclaration classDeclaration = (ClassOrInterfaceDeclaration) typeDeclaration;
		assertEquals("Dummy", classDeclaration.getName());
		final List<ClassOrInterfaceType> implementedInterfaces = classDeclaration
				.getImplements();
		assertEquals(1, implementedInterfaces.size());
		final ClassOrInterfaceType implementedInterface = implementedInterfaces
				.get(0);
		assertEquals("Comparator", implementedInterface.getName());
		final List<Type> typeArgs = implementedInterface.getTypeArgs();
		assertEquals(1, typeArgs.size());
		final Type typeArg = typeArgs.get(0);
		assertTrue(typeArg instanceof ReferenceType);
		final ReferenceType referenceTypeArg = (ReferenceType) typeArg;
		final Type referencedType = referenceTypeArg.getType();
		assertTrue(referencedType instanceof ClassOrInterfaceType);
		final ClassOrInterfaceType typeArgType = (ClassOrInterfaceType) referencedType;
		assertEquals("Integer", typeArgType.getName());

	}
 
开发者ID:highsource,项目名称:jaxb2-basics,代码行数:31,代码来源:JavaTypeParserTest.java

示例13: parse

import japa.parser.JavaParser; //导入方法依赖的package包/类
@Override
public ArrayList<JavaSegment> parse (String classFile) throws Exception {
	CompilationUnit unit = JavaParser.parse(new ByteArrayInputStream(classFile.getBytes()));
	ArrayList<JavaMethod> methods = new ArrayList<JavaMethod>();
	getJavaMethods(methods, getOuterClass(unit));
	ArrayList<JniSection> methodBodies = getNativeCodeBodies(classFile);
	ArrayList<JniSection> sections = getJniSections(classFile);
	alignMethodBodies(methods, methodBodies);
	ArrayList<JavaSegment> segments = sortMethodsAndSections(methods, sections);
	return segments;
}
 
开发者ID:basherone,项目名称:libgdxcn,代码行数:12,代码来源:RobustJavaMethodParser.java

示例14: parse

import japa.parser.JavaParser; //导入方法依赖的package包/类
private static CompilationUnit parse(List<String> lines) throws Exception {
	StringWriter writer = new StringWriter();
	write(lines, writer);
	CompilationUnit result = JavaParser.parse(new ByteArrayInputStream(writer.getBuffer().toString().getBytes()));

	return result;
}
 
开发者ID:jedwards1211,项目名称:breakout,代码行数:8,代码来源:ModifiableCompilationUnit.java

示例15: writeBuilder

import japa.parser.JavaParser; //导入方法依赖的package包/类
/**
 * Finds the java file a generated builder belongs in, and attempts to insert the builder code, or replace the existing auto-generated types and methods if
 * necessary.<br>
 * <i>Requres JavaParser 1.0.8 or greater.</i><br>
 * <br>
 * This method is crazy. Finding and modifying the file is complicated, and this method makes a backup, but who knows what could go wrong. USE AT YOUR OWN
 * RISK!
 * 
 * @param builder
 *            a builder generated by {@code #generateBuilders(Class...)}.
 * @throws Exception
 *             for any number of reasons...
 */
public static void writeBuilder( String builder ) throws Exception
{
	CompilationUnit compunit = JavaParser.parse( new ByteArrayInputStream( builder.getBytes( ) ) );
	
	for( TypeDeclaration typeDecl : compunit.getTypes( ) )
	{
		File javaFile = findJavaFile( typeDecl );
		if( javaFile != null )
		{
			List<String> lines = insertBuilder( typeDecl , javaFile );
			
			File backup = new File( javaFile + "_backup" );
			File newFile = new File( javaFile + "_new" );
			System.out.println( "Copying " + javaFile + " to " + backup );
			copy( javaFile , backup );
			System.out.println( "Writing " + newFile + "..." );
			write( lines , newFile );
			System.out.println( "Writing " + javaFile + "..." );
			write( lines , javaFile );
			System.out.println( "Deleting " + backup + "..." );
			backup.delete( );
			System.out.println( "Deleting " + newFile + "..." );
			newFile.delete( );
		}
	}
}
 
开发者ID:jedwards1211,项目名称:breakout,代码行数:40,代码来源:BuilderGenerator.java


注:本文中的japa.parser.JavaParser.parse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。