本文整理汇总了Java中japa.parser.JavaParser类的典型用法代码示例。如果您正苦于以下问题:Java JavaParser类的具体用法?Java JavaParser怎么用?Java JavaParser使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JavaParser类属于japa.parser包,在下文中一共展示了JavaParser类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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);
}
示例2: 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);
}
}
示例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);
}
}
示例4: 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;
}
示例5: resolveType
import japa.parser.JavaParser; //导入依赖的package包/类
public static JClass resolveType(JClassContainer _package, String typeDefinition) {
try {
FieldDeclaration fieldDeclaration = (FieldDeclaration) JavaParser.parseBodyDeclaration(typeDefinition + " foo;");
ClassOrInterfaceType c = (ClassOrInterfaceType) ((ReferenceType) fieldDeclaration.getType()).getType();
return buildClass(_package, c, 0);
} catch (ParseException e) {
throw new GenerationException(e);
}
}
示例6: 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();
}
}
}
示例7: 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());
}
}
示例8: 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());
}
}
示例9: 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());
}
}
示例10: 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);
}
}
示例11: 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);
}
}
示例12: 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;
}
示例13: 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());
}
示例14: 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());
}
示例15: 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;
}